-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcoldSMASwitchServer.py
383 lines (331 loc) · 12.9 KB
/
coldSMASwitchServer.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
379
380
381
382
383
# Copyright (C) 2011 Ted White
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### BEGIN NODE INFO
[info]
name = Cold Switch Server
version = 1.1
description =
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
"""
from labrad.types import Value
from labrad.devices import DeviceServer, DeviceWrapper
from labrad.server import LabradServer, setting
from labrad.errors import Error
import labrad.units as units
from twisted.internet.defer import inlineCallbacks, returnValue
from labrad import util
class ColdSwitchWrapper(DeviceWrapper):
@inlineCallbacks
def connect(self, server, port, oldState):
"""Connect to a cold switch board."""
print 'connecting to "%s" on port "%s"...' % (server.name, port),
self.state = oldState
self.setTrace = []
self.resetTrace = []
self.server = server
self.ctx = server.context()
self.port = port
p = self.packet()
p.open(port)
p.baudrate(1200L)
p.stopbits(1L)
p.bytesize(8L)
p.parity('N')
p.read() # clear out the read buffer
p.timeout(TIMEOUT)
yield p.send()
self.changeAppliedVoltage(oldState[3])
print 'done.'
def packet(self):
"""Create a packet in our private context."""
return self.server.packet(context=self.ctx)
def shutdown(self):
"""Disconnect from the serial port when we shut down."""
return self.packet().close().send()
@inlineCallbacks
def write(self, code, index = 0):
"""Write a data value to the cold switch."""
p = self.packet()
p.write(code)
yield p.send()
@inlineCallbacks
def setPulse(self):
"""Send a set command to the cold switch and check
the current output to see that it set"""
output = ''
self.setTrace = []
p = self.packet()
p.write('S')
p.read_line()
ans = yield p.send()
output = ans.read_line
for s in range(len(output)):
current = 4.0*ord(output[s])*(5.0/4095.0)/10.0
self.setTrace.append(current)
@inlineCallbacks
def resetPulse(self):
"""Send a reset command to the cold switch and check
the current output to see that it reset"""
output = ''
self.resetTrace = []
p = self.packet()
p.write('R')
p.read_line()
ans = yield p.send()
output = ans.read_line
for s in range(len(output)):
current = 4.0*ord(output[s])*(5.0/4095.0)/10.0
self.resetTrace.append(current)
@inlineCallbacks
def changeAppliedVoltage(self, voltage):
"""Change the voltage applied during set or reset pulse"""
voltValues = {'0':'0','1':'1','2':'2','3':'3',
'4':'4','5':'5','6':'6','7':'7',
'8':'8','9':'9','10':':','11':';',
'12':'<','13':'=','14':'>','15':'?'}
yield self.write(voltValues[str(voltage)])
self.state[3] = str(voltage)
returnValue(voltValues[str(voltage)])
@inlineCallbacks
def setFirstSwitchChannel(self, channel, commands):
"""change the channel set on the first switch"""
chan = str(channel)
if self.state[0]!=chan and chan!='0':
if self.state[0]!='0':
reschan = commands[self.state[0]]
yield self.write(reschan)
yield self.resetPulse()
yield util.wakeupCall(1)
setchan = commands[chan]
yield self.write(setchan)
yield self.setPulse()
yield util.wakeupCall(1)
self.state[0] = chan
if self.state[0]!=chan and chan =='0':
reschan = commands[self.state[0]]
yield self.write(reschan)
yield self.resetPulse()
self.state[0] = chan
returnValue(chan)
@inlineCallbacks
def setSecondSwitchChannel(self, channel, commands):
"""change the channel set on the second switch"""
chan = str(channel)
if self.state[1]!=chan and chan!='0':
if self.state[1]!='0':
reschan = commands[self.state[1]]
yield self.write(reschan)
yield self.resetPulse()
yield util.wakeupCall(1)
setchan = commands[chan]
yield self.write(setchan)
yield self.setPulse()
yield util.wakeupCall(1)
self.state[1] = chan
if self.state[1]!=chan and chan=='0':
reschan = commands[self.state[1]]
yield self.write(reschan)
yield self.resetPulse()
self.state[1] = chan
returnValue(chan)
@inlineCallbacks
def setThirdSwitchChannel(self, channel, commands):
"""change the channel set on the third switch"""
chan = str(channel)
if self.state[2]!=chan and chan!='0':
if self.state[2]!='0':
reschan = commands[self.state[2]]
yield self.write(reschan)
yield self.resetPulse()
yield util.wakeupCall(1)
setchan = commands[chan]
yield self.write(setchan)
yield self.setPulse()
yield util.wakeupCall(1)
self.state[2] = chan
if self.state[2]!=chan and chan=='0':
reschan = commands[self.state[2]]
yield self.write(reschan)
yield self.resetPulse()
self.state[2] = chan
returnValue(chan)
@inlineCallbacks
def getSetTrace(self):
"""Check the current values of the last set or reset pulse"""
returnValue(self.setTrace)
@inlineCallbacks
def getResetTrace(self):
"""Check the current values of the last set or reset pulse"""
returnValue(self.resetTrace)
@inlineCallbacks
def getSwitchState(self):
switchState = self.state
returnValue(switchState)
@inlineCallbacks
def masterReset(self, switch, commands):
for command in commands:
reschan = commands[command]
yield self.write(reschan)
yield self.resetPulse()
yield util.wakeupCall(2)
self.state[switch] = '0'
@inlineCallbacks
def updateRegistry(self, reg):
yield reg.cd(['', 'Servers', 'Cold Switch', 'Links'], True)
p = reg.packet()
name = self.name.split( )
key = name[0]
ss = name[0]+' '+name[1]+' '+name[2]
p.set(key,(ss,self.port,self.state))
yield p.send()
class ColdSwitchServer(DeviceServer):
deviceName = 'Cold Switch Server'
name = 'Cold Switch Server'
deviceWrapper = ColdSwitchWrapper
@inlineCallbacks
def initServer(self):
print 'loading config info...',
self.reg = self.client.registry()
yield self.loadConfigInfo()
print 'done.'
yield DeviceServer.initServer(self)
@inlineCallbacks
def loadConfigInfo(self):
"""Load configuration information from the registry."""
reg = self.reg
yield reg.cd(['', 'Servers', 'Cold Switch', 'Links'], True)
dirs, keys = yield reg.dir()
p = reg.packet()
for k in keys:
p.get(k, key=k)
ans = yield p.send()
self.serialLinks = dict((k, ans[k]) for k in keys)
@inlineCallbacks
def findDevices(self):
"""Find available devices from list stored in the registry."""
devs = []
for name, (serServer, port, oldState) in self.serialLinks.items():
if serServer not in self.client.servers:
continue
server = self.client[serServer]
ports = yield server.list_serial_ports()
if port not in ports:
continue
devName = '%s - %s' % (serServer, port)
devs += [(devName, (server, port, oldState))]
returnValue(devs)
@setting(456, 'change voltage', data='w', returns='s')
def change_voltage(self, c, data):
"""Changes the voltage applied to set or reset the switch"""
dev = self.selectedDevice(c)
reg = self.client.registry
voltage = yield dev.changeAppliedVoltage(data)
yield dev.updateRegistry(reg)
returnValue(voltage)
@setting(457, 'set switch1', data='w', returns='s')
def set_switch1(self, c, data):
"""Changes which port on switch one is connected to the output"""
commandList =[{'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f'},
{'1':'g','2':'h','3':'i','4':'j','5':'k','6':'l'},
{'1':'m','2':'n','3':'o','4':'p','5':'q','6':'r'}]
print 'cs1'
dev = self.selectedDevice(c)
print 'cs2'
reg = self.client.registry
print 'cs3'
if dev.state[0]== 'null':
print 'cs4'
returnValue('null')
print 'cs5'
else:
print 'cs6'
channel = yield dev.setFirstSwitchChannel(data, commandList[0])
print 'cs7'
yield dev.updateRegistry(reg)
print 'cs8'
returnValue(channel)
print 'cs9'
@setting(458, 'set switch2', data='w', returns='s')
def set_switch2(self, c, data):
"""Changes which port on switch two is connected to the output"""
commandList =[{'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f'},
{'1':'g','2':'h','3':'i','4':'j','5':'k','6':'l'},
{'1':'m','2':'n','3':'o','4':'p','5':'q','6':'r'}]
dev = self.selectedDevice(c)
reg = self.client.registry
if dev.state[1]== 'null':
returnValue('null')
else:
channel = yield dev.setSecondSwitchChannel(data, commandList[1])
yield dev.updateRegistry(reg)
returnValue(channel)
@setting(459, 'set switch3', data='w', returns='s')
def set_switch3(self, c, data):
"""Changes which port on switch three is connected to the output"""
commandList =[{'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f'},
{'1':'g','2':'h','3':'i','4':'j','5':'k','6':'l'},
{'1':'m','2':'n','3':'o','4':'p','5':'q','6':'r'}]
dev = self.selectedDevice(c)
reg = self.client.registry
if dev.state[2]== 'null':
returnValue('null')
else:
channel = yield dev.setThirdSwitchChannel(data, commandList[2])
yield dev.updateRegistry(reg)
returnValue(channel)
@setting(461, 'get set trace')
def get_set_trace(self, c):
"""Returns a trace of the current going through the solenoid for the last switch set command"""
dev = self.selectedDevice(c)
currents = yield dev.getSetTrace()
returnValue(currents)
@setting(462, 'get reset trace')
def get_reset_trace(self, c):
"""Returns a trace of the current going through the solenoid for the last switch reset command"""
dev = self.selectedDevice(c)
currents = yield dev.getResetTrace()
returnValue(currents)
@setting(466, 'get switch state', returns = '*s')
def get_swtich_state(self, c):
"""Returns the current channel for each switch and the current applied voltage"""
dev = self.selectedDevice(c)
state = yield dev.getSwitchState()
returnValue(state)
@setting(467, 'master reset', data = 'w')
def master_reset(self, c, data):
"""Ensures that all channels for a given switch are disconnected"""
commandList =[{'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f'},
{'1':'g','2':'h','3':'i','4':'j','5':'k','6':'l'},
{'1':'m','2':'n','3':'o','4':'p','5':'q','6':'r'}]
switch = data-1
dev = self.selectedDevice(c)
reg = self.client.registry
if dev.state[switch]== 'null':
returnValue('null')
else:
yield dev.masterReset(switch, commandList[switch])
yield dev.updateRegistry(reg)
TIMEOUT = 1*units.s
__server__ = ColdSwitchServer()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)