13 import sys, traceback, Ice
17 from thread
import start_new_thread, allocate_lock
31 print bcolors.OKBLUE+
"""
33 / ____/_ __/ /_ ___ _____/ ____/ /___ _ _____
34 / / / / / / __ \/ _ \/ ___/ / __/ / __ \ | / / _ \\
35 / /___/ /_/ / /_/ / __/ / / /_/ / / /_/ / |/ / __/
36 \____/\__, /_.___/\___/_/ \____/_/\____/|___/\___/
40 print bcolors.WARNING +
"Permission needed to open ports for gloves. If prompted, please type your sudo password below." + bcolors.ENDC
53 signal.signal(signal.SIGINT, signal_handler)
55 right_port =
'/dev/ttyUSB0'
57 right_ser = serial.Serial(right_port, 115200, timeout=1)
58 right_ser.write(
"1dw")
59 right_ser.write(
"1eu")
60 right_ser.write(
"1ds")
64 right_cal = [
"" for x
in range(33)]
66 glove_data_lock = allocate_lock()
83 properties.load(
"./default.generated.cfg")
84 properties.load(
"./default.cfg")
86 init_data = Ice.InitializationData()
87 init_data.properties = properties
89 ic = Ice.initialize(init_data)
91 Ice.loadSlice(
'-I%s %s -DMININTERFACE' % (Ice.getSliceDir(),
'CyberGloveInterface.ice'))
96 obj = ic.stringToProxy(
"IceStorm/TopicManager")
97 print(
"stringToProxy")
99 topicMan = IceStorm.TopicManagerPrx.checkedCast(obj)
101 topicName =
"CyberGloveValues"
105 topic = topicMan.retrieve(topicName);
106 except IceStorm.NoSuchTopic:
107 print(
"No such topic.")
110 topic = topicMan.create(topicName)
111 print(
"Try to create topic.")
113 except IceStorm.TopicExists:
116 topic = topicMan.retrieve(topicName)
119 pub = topic.getPublisher().ice_oneway();
120 glove = armarx.CyberGloveListenerInterfacePrx.uncheckedCast(pub);
127 raise RuntimeError(
"Invalid proxy")
133 traceback.print_exc()
138 Rcache = right_ser.read(61)
140 Rcache = Rcache[Rcache.index(
"S")+1:]
141 Rcache = Rcache + right_ser.read(61-len(Rcache))
159 glove_data_lock.acquire()
160 right_s = right_ser.read(61)
161 if len(right_s) != 0
and right_s[13] !=
"S":
162 print "Missed beginning."
164 right_s = right_s[right_s.index(
"S"):]
165 cut_len = 48 - len(right_s)
168 right_s = right_s + right_ser.read(cut_len)
173 if len(right_s) != 0
and show_stream:
175 sensorData = np.zeros(23)
179 timestamp = timestamp + right_s[n]
180 for n
in range(14, len(right_s)-1, 2):
183 dataReading = 256*ord(right_s[n]) + ord(right_s[n+1])
185 sensorData[i] = dataReading
187 print str(len(right_s)) +
" " + timestamp +
" " +
str(time.time())
188 values =
armarx.CyberGloveValues(name =
"gloveValues", time = timestamp, thumbCMC = sensorData[0], thumbMCP = sensorData[1], thumbIP = sensorData[2], thumbAbd = sensorData[3], indexMCP = sensorData[4], indexPIP = sensorData[5], indexDIP = sensorData[6], middleMCP = sensorData[7], middlePIP = sensorData[8], middleDIP = sensorData[9], middleAbd = sensorData[10], ringMCP = sensorData[11], ringPIP = sensorData[12], ringDIP = sensorData[13], ringAbd = sensorData[14], littleMCP = sensorData[15], littlePIP = sensorData[16], littleDIP = sensorData[17], littleAbd = sensorData[18], palmArch = sensorData[19], wristFlex = sensorData[20], wristAbd = sensorData[21])
189 glove.reportGloveValues(values)
191 glove_data_lock.release()
198 global show_cal_stream
200 c = raw_input(
"Choose raw sensor value (s), constant raw stream (r), glove calibration (c), stop stream (d) or quit (q)")
203 glove_data_lock.acquire()
208 for n
in range(14, len(right_s)-1, 2):
209 print 256*ord(right_s[n]) + ord(right_s[n+1])
212 glove_data_lock.release()
216 glove_data_lock.acquire()
218 glove_data_lock.release()
221 right_cal[n] = act_s[n]
224 for n
in range(14, len(act_s)-1, 2):
227 right_cal[(n-14)/2 + 14] = (256*ord(act_s[n]) + ord(act_s[n+1]) - calib_fac.offset[(n-14)/2])*calib_fac.gain[(n-14)/2]
229 print right_cal[(n-14)/2 + 14]
240 raw_input(
"Flat hand straight fingers pose.")
241 glove_data_lock.acquire()
242 for finger
in range(5):
243 for joint
in range(4):
257 sensorNo = 7 + (finger - 2)*4 + joint
258 calNo = finger*4 + joint
259 sensorNo = sensorNo*2 + 14
260 calib_fac.offset[calNo] = 256*ord(right_s[sensorNo]) + ord(right_s[sensorNo + 1])
261 print "Finger" +
str(finger)
262 print "Joint" +
str(joint)
265 print calib_fac.offset[calNo]
266 calib_fac.offset[20] = 256*ord(right_s[52]) + ord(right_s[53])
267 glove_data_lock.release()
270 sensorNo = calNo*2 + 14
272 raw_input(
"Thumb proximal joint bent 37 degrees.")
273 glove_data_lock.acquire()
274 calib_fac.gain[calNo] = (fingerAngle*PI/180)/(256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1]) - calib_fac.offset[calNo])
275 print 256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1])
276 glove_data_lock.release()
278 print calib_fac.gain[calNo]
281 sensorNo = calNo*2 + 14
283 raw_input(
"Thumb distal joint bent 76 degrees.")
284 glove_data_lock.acquire()
285 calib_fac.gain[calNo] = (fingerAngle*PI/180)/(256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1]) - calib_fac.offset[calNo])
286 print 256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1])
287 glove_data_lock.release()
288 print calib_fac.gain[calNo]
290 for finger
in range(1,5):
291 for joint
in range(3):
295 sensorNo = calNo*2 + 14
297 calNo = finger*4 + joint
298 sensorNo = (7 + (finger - 2)*4 + joint)*2 + 14
299 raw_input(
"Finger " +
str(finger) +
", joint " +
str(joint) +
" bent " +
str(fingerAngle) +
" degrees.")
300 glove_data_lock.acquire()
301 calib_fac.gain[calNo] = (fingerAngle*PI/180)/(256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1]) - calib_fac.offset[calNo])
302 print 256*ord(right_s[sensorNo]) + ord(right_s[sensorNo+1])
303 glove_data_lock.release()
304 print calib_fac.gain[calNo]
306 raw_input(
"Finger abducted with 25, 20 and 21 degrees.")
307 glove_data_lock.acquire()
308 calib_fac.gain[11] = (25*PI/180)/(256*ord(right_s[34]) + ord(right_s[35]) - calib_fac.offset[11])
309 print "Middle: " +
str(256*ord(right_s[34]) + ord(right_s[35]))
310 print calib_fac.gain[11]
311 calib_fac.gain[15] = (20*PI/180)/(256*ord(right_s[42]) + ord(right_s[43]) - calib_fac.offset[15])
312 print "Ring: " +
str(256*ord(right_s[42]) + ord(right_s[43]))
313 print calib_fac.gain[15]
314 calib_fac.gain[19] = (21*PI/180)/(256*ord(right_s[50]) + ord(right_s[51]) - calib_fac.offset[19])
315 print "Little: " +
str(256*ord(right_s[50]) + ord(right_s[51]))
316 print calib_fac.gain[19]
317 glove_data_lock.release()
319 raw_input(
"Thumb metacarpal extension 90 degrees.")
320 glove_data_lock.acquire()
321 calib_fac.gain[0] = (90*PI/180)/(256*ord(right_s[14]) + ord(right_s[15]) - calib_fac.offset[0])
322 glove_data_lock.release()
324 raw_input(
"Thumb fully abducted.")
325 glove_data_lock.acquire()
326 calib_fac.offset[3] = 256*ord(right_s[20]) + ord(right_s[21])
327 glove_data_lock.release()
329 raw_input(
"Thumb adducted 76 degrees.")
330 glove_data_lock.acquire()
331 calib_fac.gain[3] = (76*PI/180)/(256*ord(right_s[20]) + ord(right_s[21]) - calib_fac.offset[3])
332 glove_data_lock.release()
334 fingerAngle =
input(
"Palm arch, please enter acquired angle: ")
336 glove_data_lock.acquire()
337 calib_fac.gain[20] = (fingerAngle*PI/180)/(256*ord(right_s[52]) + ord(right_s[53]) - calib_fac.offset[20])
338 glove_data_lock.release()
340 file = open(
"generateGloveCalibration.cal",
"w")
342 file.write(
"VTi CyberGlove Calibration File (v2.0.0)")
343 for finger
in range(6):
344 file.write(
"Finger " +
str(finger) +
":")
345 for joint
in range(4):
346 file.write(
" " +
str(joint) +
" " +
str(calib_fac.offset[finger*4 + joint]) +
" " +
str(calib_fac.gain[finger*4 + joint]) +
" 0")
350 start_new_thread(read_glove,())
358 traceback.print_exc()