-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFback_GradualAdaptation_statictargets_rev2.py
395 lines (339 loc) · 10.2 KB
/
BFback_GradualAdaptation_statictargets_rev2.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
#script runs biofeedback routine with TMM, Vicon. Gives feedback on the length of step
#while in gait.
#revision 2 switches from using TMM to python cpp server, same as rev 1 otherwise.
#2/2/2015
import socket
import sys
import io
import re
from xml.etree import ElementTree
import viz
import threading
import Queue
import time
import json
import vizact
viz.splashScreen('C:\Users\Gelsey Torres-Oviedo\Desktop\VizardFolderVRServer\Logo_final.jpg')
viz.go(
viz.FULLSCREEN
)
#indicate flag for post-catch targets, which last for the first 12 steps
global catchflag
catchflag = 0
global stridecounter
stridecounter = 0
#set target tolerance for stride length
global targetL
targetL = 0.5
global targetR
targetR = 0.5
global targettol
targettol = 0.025
global boxL
boxL = viz.addChild('target.obj',color=(0.063,0.102,0.898),scale=[0.1,targettol,0.005])
boxL.setPosition([-0.2,targetL,0])
global boxR
boxR = viz.addChild('target.obj',color=(0.063,0.102,0.898),scale=[0.1,targettol,0.005])
boxR.setPosition([0.2,targetR,0])
viz.MainView.setPosition(0, 0.4, -1.25)
viz.MainView.setEuler(0,0,0)
#setup counter panels to count successful steps taken
global RCOUNT
global LCOUNT
RCOUNT = 0
LCOUNT = 0
#rightcounter = vizinfo.InfoPanel(str(RCOUNT),align=viz.ALIGN_RIGHT_TOP,fontSize=50,icon=False,key=None)
rightcounter = viz.addText(str(RCOUNT),pos=[4.6,3*targetR,12])
rightcounter.visible(0)
#leftcounter = vizinfo.InfoPanel(str(LCOUNT),align=viz.ALIGN_LEFT_TOP,fontSize=50,icon=False,key=None)
leftcounter = viz.addText(str(LCOUNT),pos=[-5.5,3*targetL,12])
leftcounter.visible(0)
global cursorR
cursorR = viz.add('box3.obj', color=viz.RED, scale=[0.1,0.1,0.0125], cache=viz.CACHE_NONE)
cursorR.setPosition([0.2,0,0.05])
global cursorL
cursorL = viz.add('box3.obj', color=viz.GREEN, scale=[0.1,0.1,0.0125], cache=viz.CACHE_NONE)
cursorL.setPosition([-0.2,0,0.05])
global HistBallR
HistBallR = viz.add('box.wrl', color=viz.YELLOW, scale=[0.2,0.01,0.0125], cache=viz.CACHE_NONE)
HistBallR.setPosition([0.2,targetR,0])
HistBallR.alpha(0.8)
global histzR
histzR = 0
global histL
histzL = 0
global HistBallL
HistBallL = viz.add('box.wrl', color=viz.YELLOW, scale=[0.2,0.01,0.0125], cache=viz.CACHE_NONE)
HistBallL.setPosition([-0.2,targetL,0])
HistBallL.alpha(0.8)
global steplengthL
steplengthL = 0
global steplengthR
steplengthR = 0
global psudoR
global psudoL
psudoR = 0
psudoL = 0
global Rtop
global Rhsp
global Ltop
global Lhsp
Ltop = -.334
Lhsp = 0.1483
Rtop = -0.3366
Rhsp = 0.17033
#keep track of each step, whether it is good or bad
global Rgorb
global Lgorb
Rgorb = 0
Lgorb = 0
#R Rigth leg
global R
R = 1.9811
#R left leg
global R2
R2 = 2.1531
global rsci
rsci = 2.9401
global lsci
lsci = 3.2662
global RHS
RHS = 0
global LHS
LHS = 0
def UpdateViz(root,q,savestring,q3):
timeold = time.time()
while not endflag.isSet():
global rsci
global lsci
global R
global R2
global histzR
global histzL
global Rhsp
global Lhsp
global Rtop
global Ltop
global RCOUNT
global LCOUNT
global catchflag
global steplengthL
global steplengthR
global Rgorb
global Lgorb
global stridecounter
global psudoL
global psudoR
global RHS
global LHS
#get some data
root = q.get()
#find the data we need from the frame packet
lp1 = root.find(".//Forceplate_0/Subframe_0/F_z")#Left Treadmill
rp1 = root.find(".//Forceplate_1/Subframe_0/F_z")#Right Treadmill
s0rhipy = root.find(".//Subject0/RHIP/Y")
s0lhipy = root.find(".//Subject0/LHIP/Y")
s0ranky = root.find(".//Subject0/RANK/Y")
s0lanky = root.find(".//Subject0/LANK/Y")
temp = rp1.attrib.values()
Rz = float(temp[0])#cast forceplate data as float
temp1 = lp1.attrib.values()
Lz = float(temp1[0])
temp2 = s0rhipy.attrib.values()
RHIPY = float(temp2[0])/1000 #convert to meters
temp3 = s0lhipy.attrib.values()
LHIPY = float(temp3[0])/1000
temp4 = s0ranky.attrib.values()
RANKY = float(temp4[0])/1000
temp5 = s0lanky.attrib.values()
LANKY = float(temp5[0])/1000
cursorR.setScale(0.1,rsci*(RHIPY-RANKY),0.01250)
cursorL.setScale(-0.1,lsci*(LHIPY-LANKY),0.01250)
#determine if we need to hide the cursor
if (RHIPY-RANKY < 0) | (Rz < -30):
cursorR.visible(0)
else:
cursorR.visible(1)
if (LHIPY-LANKY < 0) | (Lz < -30):
cursorL.visible(0)
else:
cursorL.visible(1)
#detect gait events
if (Rz < -30) & (histzR >-30): #RHS
HistBallR.setPosition([0.2,rsci*(RHIPY-RANKY), 0])
steplengthR = rsci*(RHIPY-RANKY)
stridecounter = stridecounter+1
RHS = 1
if (psudoR == 5):
Rhsp = RHIPY-RANKY#update the alpha value
rsci = 0.5*(1/(Rhsp+1/(1+R)*(abs(Rtop)-R*Rhsp)))
# print("rsci is: ")
# print(rsci)
if (abs(steplengthR-targetR) <= targettol):
RCOUNT = RCOUNT+1
rightcounter.message(str(RCOUNT))
Rgorb = 1 #flag this step as good or bad
else:
Rgorb = 0
if (catchflag == 1) & (stridecounter > 12):
boxR.setPosition([0.2,targetR,0])
elif (catchflag == 1) & (stridecounter <= 12):
rsci = 2
else:
boxR.setPosition([0.2,targetR,0])
elif (Rz > -30) & (histzR < -30): #RTO
#calculate Toe-Off position
if (psudoR == 5):
Rtop = RHIPY-RANKY#update beta value
psudoR = 1
psudoR = psudoR+1
RHS = 0
else:
RHS = 0
if (abs(steplengthR-targetR) <= targettol):#highlight the target when the target is hit within tolerance
boxR.color( viz.WHITE )
else:
boxR.color( viz.BLUE )
if (Lz < -30) & (histzL >-30): #LHS
HistBallL.setPosition([-0.2,lsci*(LHIPY-LANKY), 0])
steplengthL = lsci*(LHIPY-LANKY)
stridecounter = stridecounter+1
LHS = 1
if (psudoL == 5):
Lhsp = LHIPY-LANKY#update the alpha value
lsci = 0.5*(1/(Lhsp+1/(1+R2)*(abs(Ltop)-R2*Lhsp)))
# print("lsci is: ")
# print(lsci)
if (abs(steplengthL-targetL) <= targettol):
LCOUNT = LCOUNT+1
leftcounter.message(str(LCOUNT))
Lgorb = 1 #flag this step as good or bad
else:
Lgorb = 0
if (catchflag == 1) & (stridecounter > 12):
boxL.setPosition([-0.2,targetL,0])
elif (catchflag == 1) & (stridecounter <= 12):
lsci = 2
else:
boxL.setPosition([-0.2,targetL,0])
elif (Lz > -30) & (histzL < -30): #LTO
#calculate Toe-Off position
if (psudoL == 5):
Ltop = LHIPY-LANKY#update beta value
psudoL = 1
psudoL = psudoL+1
LHS = 0
else:
LHS = 0
if (abs(steplengthL-targetL) <= targettol):#highlight the target when the target is hit within tolerance
boxL.color( viz.WHITE )
else:
boxL.color( viz.BLUE )
histzR = Rz
histzL = Lz
#send some data to be saved
fn = root.find(".//FrameNumber")#find the frame number
fnn = fn.attrib.values()
# print(fnn[0])
savestring = [int(fnn[0]),Rz,Lz,RHS,LHS,Rgorb,Lgorb,RHIPY-RANKY,LHIPY-LANKY,rsci,lsci,RHIPY,LHIPY,RANKY,LANKY]#organize the data to be written to file
# print(sys.getsizeof(savestring))
q3.put(savestring)
# q3.join()
print("All data has been processed")
def runclient(root,q):
#illegal characters to remove from string later before going to xml
RE_XML_ILLEGAL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \
u'|' + \
u'([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \
(unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff))
HOST = 'localhost'#IP address of CPP server
PORT = 50008
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
print 'Socket now connecting'
s.connect((HOST,PORT))
s.send('1')#send initial request for data
while not endflag.isSet():
data = s.recv(50)#receive the initial message
data3 = data[:3]#get first 3 letters
if (data3 == "New"):
nextsizestring = data[3:]#get the integer after "New"
nextsizestring2 = nextsizestring.rstrip('\0')#format
nextsize = int(nextsizestring2,10)#cast as type int
# print("Next Packet is size: ")
# print(nextsize)
s.send('b')#tell cpp server we are ready for the packet
databuf = ''#initialize a buffer
while (sys.getsizeof(databuf) < nextsize+21):
data = s.recv(nextsize)#data buffer as a python string
databuf = databuf + data#collect data into buffer until size is matched
# print(sys.getsizeof(databuf))
root = ElementTree.ElementTree(ElementTree.fromstring(databuf))#create the element tree
q.put(root)#place the etree into the threading queue
elif (data3 != "New"):
print("WARNING! TCP SYNCH HAS FAILED")
break
if not data: break
s.send('b')
s.close()
def savedata(savestring,q3):
#initialize the file
mst = time.time()
mst2 = int(round(mst))
mststring = str(mst2)+'STATTARGsteplength.txt'
print("Data file created named:")
print(mststring)
file = open(mststring,'w+')
json.dump(['FrameNumber','Rfz','Lfz','RHS','LHS','RGORB','LGORB','Ralpha','Lalpha','Rscale','Lscale','RHIPY','LHIPY','RANKY','LANKY'],file)
file.close()
file = open(mststring,'a')#reopen for appending only
while not endflag.isSet():
savestring = q3.get()#look in the queue for data to write
if savestring is None:
continue
else:
json.dump(savestring, file)
print("savedata stop flag raised, finishing...")
while 1:
try:
savestring = q3.get(False,2)
except:
savestring = 'g'
# print(savestring)
if savestring == 'g':
break
print("data finished write to file")
else:
json.dump(savestring, file)
print("data still writing to file")
print("savedata finished writing")
file.close()
endflag = threading.Event()#an event to raise when we are ready to stop recording
def raisestop(sign):
#the sign passed in doesn't do anything, I just didn't know how to make this work without passing something in...
print("stop flag raised")
endflag.set()
t1.join(5)
t2.join(5)
t4.join(5)
viz.quit()
root = ''#empty string
savestring = ''
q = Queue.Queue()#initialize the queue
q3 = Queue.Queue()#intialize another queue for saving data
#create threads for client
t1 = threading.Thread(target=runclient,args=(root,q))
t2 = threading.Thread(target=UpdateViz,args=(root,q,savestring,q3))
t4 = threading.Thread(target=savedata,args=(savestring,q3))
t1.daemon = True
t2.daemon = True
t4.daemon = True
#start the threads
t1.start()
t2.start()
t4.start()
print("\n")
print("press 'q' to stop")
vizact.onkeydown('q',raisestop,'biggle')#biggle is meaningless, just need to pass something into the raisestop callback