-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPS_DAQ_debug.py
146 lines (118 loc) · 4.37 KB
/
PS_DAQ_debug.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
import owl
import sys
import atexit
import time
import re
import struct
import collections
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
num_markers = 28
PERSIST = True
OWL = owl.Context()
#OWL.debug = True
def shutdown():
global PERSIST
global OWL
print("#stopping")
PERSIST = False
OWL.done()
OWL.close()
atexit.register(shutdown)
def parse_channels(options):
ch = re.findall("channelids=([0-9,]+)", options)[0].split(',')
return [int(x,10) for x in filter(lambda x: len(x) > 0, ch)]
def wait_for_channel_info():
t0 = time.time()
while OWL.streaming() and PERSIST:
if time.time() - t0 > 30:
raise Exception("timed out waiting for device info")
e = OWL.nextEvent()
if e == None: continue
for di in OWL.property("deviceinfo"):
# print(di.name)
if di.name == "daq":
print("#", e)
print("#", di)
return parse_channels(di.options)
print("# open: ", OWL.open(sys.argv[1]))
print("# init: ", OWL.initialize("streaming=1 event.inputs=1 profile=LowerBodyProfile"))
channels = wait_for_channel_info()
print("# channels:", channels)
st = struct.Struct("<%dh" % (len(channels)))
frame = 0
frame_flag = 0
# # PhaseSpace output
# file_mocap = open("markers.txt", "w")
# file_mocap.write("Time" "\t")
# for i in range(num_markers):
# file_mocap.write("X" "%d" "\t" "Y" "%d" "\t" "Z" "%d" "\t" % (i, i, i))
# file_mocap.write("\n")
# # DAQ output and headers
# file_daq = open("analog.txt", "w")
# file_daq.write("Time" "\t" "FrameFlag" "\t")
# file_daq.write("EMG1" "\t" "EMG2" "\t" "EMG3" "\t" "EMG4" "\t" "EMG5" "\t" "EMG6" "\t" "EMG7" "\t" "EMG8" "\t")
# file_daq.write("R.FX" "\t" "R.FY" "\t" "R.FZ" "\t" "R.MX" "\t" "R.MY" "\t" "R.MZ" "\t")
# file_daq.write("L.FX" "\t" "L.FY" "\t" "L.FZ" "\t" "L.MX" "\t" "L.MY" "\t" "L.MZ" "\t")
# file_daq.write("TTL" "\n")
# Scaling factor: -10V ~ 10V
scale_factor = 10/32767
out = collections.deque(np.zeros(10))
# Figure appearance
plt.style.use('seaborn-colorblind')
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
# Get data
while OWL.streaming() and PERSIST:
evt = OWL.nextEvent()
if evt == None: continue
# # Get marker data
# if evt.type_id == owl.Type.FRAME:
# # print markers
# if "markers" in evt:
# for m in evt.markers:
# if m.id == 0:
# file_mocap.write("%8d" "\t" "%8d" "\t" "%8d" "\t" "%8d" "\t" % (m.time, m.x, m.y, m.z))
# elif m.id == num_markers-1:
# file_mocap.write("%8d" "\t" "%8d" "\t" "%8d" "\n" % (m.x, m.y, m.z))
# else:
# file_mocap.write("%8d" "\t" "%8d" "\t" "%8d" "\t" % (m.x, m.y, m.z))
# Get analog data
if evt.type_id == owl.Type.INPUT:
if frame != evt.time:
frame = evt.time
frame_flag = (frame_flag + 1) % 2
for inp in evt.data:
for offset in range(0, len(inp.data), len(channels)*2):
analog = st.unpack_from(inp.data, offset)
# file_daq.write("%8d""\t""%2d""\t" % (inp.time, frame_flag))
# for i in range(0,len(analog)-1):
# scaled_out = analog[i]*scale_factor
# file_daq.write("%.5f" "\t" % scaled_out)
# TTL = analog[len(analog)-1]*scale_factor
# file_daq.write("%.5f" "\n" % TTL)
# CH 0 ~ 7 EMG
scaled_out = analog[0]*scale_factor
TTL = analog[len(analog)-1]*scale_factor
if (inp.time%100 == 0):
out.popleft()
out.append(scaled_out)
# out.append(TTL)
plt.plot(out)
plt.ylim(-5, 5)
plt.draw()
plt.pause(0.01)
plt.cla()
elif evt.type_id == owl.Type.ERROR:
# handle errors
print(evt.name, evt.data)
if evt.name == "fatal":
break
elif evt.name == "done":
# done event is sent when master connection stops session
print("done")
fig.close()
print("out: {}".format(out))
break
OWL.shutdown()