-
Notifications
You must be signed in to change notification settings - Fork 0
/
LizardForceRecorder.py
executable file
·321 lines (216 loc) · 9.66 KB
/
LizardForceRecorder.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
# Settings are at the end of the file!
################################################################################
### Libraries ###
################################################################################
import DAQToolbox as IOT
# import multiprocessing as MPR
import time as TI
import os as OS
import numpy as NP
import pandas as PD
import re as RE
import threading as TH
from collections import deque as DEQue # double ended queue
import matplotlib as MP # plotting
import matplotlib.pyplot as MPP # plot control
MP.use('TkAgg')
# check if there is a data folder
if not OS.path.exists('data'):
OS.makedirs('data')
################################################################################
### Muted Force Plate ###
################################################################################
class SilentForcePlateDAQ(IOT.TriggeredForcePlateDAQ):
def StdOut(self, *args, **kwargs):
pass
################################################################################
### Force Recorder ###
################################################################################
class ForceRecorder(object):
def __init__(self, recording_duration, label = '', sampling_rate = 1e3, scan_frq = 1e6, clock_hz = 1.0e6, viewer = None):
self.sampling_rate = sampling_rate
self.scan_frq = scan_frq
self.recording_duration = recording_duration
self.label = label
self.viewer = viewer
self._threads = None
# initialize first DAQ
self.daqs = {}
try:
daq1 = SilentForcePlateDAQ( \
fp_type = 'kistler' \
, device_nr = 0 \
, pins = {'led': 7} \
, sampling_rate = self.sampling_rate \
, scan_frq = self.scan_frq \
, recording_duration = self.recording_duration \
)
self.daqs[daq1.label] = daq1
except Exception as e:
print ('error in DAQ1 setup:\n\t', e, '\n')
# initialize no second DAQ
# initialize no NXP sensor
# prepare auto save
self.PrepareAutosave()
### initialize viewer
self.device_labels = self.daqs.keys()
self.q = DEQue()
self.PreparePlot()
def RestoreAxes(self):
for daq, ax in self.ax_dict.items():
ax.clear()
ax.set_xlim(0., self.recording_duration)
ax.set_ylim(0., len(all_data_columns[daq])+1.)
ax.set_xlabel('time (s)')
ax.set_yticks(NP.arange(len(all_data_columns[daq]))+1)
ax.set_yticklabels(all_data_columns[daq][::-1])
for split in plot_splits[daq]:
ax.axhline(split, color = 'k')
def PreparePlot(self):
self.fig, axes = MPP.subplots(1, len(self.device_labels))
if len(self.device_labels) < 2:
axes = [axes]
self.fig.subplots_adjust( \
top = 0.99 \
, right = 0.99 \
, bottom = 0.06 \
, left = 0.06 \
, wspace = 0.10 # column spacing \
, hspace = 0.10 # row spacing \
)
self.ax_dict = {devlab: axes[nr] for nr, devlab in enumerate(self.device_labels)}
self.RestoreAxes()
# self.ax.yaxis.tick_right()
# self.ax.yaxis.set_label_position("right")
# self.ax.set_title('press "E" to exit.')
# draw all empty
# MPP.draw()
self.fig.canvas.draw()
# self.fig.canvas.mpl_connect('key_press_event', self.Exit)
# cache the background
self.plot_backgrounds = { key: self.fig.canvas.copy_from_bbox(ax.bbox) \
for key, ax in self.ax_dict.items() \
}
# prepare lines
self.handles = {}
for devlab in self.device_labels:
for col in all_data_columns[devlab]:
self.handles[col] = self.ax_dict[devlab].plot([0], [0], linestyle = '-')[0]
def UpdatePlot(self):
rec_nr, daq, data = self.q.popleft()
# restore background
# self.fig.canvas.restore_region(self.plot_backgrounds[daq])
self.RestoreAxes()
# self.fig.suptitle("recording %i" % (rec_nr))
# adjust and redraw data
y_ticks = []
for col in data.columns:
t = NP.linspace(0, self.recording_duration, data.shape[0], endpoint = False)
y = NP.array(data[col].values, dtype = float)
# normalize
y -= y[0]
y /= (NP.max(y)-NP.min(y)+1e-3)/2
# shift
offset = len(all_data_columns[daq])-all_data_columns[daq].index(col)
y += offset
self.handles[col].set_data(t, y) # plot one less to avoid flickering
self.ax_dict[daq].draw_artist(self.handles[col])
self.ax_dict[daq].set_title("recording %i" % (rec_nr))
self.fig.canvas.blit(self.ax_dict[daq].bbox)
self.fig.canvas.flush_events()
def PrepareAutosave(self):
# auto file saving
self.datetag = TI.strftime('%Y%m%d')
self.file_pattern = "data/{date:s}_{label:s}_rec{nr:03.0f}_{suffix:s}.csv"
self.suffix = ''
# check how many previous recordings there were
previous_recordings = [file for file in OS.listdir('data') if OS.path.splitext(file)[1] == '.csv']
counts = [0]
for file in previous_recordings:
filename = OS.path.splitext(file)[0]
found = RE.findall(r"(?<=_rec)\d*", filename) # find file name patterns of the type "*daqXXX_*"
# print (filename, found)
if not (len(found) == 0):
counts.append(int(found[0])) #[:-1]
self.recording_counter = max(counts) + 1
print (f'continuing on recording {self.recording_counter}')
def SetRecordingDuration(self, new_duration):
self.recording_duration = new_duration
for daq in self.daqs.values():
daq.recording_duration = new_duration
daq.PrepareAnalogAcquisition()
def LaunchPlates(self, daq):
# will start up all the input devices
daq.Record()
def Record(self):
# will begin recording, waiting for trigger
print ('\n', '_'*32)
self._threads = [TH.Thread(target = self.LaunchPlates, args = [daq]) for daq in self.daqs.values()]
print ('waiting for trigger to record...', end = '\r')
for trd in self._threads:
trd.start()
TI.sleep(0.1)
for trd in self._threads:
trd.join()
self._threads = None
print ('saving...', ' '*32 , end = '\r')
self.StoreOutput()
TI.sleep(1.)
# print (self.q)
def StoreOutput(self):
MakeFileName = lambda suffix: self.file_pattern.format( \
date = self.datetag \
, label = self.label \
, nr = self.recording_counter \
, suffix = suffix \
)
for daq, device in self.daqs.items():
sync, data = device.RetrieveOutput()
sync.to_csv(MakeFileName(suffix = 'sync'), sep = ';', index = False)
data.to_csv(MakeFileName(suffix = 'force'), sep = ';')
# send to viewer
self.q.append([self.recording_counter, daq, data])
device.Empty()
print('done recording %i! ' % (self.recording_counter), ' '*32)
self.recording_counter += 1
def Stop(self):
for daq, device in self.daqs.items():
device.analog_input.scan_stop()
device.Quit()
self.playing = False
self.fig.close()
def Loop(self):
self.playing = True
while self.playing:
try:
self.Record()
except Exception as e:
raise e
self.Stop()
break
except KeyboardInterrupt as ki:
self.Stop()
break
# TI.sleep(1)
MPP.pause(1.e0)
while (len(self.q) > 0):
self.UpdatePlot()
# MPP.pause(1.e0)
# MPP.show()
################################################################################
### Data Viewer ###
################################################################################
all_data_columns = { \
'blue': list(sorted(IOT.forceplate_settings['kistler']['channel_order'])) \
}
plot_splits = { \
'blue': [8.5] \
}
################################################################################
### Mission Control ###
################################################################################
if __name__ == "__main__":
ForceRecorder( recording_duration = 8 # s \
, sampling_rate = 10e3 # Hz \
, label = 'lizards' \
).Loop() # (starting immediately)