forked from conorobrien/hapticsstb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
legacy_RunSTB.py
263 lines (159 loc) · 5.09 KB
/
legacy_RunSTB.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
print "DONT RUN THIS, REFERENCE ONLY"
exit(1)
import pylab as pl
import numpy as np
import sys, os, glob, pdb, time, threading, subprocess
from cv2 import VideoCapture, VideoWriter
from cv2.cv import CV_FOURCC
from HapticsSTB import *
from STBClassTest import *
inputs = ArgParse(sys.argv)
## Video Capture Setup
if inputs['video_capture']:
# OpenCV initialization, create videoCapture object and codec
# Switch Capture Card input to s-video
err = subprocess.call(['v4l2-ctl', '-i 4'])
if err == 1:
print "VIDEO CAPTURE ERROR, CHECK CARD AND TRY AGAIN"
sys.exit()
cap = VideoCapture(-1)
fourcc = CV_FOURCC(*'XVID')
# Thread function for video capture
class OpenCVThread(threading.Thread):
def __init__(self, cap, out):
threading.Thread.__init__(self)
self.stop = threading.Event()
self.out = out
self.cap = cap
def run(self):
while not self.stop.is_set():
ret, frame = self.cap.read()
if ret == True:
self.out.write(frame)
# Graphing Initialization
if inputs['graphing']:
inputs['sample_rate'] = 500;
print 'Forcing sample rate to 500Hz for graphing'
plot_objects = GraphingSetup(inputs)
# Auto Detection for USB, not needed on mac, but linux serial devices only
# show up as ttyACMn, not with a unique ID
(STB, PedalSerial) = DeviceID(inputs)
## BIASING
# read first 500 samples and average to get bias
print "DON'T TOUCH BOARD, BIASING..."
bias_hist = np.zeros((6,inputs['bias_sample']))
STB.start()
for ii in range(0, inputs['bias_sample']):
try:
dat = STB.read()
except EmptyPacketError:
print 'NOTHING RECIEVED, EXITING...'
STB.close()
if inputs['pedal']:
PedalSerial.close()
sys.exit()
bias_hist[:,ii] = Serial2M40Volts(dat)
bias = np.mean(bias_hist, axis=1).T
print "SAFE TO TOUCH"
print 'BIAS MATRIX'
print bias
STB.stop()
## SAMPLING
# Code takes samples for seconds defined in sample_time
# set lengths for hist vectors, if pedal mode just preallocate for 10 min
# session
if inputs['pedal']:
num_samples = inputs['sample_rate']*600
else:
num_samples = inputs['sample_rate']*inputs['sample_time']
try:
while 1:
# Pedal input blocking, single or double tap starts trial, triple quits
if inputs['pedal']:
print 'WAITING FOR PEDAL INPUT...'
pedal_input = ''
while pedal_input != '\x01':
pedal_input = PedalSerial.read()
if pedal_input == '\x03':
print 'QUITTING...'
sys.exit()
# File operations, checks to make sure folders exist and creates timestamp
if inputs['write_data'] or inputs['video_capture']:
data_dir = 'TestData'
subject_dir = 'Subject'+str(inputs['subject']).zfill(3)
test_filename = 'S' + str(inputs['subject']).zfill(3) + 'T' + str(inputs['task']) +'_' + time.strftime('%m-%d_%H-%M')
test_path = data_dir + '/' + subject_dir + '/' + test_filename
if [] == glob.glob(data_dir):
print "MAKING " + data_dir
os.mkdir(data_dir)
if [] == glob.glob(data_dir + '/' + subject_dir):
print "MAKING " + subject_dir
os.mkdir(data_dir + '/' + subject_dir)
# Video prep
if inputs['video_capture']:
out = VideoWriter(test_path+'.avi',fourcc, 29.970, (720,480))
videoThread = OpenCVThread(cap, out)
videoThread.start()
print 'STARTING DATA COLLECTION...'
start = time.time()
# If plotting voltage as well, DAT_hist has an extra six columns at the end
# which contain the voltage channels
if inputs['graphing'] == 2:
DAT_hist = np.zeros((num_samples,21))
else:
DAT_hist = np.zeros((num_samples, 15))
STB.start()
for ii in range(0,num_samples):
try:
dat = STB.read()
except EmptyPacketError:
print 'NOTHING RECIEVED, EXITING...'
STB.stop()
if inputs['pedal']:
PedalSerial.close()
if inputs['video_capture']:
videoThread.stop.set()
sys.exit()
DAT_hist[ii, 0:15] = Serial2Data(dat, bias)
if inputs['graphing'] == 2:
DAT_hist[ii,15:] = Serial2M40Volts(dat)
# Update Graph
if inputs['graphing']:
if ii % inputs['update_interval'] == 0 and ii > inputs['line_length']:
updated_data = DAT_hist[(ii + 1 - inputs['line_length']):(ii+1),:]
GraphingUpdater(inputs, updated_data, plot_objects)
if inputs['pedal']:
pedal_input = PedalSerial.read()
if pedal_input == '\x03':
print 'THREE CLICKS'
break
sys.exit()
elif pedal_input == '\x02':
print 'PEDAL STOP'
break
STB.stop()
print 'FINISHED SAMPLING'
print time.time()-start
if inputs['video_capture']:
videoThread.stop.set()
if inputs['write_data'] == 1:
print 'WRITING DATA TO %s...' %test_filename
np.savetxt(test_path + '.csv', DAT_hist[:(ii+1),0:15], delimiter=",")
print 'FINISHED WRITING'
if inputs['compress']:
os.chdir(data_dir + '/' + subject_dir)
err = subprocess.call(['7z','a',test_filename + '.7z', test_filename+ '.*'])
os.chdir('../..')
print '*'*80
if not inputs['pedal']:
break
except KeyboardInterrupt:
print '***** ENDING TESTING *****'
# STBserial.close()
STB.stop()
if inputs['pedal']:
PedalSerial.close()
if inputs['video_capture']:
videoThread.stop.set()