forked from AbateLab/Pump-Control-Program
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpf_filter_comm.py
143 lines (107 loc) · 4.78 KB
/
pf_filter_comm.py
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
133
134
135
136
137
138
139
140
141
142
import serial
import socket
import new_era
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#syringe pump serial port
pump = serial.Serial('/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0', 19200)
#actual 3D printer that all pronterface commands will be routed to
r3dp = serial.Serial('/dev/serial/by-id/usb-Uberclock_Smoothieboard_0901100BAEAB2005551F8D76F50020C4-if00',19200)
#dictionary list for syringe pump commands
pumpCommands = {'S1':'RUN', #S1 is for running all syringe pumps at once
'S2':'STP', #S2 is for stopping all syringe pumps at once
'S3':'*DIRINF\x0D', #S3 is to set all syringe pumps forward
'S4':'*DIRWDR\x0D', #S4 is to set all syringe pumps backwards
'S5':'RAT'} #S5 is for rates, since there's more to it I'm just gonna make an arbitrary thing for the program to look out for later
#important socket setup
TCP_IP = '10.0.0.100'
TCP_PORT = 5005
BUFFER_SIZE = 50 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
pronter, addr = s.accept()
print 'Connected!'
allSystemsGo = 0
while(1):
#need to store the commands into a queue so we don't lose commands
#next, make sure connection to real 3d printer is good
if r3dp.isOpen():
#next, check that the syringe connection is good
if pump.isOpen():
if allSystemsGo is 0: #run once
new_era.run_all(pump)
new_era.stop_all(pump) #this is to wake up the pumps
#set the all go variable!
allSystemsGo = 1
else:
print "The syringe pump connection is no good."
else:
print "The 3D printer connection is no good."
#--------------------------------------------------------------------------------------------------
#Can we do stuff??
if allSystemsGo:
#read in the command from pronterface
val = pronter.recv(BUFFER_SIZE)
print val #it seems that the messages come out length + 1 because of pronterface
#then time to filter!
#what to do with our pump commands
if val[0] is 'S':
#check if we will selectively choose a pump to run
if pumpCommands[val[0]+val[1]] is 'RUN':
#further figure out what kind of
if len(val) > 3: #val[2] is "_": #command will be P1_adr
print val[3]
adr = int(val[3:])
new_era.run_pump(pump, adr)
else:
#print 'run all pumps!'
new_era.run_all(pump)
#check if we will selectively choose a pump to stop
elif pumpCommands[val[0]+val[1]] is 'STP':
if len(val) > 3: #val[2] is "-": #command will be P2_adr
adr = int(val[3:])
new_era.stop_pump(pump, adr)
else:
#print 'stop all pumps!'
new_era.stop_all(pump)
# #then we will check the second letter to determine what the command actually is
elif pumpCommands[val[0]+val[1]] is 'RAT':
#brute forcing the logic to get adr (1 or 2 values)
if val[4] is '_': #one address digit
#print 'one digit adr rate set!'
#print val[3]
#print val[4]
#print val[5:]
adr = int(val[3])
rat = float(val[5:])
rate2set = {adr:rat}
new_era.set_rates(pump,rate2set)
else: #two address digits
#print 'two digit adr rate set!'
adr = int(val[3]+val[4])
rat = float(val[6:])
rate2set = {adr:rat}
new_era.set_rates(pump,rate2set)
else:
#print 'infuse/withdraw set!'
pump.write(pumpCommands[val[0]+val[1]])
#if its not an S command, then we need to send it to the printer
else:
r3dp.write(val)
#if that's not working, don't do anything
else:
pass
#check if theres anything else to communicate
#back to pronterface from the printer
check = r3dp.inWaiting()
#print 'in waiting -' + str(check)
if check:
printer = r3dp.read(check)
pronter.send(printer)
#print 'sent to pronterface - ' + str(printer)