-
Notifications
You must be signed in to change notification settings - Fork 6
/
cryo-con.py
executable file
·378 lines (344 loc) · 20.3 KB
/
cryo-con.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
import os
import sys
import re
import numpy as np
from daemon import SimpleFactory, SimpleProtocol
from command import Command
from daemon import catch
class DaemonProtocol(SimpleProtocol):
_debug = False # Display all traffic for debug purposes
_simulator = False
@catch
def processMessage(self, string):
# It will handle some generic messages and return pre-parsed Command object
cmd = SimpleProtocol.processMessage(self, string)
if cmd is None:
return
obj = self.object # Object holding the state
hw = obj['hw'] # HW factory
string=string.strip()
STRING = string.upper()
while True:
if cmd.name == 'get_status':
self.message('status hw_connected=%s status=%s temperatureA=%g temperatureB=%g temperatureC=%g temperatureD=%g control=%s\
htr_status1=%s range1=%s ctrl_type1=%s pwr_set1=%g pwr_actual1=%g load1=%g source1=%s set_point1=%g ramp1=%s rate1=%g pwr_man1=%s\
htr_status2=%s range2=%s ctrl_type2=%s pwr_set2=%g pwr_actual2=%g load2=%g source2=%s set_point2=%g ramp2=%s rate2=%g pwr_man2=%s\
htr_status3=%s range3=%s ctrl_type3=%s pwr_set3=%s pwr_actual3=%s load3=%s source3=%s set_point3=%g ramp3=%s rate3=%g pwr_man3=%s\
htr_status4=%s range4=%s ctrl_type4=%s pwr_set4=%s pwr_actual4=%s load4=%s source4=%s set_point4=%g ramp4=%s rate4=%g pwr_man4=%s'
% (self.object['hw_connected'], self.object['status'],
self.object['temperatureA'], self.object['temperatureB'], self.object['temperatureC'], self.object['temperatureD'], self.object['control'],
self.object['htr_status1'], self.object['range1'], self.object['ctrl_type1'], self.object[
'pwr_set1'], self.object['pwr_actual1'], self.object['load1'],
self.object['source1'], self.object['set_point1'], self.object['ramp1'], self.object['rate1'], self.object['pwr_man1'],
self.object['htr_status2'], self.object['range2'], self.object['ctrl_type2'], self.object[
'pwr_set2'], self.object['pwr_actual2'], self.object['load2'],
self.object['source2'], self.object['set_point2'], self.object['ramp2'], self.object['rate2'], self.object['pwr_man2'],
self.object['htr_status3'], self.object['range3'], self.object['ctrl_type3'], self.object[
'pwr_set3'], self.object['pwr_actual3'], self.object['load3'],
self.object['source3'], self.object['set_point3'], self.object['ramp3'], self.object['rate3'], self.object['pwr_man3'],
self.object['htr_status4'], self.object['range4'], self.object['ctrl_type4'], self.object[
'pwr_set4'], self.object['pwr_actual4'], self.object['load4'],
self.object['source4'], self.object['set_point4'], self.object['ramp4'], self.object['rate4'], self.object['pwr_man4'],))
break
regex = re.compile(r'(CONT|CONTR|CONTRO|CONTROL)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
if STRING == 'STOP':
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'(CONT|CONTR|CONTRO|CONTROL)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
if STRING == '*OPC?':
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
if STRING == '*IDN?':
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(SOUR|SOURC|SOURCE)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(SOUR|SOURC|SOURCE) [A-D]')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(RANG|RANGE)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP 1:(RANG|RANGE) (HI|MID|LOW)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP 2:(RANG|RANGE) (HI|LOW)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [3-4]:(RANG|RANGE) (5V|10V)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:([PID]GA|[PID]GAI|[PID]GAIN)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:([PID]GA|[PID]GAI|[PID]GAIN) (\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
if float(STRING.split()[-1]) < 0 or float(STRING.split()[-1]) > 1000:
daemon.log('WARNING value out of range: ' +
string+' from connection '+self.name)
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(SETP|SETPT)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(SETP|SETPT) -?(\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(TYP|TYPE)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-2]:(TYP|TYPE) (OFF|PID|MAN|TABLE|RAMPP)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [3-4]:(TYP|TYPE) (OFF|PID|MAN|TABLE|RAMPP|SCALE)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(MAXP|MAXPW|MAXPWR)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(MAXP|MAXPW|MAXPWR) (\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
if float(STRING.split()[-1]) < 0 or float(STRING.split()[-1]) > 100:
daemon.log('WARNING value out of range: ' +
string+' from connection '+self.name)
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(PMAN|PMANU|PMANUA|PMANUAL)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(PMAN|PMANU|PMANUA|PMANUAL) ?(\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
if float(STRING.split()[-1]) < 0 or float(STRING.split()[-1]) > 100:
daemon.log('WARNING value out of range: ' +
string+' from connection '+self.name)
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:RAMP\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(RAT|RATE) ?(\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
if float(STRING.split()[-1]) < 0 or float(STRING.split()[-1]) > 100:
daemon.log('WARNING value out of range: ' +
string+' from connection '+self.name)
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(RAT|RATE)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(STAR|START|EXIT|SAVE)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(MOD|MODE)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(MOD|MODE) (P--|PI-|PID)')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(DELT|DELTA|DELTAP)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(DELT|DELTA|DELTAP) ?(\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
if float(STRING.split()[-1]) < 0 or float(STRING.split()[-1]) > 100:
daemon.log('WARNING value out of range: ' +
string+' from connection '+self.name)
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(TIM|TIME|TIMEO|TIMEOU|TIMEOUT)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(TIM|TIME|TIMEO|TIMEOU|TIMEOUT) ?(\d+?)?\.?(\d+?$)?$')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):([PID]GA|[PID]GAI|[PID]GAIN)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
regex = re.compile(r'LOOP [1-4]:(AUT|AUTO|AUTOT|AUTOTU|AUTOTUN|AUTOTUNE):(STAT|STATU|STATUS)\?')
if re.match(regex, STRING):
hw.messageAll(string, type='hw', keep=True, source=self.name)
break
if STRING[-1] == '?':
hw.messageAll(string, type='hw', keep=True, source=self.name)
else:
hw.messageAll(string, type='hw', keep=False, source=self.name)
break
class CryoConProtocol(SimpleProtocol):
_debug = False # Display all traffic for debug purposes
_tcp_keepidle = 1 # Faster detection of peer disconnection
_tcp_user_timeout = 3000 # Faster detection of peer disconnection
@catch
def __init__(self):
SimpleProtocol.__init__(self)
self.commands = [] # Queue of command sent to the device which will provide replies, each entry is a dict with keys "cmd","source"
self.name = 'hw'
self.type = 'hw'
self.status_commands = ["input? a,b,c,d;:cont?",
":loop 1:err?;rang?;type?;load?;outp?;htrr?;sour?;setp?;ramp?;rate?;pman?;",
":loop 2:err?;rang?;type?;load?;outp?;htrr?;sour?;setp?;ramp?;rate?;pman?;",
":loop 3:err?;rang?;type?;load?;outp?;htrr?;sour?;setp?;ramp?;rate?;pman?;",
":loop 4:err?;rang?;type?;load?;outp?;htrr?;sour?;setp?;ramp?;rate?;pman?;"]
@catch
def connectionMade(self):
self.object['hw_connected'] = 1
self.name = 'hw'
self.type = 'hw'
SimpleProtocol.connectionMade(self)
# make sure the units are Celsius
self.message('input a,b,c,d:units c;:loop 1:load 50;:loop 2:load 0;')
@catch
def connectionLost(self, reason):
self.object['hw_connected'] = 0
SimpleProtocol.connectionLost(self, reason)
self.object['status'] = '----'
@catch
def processMessage(self, string):
# Process the device reply
if self._debug:
print ("hw cc > %s" % string)
obj = self.object # Object holding the state
daemon = obj['daemon']
obj['hw_connected'] = 1
pwrfactor1 = {'HI': 1., 'MID': 0.1, 'LOW': 0.01}
pwrfactor2 = {'HI': 0.5, 'LOW': 0.05}
if len(self.commands) and string != "\r":
if self._debug:
print ("last command which expects reply was:", self.commands[0]['cmd'])
print ("received reply:", string)
if self.commands[0]['cmd'] in self.status_commands:
if len(string) and 'NAK' not in string:
# reply to parse looks like this:
# 20.806670;20.800480;20.896670;20.853670;--Htr OK--;HI ;MAN ;50;0.000000; 0%;--Htr OK--;LOW;MAN ;50;0.000000; 0%
# values for channel a;b;c;d (....... means not connected)
sstring = string.split(';')
if self.commands[0]['cmd'] == self.status_commands[0]:
status = ''
channel = ['temperatureA', 'temperatureB',
'temperatureC', 'temperatureD']
# temperatures
for s in range(4):
try:
sstring[s] = float(sstring[s])
status = status + '1'
self.object[channel[s]] = sstring[s]
except ValueError:
status = status + '0'
self.object[channel[s]] = np.nan
self.object['control'] = sstring[4]
self.object['status'] = status
else:
nn = self.status_commands.index(self.commands[0]['cmd'])
self.object['htr_status' + str(nn)] = sstring[0].replace(' ', '-')
self.object['range' + str(nn)] = sstring[1].replace(' ', '')
self.object['ctrl_type' + str(nn)] = sstring[2].replace(' ', '')
if nn == 1:
self.object['load'+str(nn)] = float(sstring[3])
self.object['pwr_set'+str(nn)] = float(sstring[4])*pwrfactor1[self.object['range'+str(nn)]]*self.object['load'+str(nn)]/100.
self.object['pwr_actual'+str(nn)] = float(sstring[5].replace('%', ''))*pwrfactor1[self.object['range'+str(nn)]]*self.object['load'+str(nn)]/100.
elif nn == 2:
self.object['load'+str(nn)] = float(sstring[3])
self.object['pwr_set'+str(nn)] = float(sstring[4])*pwrfactor2[self.object['range'+str(nn)]]*self.object['load'+str(nn)]/100.
self.object['pwr_actual'+str(nn)] = float(sstring[5].replace('%', ''))*pwrfactor2[self.object['range'+str(nn)]]*self.object['load'+str(nn)]/100.
else:
self.object['load'+str(nn)] = sstring[3]
self.object['pwr_set'+str(nn)] = sstring[4]+'%'
self.object['pwr_actual' + str(nn)] = sstring[5].replace(' ', '')
self.object['source'+str(nn)] = sstring[6]
self.object['set_point' + str(nn)] = float(sstring[7][:-2])
self.object['ramp'+str(nn)] = sstring[8]
self.object['rate'+str(nn)] = float(sstring[9])
self.object['pwr_man'+str(nn)] = float(sstring[10])
else:
# not recognized command, just pass the output
daemon.messageAll(string, self.commands[0]['source'])
self.commands.pop(0)
@catch
def update(self):
if self._debug:
print ('self.commands',self.commands)
# Request the hardware state from the device
if len(self.commands):
SimpleProtocol.message(self, self.commands[0]['cmd'])
if not self.commands[0]['keep']:
self.commands.pop(0)
else:
for k in self.status_commands:
self.commands.append({'cmd': k, 'source': 'itself', 'keep': True})
@catch
def message(self, string, keep=False, source='itself'):
"""
Send the message to the controller. If keep=True, expect reply
"""
self.commands.append({'cmd': string, 'source': source, 'keep': keep})
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] arg")
parser.add_option('-H', '--hw-host', help='Hardware host to connect',
action='store', dest='hw_host', default='192.168.1.5')
parser.add_option('-P', '--hw-port', help='Hardware port to connect',
action='store', dest='hw_port', type='int', default=5000)
parser.add_option('-p', '--port', help='Daemon port',
action='store', dest='port', type='int', default=7024)
parser.add_option('-n', '--name', help='Daemon name',
action='store', dest='name', default='cryo-con')
parser.add_option("-D", '--debug', help='Debug mode',
action="store_true", dest="debug")
parser.add_option("-S", '--simulator', help='Simulator mode',
action="store_true", dest="simulator")
(options, args) = parser.parse_args()
# Object holding actual state and work logic.
# May be anything that will be passed by reference - list, dict, object etc
obj = {'hw_connected': 0,
'status': '----', 'temperatureA': 0, 'temperatureB': 0, 'temperatureC': 0, 'temperatureD': 0, 'control':'-',
'htr_status1': '-', 'range1': '-', 'ctrl_type1': '-', 'pwr_set1': 0, 'pwr_actual1': 0, 'load1': 0, 'source1': '-', 'set_point1': np.nan, 'ramp1': '-', 'rate1': np.nan, 'pwr_man1': np.nan,
'htr_status2': '-', 'range2': '-', 'ctrl_type2': '-', 'pwr_set2': 0, 'pwr_actual2': 0, 'load2': 0, 'source2': '-', 'set_point2': np.nan, 'ramp2': '-', 'rate2': np.nan,'pwr_man2': np.nan,
'htr_status3': '-', 'range3': '-', 'ctrl_type3': '-', 'pwr_set3': 0, 'pwr_actual3': 0, 'load3': 0, 'source3': '-', 'set_point3': np.nan, 'ramp3': '-', 'rate3': np.nan,'pwr_man3': np.nan,
'htr_status4': '-', 'range4': '-', 'ctrl_type4': '-', 'pwr_set4': 0, 'pwr_actual4': 0, 'load4': 0, 'source4': '-', 'set_point4': np.nan, 'ramp4': '-', 'rate4': np.nan,'pwr_man4': np.nan}
# Factories for daemon and hardware connections
# We need two different factories as the protocols are different
daemon = SimpleFactory(DaemonProtocol, obj)
hw = SimpleFactory(CryoConProtocol, obj)
daemon.name = options.name
obj['daemon'] = daemon
obj['hw'] = hw
if options.debug:
daemon._protocol._debug = True
hw._protocol._debug = True
if options.simulator:
daemon._protocol._simulator = True
# Incoming connections
daemon.listen(options.port)
# Outgoing connection
hw.connect(options.hw_host, options.hw_port)
daemon._reactor.run()