/
Vazquez_ScrollingSteps_V1_rev2.py
309 lines (271 loc) · 9.39 KB
/
Vazquez_ScrollingSteps_V1_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
#This script is a scrolling scene biofeedback exercise. Subjects walk on the treadmill
#at a set speed while "stepping stones" or step length targets appear along the way.
#Subjects are intended to walk on the targets to their best ability. Similar to Guitar Hero
#
#V1 training with biofeedback
#V2P DK2 R2
#
#Rev 2 first version to animate an avitar's bone
#
#WDA 2/18/2016
import viz
import vizshape
import time
import vizinfo
import random
import itertools
import socket
import sys
import io
import re
import threading
import Queue
import time
import json
import vizact
import struct
import array
import math
import subprocess
import win32gui
import numpy as np
import oculus
import vizlens
global cpps
cpps = subprocess.Popen("C:/Users/Gelsey Torres-Oviedo/Documents/Visual Studio 2013/Projects/Vicon2Python_DK2_rev1/x64/Release/Vicon2Python_DK2_rev1.exe")
time.sleep(2)
viz.splashScreen('C:\Users\Gelsey Torres-Oviedo\Desktop\VizardFolderVRServer\Logo_final_DK2.jpg')
viz.go(
#viz.FULLSCREEN #run world in full screen
)
#time.sleep(2)#show off our cool logo, not really required but cool
global targetSL
targetSL = 0.55 #units must be meters
global targettol
targettol = 0.025
global subjectheight
subjectheight = 1.7 #units must be meters
#global hmd
## Setup Oculus Rift HMD
#hmd = oculus.Rift()
#if not hmd.getSensor():
# sys.exit('Oculus Rift not detected')
#
## Check if HMD supports position tracking
#supportPositionTracking = hmd.getSensor().getSrcMask() & viz.LINK_POS
#if supportPositionTracking:
# global man
# # Add camera bounds model
# camera_bounds = hmd.addCameraBounds()
# camera_bounds.visible(True)
#
# # Change color of bounds to reflect whether position was tracked
# def CheckPositionTracked():
# if hmd.getSensor().getStatus() & oculus.STATUS_POSITION_TRACKED:
# camera_bounds.color(viz.GREEN)
# else:
# camera_bounds.color(viz.RED)
# vizact.onupdate(0, CheckPositionTracked)
#
#
## Setup navigation node and link to main view
#navigationNode = viz.addGroup()
#viewLink = viz.link(navigationNode, viz.MainView)
#viewLink.preMultLinkable(hmd.getSensor())
#
##apply user profile data to view
#profile = hmd.getProfile()
#if profile:
## print(profile)
# viewLink.setOffset([0,subjectheight,0])
# hmd.setIPD(profile.ipd)
#
#
#def ReCenterView(hmd):
# hmd.getSensor().reset()
global beltspeed
beltspeed = 1 #units must be meters/second
global walkdistance
walkdistance = 50/50 #this sets the scale of the walkway length, numerator should be the desired length, denominator is the natural length of the walkway
global walktime
walktime = (walkdistance*50)/beltspeed
global walkway
walkway = viz.addChild('ground.osgb',scale = [0.0167,1,walkdistance])
walkway.setPosition(0,0,(walkdistance*50)/2)#start at one end of the walkway
sky = viz.addChild('sky_day.osgb')
grass1 = viz.addChild('ground_grass.osgb')
grass1.setPosition(0,-0.01,0)
grass2 = viz.addChild('ground_grass.osgb')
grass2.setPosition(0,-0.01,50)
grass3 = viz.addChild('ground_grass.osgb')
grass3.setPosition(0,-0.01,50)
#create a divider line
global divider
divider = vizshape.addQuad(size=(0.01,walkdistance*50),
axis=-vizshape.AXIS_Y,
cullFace=False,
cornerRadius=0)
divider.setPosition(0,0.001,(walkdistance*50)/2)
divider.color(255,255,255)
#make the Right targets
rt = {}
for x in range(1,int((walkdistance*50)/(targetSL*2)),1):
# rt["T{0}".format(x)]=vizshape.addQuad((0.1,targettol),axis=-vizshape.AXIS_Y,cullFace=False,cornerRadius=0)
rt["T{0}".format(x)]=vizshape.addBox(size=[0.25,0.02,2*targettol])
rt["T{0}".format(x)].setPosition(0.2,0.011,x*2*targetSL)
rt["T{0}".format(x)].color(0,0,0)
lt = {}
for x in range(1,int((walkdistance*50)/(targetSL*2)),1):
# rt["T{0}".format(x)]=vizshape.addQuad((0.1,targettol),axis=-vizshape.AXIS_Y,cullFace=False,cornerRadius=0)
lt["T{0}".format(x)]=vizshape.addBox(size=[0.25,0.02,2*targettol])
lt["T{0}".format(x)].setPosition(-0.2,0.011,targetSL+x*2*targetSL)
lt["T{0}".format(x)].color(0,0,0)
#load avatar Mark
global man
#man = viz.add('mark.cfg')
man = viz.add('vcc_male.cfg')
man.disable(viz.LIGHTING)
man.setPosition(0,0,-0.5)
global rfoot
rfoot = man.getBone('Bip01 R Foot')
rfoot.lock()
global rcalf
rcalf = man.getBone('Bip01 R Calf')
rcalf.lock()
global rthigh
rthigh = man.getBone('Bip01 R Thigh')
rthigh.lock()
global lfoot
lfoot = man.getBone('Bip01 L Foot')
lfoot.lock()
global lcalf
lcalf = man.getBone('Bip01 L Calf')
lcalf.lock()
global lthigh
lthigh = man.getBone('Bip01 L Thigh')
lthigh.lock()
time.sleep(1)
#generate forward velocity
scroll = vizact.move(0,0,-1*beltspeed,walktime)
#start the movement
walkway.add(scroll)
grass1.add(scroll)
grass2.add(scroll)
grass3.add(scroll)
divider.add(scroll)
for x in range(1,int((walkdistance*50)/(targetSL*2)),1):
rt["T{0}".format(x)].add(scroll)
lt["T{0}".format(x)].add(scroll)
def UpdateViz(root,q):#,speedlist,qq,savestring,q3):
while not endflag.isSet():
global man
global rfoot
global rcalf
global rthigh
global lfoot
global lcalf
global lthigh
root = q.get()
data = ParseRoot(root)
FN = int(data["FN"])
Rz = float(data["Rz"])
Lz = float(data["Lz"])
# rcalf.setEuler(180/math.pi*float(data["RightTibia"][1]),180/math.pi*float(data["RightTibia"][1]),-1*180/math.pi*float(data["RightTibia"][1]))
rcalf.setEuler(0,0,-1*180/math.pi*float(data["RightTibia"][1]))
rthigh.setEuler(180,0,-1*180/math.pi*float(data["RightFemur"][1])-20)
rfoot.setEuler(0,0,-1*180/math.pi*float(data["RightFoot"][1]))
lcalf.setEuler(0,0,-1*180/math.pi*float(data["LeftTibia"][1]))
lthigh.setEuler(180,0,-1*180/math.pi*float(data["LeftFemur"][1])-20)
lfoot.setEuler(0,0,-1*180/math.pi*float(data["LeftFoot"][1]))
# rcalf.setEuler(-1*180/math.pi*float(data["RightTibia"][2]),180/math.pi*float(data["RightTibia"][0]),-1*180/math.pi*float(data["RightTibia"][1]))
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():
global FNold
global repeatcount
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):
data = s.recv(nextsize)#data buffer as a python string
databuf = databuf + data#collect data into buffer until size is matched
root = databuf
# print('root',root)
# 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 ParseRoot(root):#the purpose of this function is to make sure that marker data is used correctly since they can arrive in different order, depending on the order the models are listed in Nexus
tempdat = root.split(',')
# print tempdat
del tempdat[-1]#the last element is a empty string ""
data = {}#create dictionary
data["FN"] = int(tempdat[0])#frame number
data["Rz"] = float(tempdat[4])#right forceplate Z component
data["Lz"] = float(tempdat[2])#left forceplate Z comp.
data["DeviceCount"] = float(tempdat[5])# #of devices besides forceplates
for x in range(6,6+2*int(data["DeviceCount"])-1,2): #assumes one value per device for now...
temp = tempdat[x]
data[temp] = [tempdat[x+1]]
# print temp
#place marker data into dictionary
for z in range(6+2*int(data["DeviceCount"]),len(tempdat),4):
temp = tempdat[z]
# print temp
data[temp] = [tempdat[z+1],tempdat[z+2],tempdat[z+3]]
# print(data)
return data
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 = ''
#speedlist = array.array('i')
q = Queue.Queue()#initialize the queue
qq = Queue.Queue()#another queue for treadmill commands
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))#,speedlist,qq,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
#vizact.onkeydown('r',ReCenterView,hmd)#biggle is meaningless, just need to pass something into the raisestop callback