forked from mtu-most/linear-actuator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump-server
executable file
·132 lines (122 loc) · 3.85 KB
/
pump-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
# Copyright 2013 Michigan Technological University
# Author: Bas Wijnen <[email protected]>
# This design was developed as part of a project with
# the Michigan Tech Open Sustainability Technology Research Group
# http://www.appropedia.org/Category:MOST
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or(at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import websockets
import sys
import argparse
import time
try:
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
except:
sys.stderr.write('Warning: using emulation because RPi.GPIO could not be used\n')
class gpio:
LOW = 0
HIGH = 1
IN = 0
OUT = 1
@classmethod
def output(cls, pin, state):
pass
@classmethod
def setup(cls, pin, type, initial):
pass
DIR = 11
STEP = 13
SLEEP = 15
MS3 = 19
MS2 = 21
MS1 = 23
gpio.setup(SLEEP, gpio.OUT, initial = gpio.HIGH)
gpio.setup(STEP, gpio.OUT, initial = gpio.HIGH)
gpio.setup(DIR, gpio.OUT, initial = gpio.HIGH)
gpio.setup(MS1, gpio.OUT, initial = gpio.HIGH)
gpio.setup(MS2, gpio.OUT, initial = gpio.HIGH)
gpio.setup(MS3, gpio.OUT, initial = gpio.HIGH)
a = argparse.ArgumentParser()
a.add_argument('--port', help = 'port to listen on(8000)', default = '8000')
a.add_argument('--pitch', help = 'lead screw pitch(.8 mm; M5)', default = .8, type = float)
a.add_argument('--steps', help = 'motor steps per revolution(3200)', default = 3200, type = float)
a.add_argument('--html', help = 'path to html files', default = 'html')
args = a.parse_args()
class Pump:
def __init__(self, pitch, steps):
self.position = 0.
self.steps_per_mm = steps / pitch
self.ml_per_s = .7
self.ml_per_mm = 0.16355310023987787
def calibrate(self, ml_per_mm):
self.ml_per_mm = ml_per_mm * 1.
notify('calibration', self.ml_per_mm)
def setposition(self, position):
self.position = position
notify('position', self.position)
def goto(self, ml):
self.move(ml - self.position)
def move(self, ml):
gpio.output(SLEEP, gpio.HIGH)
gpio.output(DIR, gpio.HIGH if ml < 0 else gpio.LOW)
notify('move', self.position, ml)
# ml/mm
# ml/s
# step/mm
# ml
# s/step ?
s_per_half_step = self.ml_per_mm / self.steps_per_mm / self.ml_per_s / 2
steps = int(ml / self.ml_per_mm * self.steps_per_mm + .5)
target = time.time()
for t in range(abs(steps)):
gpio.output(STEP, gpio.HIGH)
target += s_per_half_step
while time.time() < target:
# Yes, this is a busy wait.
pass
gpio.output(STEP, gpio.LOW)
target += s_per_half_step
while time.time() < target:
# Yes, this is a busy wait.
pass
self.position += ml
notify('position', self.position)
def speed(self, ml_per_s):
self.ml_per_s = ml_per_s
notify('speed', ml_per_s)
def sleep(self):
gpio.output(SLEEP, gpio.LOW)
users = set()
def send(socket, arg):
getattr(socket, 'notify') (*arg)
def notify(*arg):
for u in users:
send(u, arg)
p = Pump(args.pitch, args.steps)
class Connection:
def __init__(self, socket):
self.socket = socket
def monitor(self):
users.add(self.socket)
return(('calibration', p.ml_per_mm), ('position', p.position), ('speed', p.ml_per_s))
def __getattr__(self, attr):
return getattr(p, attr)
def disconnect(socket):
if socket in users:
users.remove(socket)
s = websockets.RPChttpd(args.port, httpdirs = args.html.split(), target = Connection, disconnect_cb = disconnect)
websockets.fgloop()