-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfungible_board_class.py
560 lines (474 loc) · 22.5 KB
/
fungible_board_class.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import sys
import os
import serial
import mapper
import string
import time
from multiprocessing import Process, Queue
from threading import Thread
from Queue import Empty
import time
sensorinfo = [(1, '0S1', 'LeftBase', 'A', 'ON'),
(1, '1S1', 'LeftBase', 'B', 'ON'),
(1, '2S1', 'LeftBase', 'C', 'ON'),
(1, '3S1', 'LeftWingBend', '1', 'ON'),
(1, '4S1', 'LeftWingBend', '2', 'ON'),
(1, '5S1', 'LeftWingBend', '3', 'ON'),
(1, '6S1', 'LeftWingBend', '4', 'ON'),
(1, '7S1', 'Horizontal', '1', 'ON'),
(1, '0D1', 'LeftBase', 'A-B', 'ON'),
(1, '1D1', 'LeftBase', 'A-C', 'ON'),
(1, '2D1', 'instr1', 'NA', 'ON'),
(1, '3D1', 'instr2', 'NA', 'ON'),
(1, '4D1', 'N-A1', 'NA', 'ON'),
(1, '5D1', 'N-A2', 'NA', 'ON'),
(1, '6D1', 'N-A3', 'NA', 'ON'),
(1, '7D1', 'N-A4', 'NA', 'ON'),
(1, '0S0', 'LeftBase', 'A', 'OFF'),
(1, '1S0', 'LeftBase', 'B', 'OFF'),
(1, '2S0', 'LeftBase', 'C', 'OFF'),
(1, '3S0', 'LeftWingBend', '1', 'OFF'),
(1, '4S0', 'LeftWingBend', '2', 'OFF'),
(1, '5S0', 'LeftWingBend', '3', 'OFF'),
(1, '6S0', 'LeftWingBend', '4', 'OFF'),
(1, '7S0', 'Horizontal', '1', 'OFF'),
(1, '0D0', 'LeftBase', 'A-B', 'OFF'),
(1, '1D0', 'LeftBase', 'A-C', 'OFF'),
(1, '2D0', 'instr1', 'NA', 'OFF'),
(1, '3D0', 'instr2', 'NA', 'OFF'),
(1, '4D0', 'N-A1', 'NA', 'OFF'),
(1, '5D0', 'N-A2', 'NA', 'OFF'),
(1, '6D0', 'N-A3', 'NA', 'OFF'),
(1, '7D0', 'N-A4', 'NA', 'OFF'),
(2, '0S1', 'LeftMid', 'A', 'ON'),
(2, '1S1', 'LeftMid', 'B', 'ON'),
(2, '2S1', 'LeftMid', 'C', 'ON'),
(2, '3S1', 'LeftTip', 'A', 'ON'),
(2, '4S1', 'LeftTip', 'B', 'ON'),
(2, '5S1', 'Horizontal', '2', 'ON'),
(2, '6S1', 'Horizontal', '3', 'ON'),
(2, '7S1', 'Horizontal', '4', 'ON'),
(2, '0D1', 'LeftMid', 'A-B', 'ON'),
(2, '1D1', 'LeftMid', 'A-C', 'ON'),
(2, '2D1', 'instr1', 'NA', 'ON'),
(2, '3D1', 'instr2', 'NA', 'ON'),
(2, '4D1', 'LeftTip-HorizontalBend', 'NA', 'ON'),
(2, '5D1', 'LeftTip', 'B-A', 'ON'),
(2, '6D1', 'LeftTip-LeftMid', 'NA', 'ON'),
(2, '7D1', 'Horizontal_Bend', '3-4', 'ON'),
(2, '0S0', 'LeftMid', 'A', 'OFF'),
(2, '1S0', 'LeftMid', 'B', 'OFF'),
(2, '2S0', 'LeftMid', 'C', 'OFF'),
(2, '3S0', 'LeftTip', 'A', 'OFF'),
(2, '4S0', 'LeftTip', 'B', 'OFF'),
(2, '5S0', 'Horizontal', '2', 'OFF'),
(2, '6S0', 'Horizontal', '3', 'OFF'),
(2, '7S0', 'Horizontal', '4', 'OFF'),
(2, '0D0', 'LeftMid', 'A-B', 'OFF'),
(2, '1D0', 'LeftMid', 'A-C', 'OFF'),
(2, '2D0', 'instr1', 'NA', 'OFF'),
(2, '3D0', 'instr2', 'NA', 'OFF'),
(2, '4D0', 'LeftTip-HorizontalBend', 'NA', 'OFF'),
(2, '5D0', 'LeftTip', 'B-A', 'OFF'),
(2, '6D0', 'LeftTip-LeftMid', 'NA', 'OFF'),
(2, '7D0', 'Horizontal_Bend', '3-4', 'OFF'),
(3, '0S1', 'RightBase', 'A', 'ON'),
(3, '1S1', 'RightBase', 'B', 'ON'),
(3, '2S1', 'RightBase', 'C', 'ON'),
(3, '3S1', 'RightWingBend', '1', 'ON'),
(3, '4S1', 'RightWingBend', '2', 'ON'),
(3, '5S1', 'RightWingBend', '3', 'ON'),
(3, '6S1', 'RightWingBend', '4', 'ON'),
(3, '7S1', 'VerticalBend', '2', 'ON'),
(3, '0D1', 'RightBase', 'A-B', 'ON'),
(3, '1D1', 'RightBase', 'A-C', 'ON'),
(3, '2D1', 'RightWingBend', '2-3', 'ON'),
(3, '3D1', 'RightWingBend', '1-2', 'ON'),
(3, '4D1', 'RightBase-RightWingBend', 'NA', 'ON'),
(3, '5D1', 'RightWingBend-VerticalBend', 'NA', 'ON'),
(3, '6D1', 'instr1', 'NA', 'ON'),
(3, '7D1', 'instr2', 'NA', 'ON'),
(3, '0S0', 'RightBase', 'A', 'OFF'),
(3, '1S0', 'RightBase', 'B', 'OFF'),
(3, '2S0', 'RightBase', 'C', 'OFF'),
(3, '3S0', 'RightWingBend', '1', 'OFF'),
(3, '4S0', 'RightWingBend', '2', 'OFF'),
(3, '5S0', 'RightWingBend', '3', 'OFF'),
(3, '6S0', 'RightWingBend', '4', 'OFF'),
(3, '7S0', 'VerticalBend', '2', 'OFF'),
(3, '0D0', 'RightBase', 'A-B', 'OFF'),
(3, '1D0', 'RightBase', 'A-C', 'OFF'),
(3, '2D0', 'RightWingBend', '2-3', 'OFF'),
(3, '3D0', 'RightWingBend', '1-2', 'OFF'),
(3, '4D0', 'RightBase-RightWingBend', 'NA', 'OFF'),
(3, '5D0', 'RightWingBend-VerticalBend', 'NA', 'OFF'),
(3, '6D0', 'instr1', 'NA', 'OFF'),
(3, '7D0', 'instr2', 'NA', 'OFF'),
(1, '0S2', 'LeftBase', 'A', 'COMP'),
(1, '1S2', 'LeftBase', 'B', 'COMP'),
(1, '2S2', 'LeftBase', 'C', 'COMP'),
(1, '3S2', 'LeftWingBend', '1', 'COMP'),
(1, '4S2', 'LeftWingBend', '2', 'COMP'),
(1, '5S2', 'LeftWingBend', '3', 'COMP'),
(1, '6S2', 'LeftWingBend', '4', 'COMP'),
(1, '7S2', 'Horizontal', '1', 'COMP'),
(1, '0D2', 'LeftBase', 'A-B', 'COMP'),
(1, '1D2', 'LeftBase', 'A-C', 'COMP'),
(1, '2D2', 'instr1', 'NA', 'COMP'),
(1, '3D2', 'instr2', 'NA', 'COMP'),
(1, '4D2', 'N-A1', 'NA', 'COMP'),
(1, '5D2', 'N-A2', 'NA', 'COMP'),
(1, '6D2', 'N-A3', 'NA', 'COMP'),
(1, '7D2', 'N-A4', 'NA', 'COMP'),
(2, '0S2', 'LeftMid', 'A', 'COMP'),
(2, '1S2', 'LeftMid', 'B', 'COMP'),
(2, '2S2', 'LeftMid', 'C', 'COMP'),
(2, '3S2', 'LeftTip', 'A', 'COMP'),
(2, '4S2', 'LeftTip', 'B', 'COMP'),
(2, '5S2', 'Horizontal', '2', 'COMP'),
(2, '6S2', 'Horizontal', '3', 'COMP'),
(2, '7S2', 'Horizontal', '4', 'COMP'),
(2, '0D2', 'LeftMid', 'A-B', 'COMP'),
(2, '1D2', 'LeftMid', 'A-C', 'COMP'),
(2, '2D2', 'instr1', 'NA', 'COMP'),
(2, '3D2', 'instr2', 'NA', 'COMP'),
(2, '4D2', 'LeftTip-HorizontalBend', 'NA', 'COMP'),
(2, '5D2', 'LeftTip', 'B-A', 'COMP'),
(2, '6D2', 'LeftTip-LeftMid', 'NA', 'COMP'),
(2, '7D2', 'Horizontal_Bend', '3-4', 'COMP'),
(3, '0S2', 'RightBase', 'A', 'COMP'),
(3, '1S2', 'RightBase', 'B', 'COMP'),
(3, '2S2', 'RightBase', 'C', 'COMP'),
(3, '3S2', 'RightWingBend', '1', 'COMP'),
(3, '4S2', 'RightWingBend', '2', 'COMP'),
(3, '5S2', 'RightWingBend', '3', 'COMP'),
(3, '6S2', 'RightWingBend', '4', 'COMP'),
(3, '7S2', 'VerticalBend', '2', 'COMP'),
(3, '0D2', 'RightBase', 'A-B', 'COMP'),
(3, '1D2', 'RightBase', 'A-C', 'COMP'),
(3, '2D2', 'RightWingBend', '2-3', 'COMP'),
(3, '3D2', 'RightWingBend', '1-2', 'COMP'),
(3, '4D2', 'RightBase-RightWingBend', 'NA', 'COMP'),
(3, '5D2', 'RightWingBend-VerticalBend', 'NA', 'COMP'),
(3, '6D2', 'instr1', 'NA', 'COMP'),
(3, '7D2', 'instr2', 'NA', 'COMP')]
class Fungible_Node:
def __init__(self,port_num,baud,t_out, m_inst):
self.quit=0
print "Starting Fungible Initialization"
try:
self.OS = os.name
self.inBuffer=""
self.port_num=port_num
print self.port_num
self.baud=baud
print self.baud
self.t_out=t_out
print self.t_out
self.open_connection()
print ("Connection opened",self.ser)
self.temp_buff=""
# self.q = Queue()
# print self.q
#try:
# if self.OS == 'nt':
# print __name__
# self.p = Thread(target=self.f, args=(self.q,))
# elif self.OS == 'posix':
#self.p = Process(target=self.f, args=(self.q,))
#print self.pNoneType
#except:
#print ("Thread couldn't initialize")
#try:
#self.p.start()
#except:
# print("Thread couldn't start")
# raise
self.addr=0
print ("Try to get i2c addr")
while (self.addr<1):
self.get_i2c_address()
print ("I2C_address:",self.addr)
self.pairs={}
item_num=0
self.number_of_sigs=0
self.sig_names={}
self.info_index={}
self.state=0
print ("Try to get sig info")
while (len(self.sig_names)<1):
self.get_signal_info()
time.sleep(1)
print("sig names attained: ",self.sig_names)
self.serial_sigs={}
self.old_s_sigs={}
self.mapper_sigs={}
self.create_mapper_signals(m_inst)
self.s_write("Sc 5\r")
time.sleep(1)
self.s_write("!e\r")
#self.s_read(100)
print ("Try to get all serial data")
while (len(self.serial_sigs)<self.number_of_sigs):
self.s_write("!e\r")
self.get_serial_data()
time.sleep(1)
print ("Got all serial data")
self.old_s_sigs=self.serial_sigs.copy()
#except:
# print ("well the first part didn't work")
#try:
self.update_mapper_signals()
self.poll(m_inst)
except:
print ("Funginble Node initialization failed!")
raise
self.close_nicely()
def close_connection(self):
try:
self.s_write("!d\r")
#self.ser.writelines("!d\r")
self.ser.close()
except:
print ("Could not close port: ", self.port_num)
def s_write(self,message):
try:
self.ser.writelines(message)
except:
print ("Could not write to port:", self.port_num)
def s_read(self,num_lines):
try:
if((self.ser.inWaiting()>0)):
# print ("num in waiting", self.ser.inWaiting())
# self.inBuffer=self.ser.readlines(num_lines)
# print ("type in waiting: ", type(self.ser.inWaiting()))
self.inBuffer=self.ser.read(int(self.ser.inWaiting()))
self.inBuffer=self.temp_buff+self.inBuffer
self.temp_buff=""
buff_length=len(self.inBuffer)
d_indx=self.inBuffer.rfind("\n")
if (d_indx>-1): #there is a carriage return
try:
self.temp_buff=self.inBuffer[d_indx+1:buff_length-1]
except:
pass
self.inBuffer=self.inBuffer[0:d_indx]
self.inBuffer=self.inBuffer.split("\n")
else: #no carriage return, store partial buffer
self.temp_buff=self.inBuffer
self.inBuffer=""
return self.inBuffer
#output_buff=self.ser.readlines(num_lines)
#return output_buff
else:
# print("nothing in serial buffer")
return ""
except:
print ("Could not read from port:", self.port_num)
raise
return ""
# def close_thread(self):
# self.p.join()
def close_nicely(self):
self.quit=1
self.close_connection()
# self.close_thread()
def open_connection(self):
# print self.port_num
# print self.baud
try:
self.ser = serial.Serial(self.port_num,self.baud,timeout=1)
#print self.ser
except:
print ("Could not open port: " , self.port_num)
def get_i2c_address(self):
self.s_write("!d\r")
#self.ser.writelines("!d\r")
self.ser.flushInput()
time.sleep(1)
self.s_write("SI 0\r")
#self.ser.writelines("SI 0\r")
#s = self.ser.readlines(20)
s = self.s_read(200)
#print ("printing s, line 176", s)
try:
if len(s)>1:
for word in s:
if ("I2C Slave Address:" in word):
address = word.split("I2C Slave Address:")
self.addr=int(address[1])
return self.addr
except:
self.addr=0
print ("I2C address not found")
return self.addr
def get_signal_info(self):
self.s_write("!d\r")
self.s_write("Sc 5\r")
time.sleep(1)
self.s_write("SSR\r")
time.sleep(1)
self.ser.flushInput()
self.s_write("Sbc\r")
time.sleep(1)
#self.ser.writelines("!d\r")
#self.ser.writelines("Sbb\r")
# print "************Reading Serial Content after Sbb***********"
# s = self.s_read(60)
s = self.s_read(200)
#s = self.ser.readlines(100)
print "number of lines:"
print (len(s))
print "content"
print (s)
# self.pairs={}
item_num=0
# self.sig_names={}
# self.info_index={}
if len(s)>1:
for word in s:
# length_position =
if (word.find("Length of List of Printed signals:") != -1):
# print ("word at 214" ,word)
# index_colon = word.find(":")
#num_sig = word[index_colon+1:len(word)]
num_sig=word.split('Length of List of Printed signals:')
# if(num_sig[1].isdigit()):
self.number_of_sigs = int(num_sig[1])
print "Number of Signals:", self.number_of_sigs
elif (word.find("List of Printed signals:")!=-1):
#print ("word at 219 " ,word)
substr=word.split(':')
#print ("substr " ,substr)
lnames=substr[1].split()
print ("lnames " ,lnames)
for item in lnames:
sub_item=item.split()
self.pairs[item_num]=sub_item[0]
item_num=item_num+1
item_num=0
for item_num in self.pairs.keys():
list_num=0
for element in sensorinfo:
if ((self.pairs[item_num] in element[1]) & (self.addr == element[0])):
#print (item_num,"index gives",element[2] ,element[3], element[4])
self.sig_names[item_num]=str(element[0]) +'/'+ element[2] + '/' + element[3] +'/'+ element[4]
# allboards creates the mapper signal name and is also used to index information from sensorinfo
self.info_index[item_num]=list_num
#info_index directly gives the corresponding row number (i.e. index) of the (board_number, signal_number) pair from sensorinfo
#without having to refer through the 0S1, 1S1, 2S1, etc. notation
list_num=list_num+1
# print("sig_names",self.sig_names)
def update_mapper_signals(self):
#for sig_num in range(number_of_mapped_sigs):
#map_num=0
#print("signal value ",self.serial_sigs)
#print ("for board ", self.port_num)
# print ("mapper sigs",self.mapper_sigs)
for sig_num in range(self.number_of_sigs):
#print("sig number:",sig_num, " value:", self.serial_sigs[sig_num])
#print("old sig number:",sig_num, " value:", self.old_s_sigs[sig_num])
#print ("what sigs: ",sig_num)
if (self.serial_sigs[sig_num] == self.old_s_sigs[sig_num]):
#Only update data if it has a new value...no point sending the old one again
#print("signal is the same as before: ", sig_num)
# print ("not new updating signal: ", sig_num, "val: ", self.serial_sigs[sig_num])
continue
else:
#print ("what sigs: ",self.number_of_sigs)
# print ("updating signal: ", sig_num, "val: ", self.serial_sigs[sig_num])
self.mapper_sigs[sig_num].update(self.serial_sigs[sig_num])
#print("sig number:",sig_num, " value:", self.serial_sigs[sig_num])
#print("old sig number:",sig_num, " value:", self.old_s_sigs[sig_num])
# map_num=map_num+1
def create_mapper_signals(self,m_inst):
# print ("sig names.keys", self.sig_names.keys())
# print ("info index", self.info_index)
for sig in self.sig_names.keys():
#for sig in allboards[board].keys():
if (sensorinfo[self.info_index[sig]][1][1]=='D'):
max_size=255
elif (sensorinfo[self.info_index[sig]][1][1]=='S'):
max_size=128
# print "max size", max_size
self.mapper_sigs[sig] = m_inst.add_output(self.sig_names[sig],'i',"Volts",0,max_size)
print "Created Mapper Signals", self.sig_names[sig]
def get_serial_data(self):
machine = { (0, 8): 1,
(1, 8): 2,
(2, 8): 3,
(3, 8): 4,
(4, 0): 0}
lines=self.s_read(50)
#lines=self.ser.readlines(6)
#print ("length of serial buff: ", len(lines))
#print ("lines at 358: ", lines)
self.old_s_sigs=self.serial_sigs.copy()
for s in lines:
data = s.split()
#print "Data Coming In", data
L = len(data)
try:
self.state = machine[(self.state, L)]
except:
self.state = 0
#print ('data', data, 'state', self.state)
for i in range(8):
if self.state==1 and L==8:
try:
#print ("getting signalin state 1")
self.serial_sigs[i]=int(data[i])
#print ("sigs 2", self.serial_sigs[i])
except:
print ("Signal is beyond range (not evenly divisible by 8)")
#mapper_signals[active_board][i].update(int(data[i]))
#print "Active Signal Being Updates", mapper_signals[active_board][i]
#print "Data given to active board", data[i]
if self.state==2 and L==8:
try:
#print ("getting signalin state 2")
self.serial_sigs[i+8]=int(data[i])
# print ("sigs 2", self.serial_sigs[i+8])
except:
print ("Signal is beyond range (not evenly divisible by 8)")
# mapper_signals[active_board][i+8].update(int(data[i]))
#print "Active Signal Being Updates", mapper_signals[active_board][i]
#print "Data given to active board", data[i]
if self.state==3 and L==8:
try:
self.serial_sigs[i+16]=int(data[i])
except:
print ("Signal is beyond range (not evenly divisible by 8)")
# mapper_signals[active_board][i+16].update(int(data[i]))
if self.state==4 and L==8:
try:
self.serial_sigs[i+24]=int(data[i])
except:
print ("Signal is beyond range (not evenly divisible by 8)")
# mapper_signals[active_board][i+24].update(int(data[i]))
#return state
def f(self,q):
# def f(q):
import Tkinter
def tcall():
temp_str=tbox.get()
q.put(temp_str)
def onquit():
q.put("quit")
root.quit()
def ontimer():
print 'someshit'
# check the serial port
root.after(5, ontimer)
root = Tkinter.Tk()
quit_button = Tkinter.Button(root, text="Quit", command=lambda: onquit())
quit_button.pack()
#command_button = Tkinter.Button(root, text="Enter command for port: "+self.port_num, command=tcall)
command_button = Tkinter.Button(root, text="Enter command for port:", command=tcall)
command_button.pack()
tbox=Tkinter.Entry(root)
tbox.pack()
root.after(5, ontimer)
# root.mainloop()
def poll(self,m_inst):
m_inst.poll(0)