-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
323 lines (302 loc) · 13.6 KB
/
sensors.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
"""
Jonne Klockars 2023
HUMEA Lab
Sensors class handles sensors to be used with Sonic Move Biodata Sonata.
"""
from math import floor
# https://dearpygui.readthedocs.io/en/latest/
import dearpygui.dearpygui as dpg
def dancers(number_of_dancers=3, number_of_sensors=3):
"""Returns a list of dictionaries of dictionaries for
sensor data of each dancer.
"""
dancers = [
{'snsr_1' : {} ,'snsr_2' : {}, 'snsr_3' : {}}
for _ in range(number_of_dancers)
]
# Length of data plots' x-axes is 500, initialise data as zeros.
for dancer in dancers:
for i in range(1, number_of_sensors+1):
dancer[f'snsr_{i}']['tot_a'] = [0] * 500
dancer[f'snsr_{i}']['b_tot_a'] = [0] * 500
dancer[f'snsr_{i}']['rot'] = [0] * 500
dancer[f'snsr_{i}']['ori_p'] = [0] * 500
dancer[f'snsr_{i}']['ori_r'] = [0] * 500
dancer[f'snsr_{i}']['ori_y'] = [0] * 500
for data in ['acc', 'gyr', 'mag']:
for axis in ['x', 'y', 'z']:
dancer[f'snsr_{i}'][f'{data}_{axis}'] = [0] * 500
return dancers
class Sensors:
def __init__(self):
"""XdaDevice instance method configure_device() appends
a list of XsDevice pointers to sensors connected to the
main device to Sensors attribute 'sensors'.
"""
self.axes = ['x', 'y', 'z']
self.sensors = None
# locations[id_key][0]: sensor position on the dancer.
# locations[id_key][1]: dancers 1,2 and 3.
# locations[id_key][2]: 1 for left, 2 for right, 3 for torso.
self.locations = {
'00B42D46' : ['left', 1, 1], # dancer 1 left etc.
'00B42D4F' : ['right', 1, 2],
'00B42D4B' : ['torso', 1, 3],
'00B42D56' : ['left', 2, 1],
'00B42D32' : ['right', 2, 2],
'00B42D44' : ['torso', 2, 3],
'00B42D54' : ['left', 3, 1],
'00B42D4E' : ['right', 3, 2],
'00B42B48' : ['torso', 3, 3]
}
self.labels = [
('acc','Acceleration'), ('tot_a', 'Total Acceleration'),
('ori', 'Orientation'), ('gyr', 'Gyroscope'),
('rot', 'Rate of Turn'), ('mag', 'Magnetometer')
]
self.minmax = [
{
'id': None, 'acc_min':[0, 0, 0], 'gyr_min':[0, 0, 0],
'mag_min':[0, 0, 0], 'ori_min':[0, 0, 0], 'tot_a_min':[0],
'rot_min':[0], 'acc_max':[1, 1, 1], 'gyr_max':[1, 1, 1],
'mag_max':[1, 1, 1], 'ori_max':[1, 1, 1], 'tot_a_max':[1],
'rot_max':[1]
}
for _ in range(9)
]
self.dancers = dancers()
def set_ids(self):
"""Sets sensor ids to the dashboard and to self.minmax dictionary
list used for data scaling.
"""
for i, sensor in enumerate(self.sensors):
sensor_id = f'{sensor.deviceId()}'
w = self.locations[sensor_id][1]
k = self.locations[sensor_id][2]
self.minmax[i]['id'] = sensor_id
for j in range(6):
dpg.configure_item(
f'dncr{w}_snsr{k}_{self.labels[j][0]}',
label=f'{sensor_id} {self.labels[j][1]}'
)
def scale_data(self, sensor_id, data_type, value):
"""Scales sensor data to the unit interval."""
scaled_data = []
sensor = [
sensor for sensor in self.minmax if sensor['id'] == sensor_id
][0]
for i, val in enumerate(value):
minimum = sensor[f'{data_type}_min'][i]
maximum = sensor[f'{data_type}_max'][i]
if val < minimum:
sensor[f'{data_type}_min'][i] = val
elif val > maximum:
sensor[f'{data_type}_max'][i] = val
# numpy.float64 value to Python's native float.
scaled_data.append(
float((val - minimum) / (maximum - minimum))
)
return scaled_data
def send_data(self, sensor_id, data_type, value):
"""Sends sensor data and ids to the dashboard plots."""
s = self.locations[sensor_id][2]
k = self.locations[sensor_id][1] - 1
# x, y, and z coordinates data to their plots.
if data_type in ['acc', 'gyr', 'mag']:
for i, val in enumerate(value):
self.dancers[k][f'snsr_{s}'][f'{data_type}_{self.axes[i]}'].append(val)
cutoff = len(self.dancers[k][f'snsr_{s}'][f'{data_type}_{self.axes[i]}']) - 500
if cutoff > 0:
del self.dancers[k][f'snsr_{s}'][f'{data_type}_{self.axes[i]}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}{self.axes[i]}',
y=self.dancers[k][f'snsr_{s}'][f'{data_type}_{self.axes[i]}']
)
# Euler angles data to its plot.
elif data_type == 'ori':
self.dancers[k][f'snsr_{s}'][f'{data_type}_p'].append(value[0])
self.dancers[k][f'snsr_{s}'][f'{data_type}_r'].append(value[1])
self.dancers[k][f'snsr_{s}'][f'{data_type}_y'].append(value[2])
for angle in ['p', 'y', 'r']:
cutoff = len(self.dancers[k][f'snsr_{s}'][f'{data_type}_{angle}']) - 500
if cutoff > 0:
del self.dancers[k][f'snsr_{s}'][f'{data_type}_{angle}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}{angle}',
y=self.dancers[k][f'snsr_{s}'][f'{data_type}_{angle}']
)
# Rate of turn data to its plot.
elif data_type == 'rot':
self.dancers[k][f'snsr_{s}'][f'{data_type}'].append(value[0])
cutoff = len(self.dancers[k][f'snsr_{s}'][f'{data_type}']) - 500
if cutoff > 0:
del self.dancers[k][f'snsr_{s}'][f'{data_type}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}',
y=self.dancers[k][f'snsr_{s}'][f'{data_type}']
)
# Total acceleration and binary value data to their plot.
elif data_type == 'tot_a':
self.dancers[k][f'snsr_{s}'][f'{data_type}'].append(value[0])
self.dancers[k][f'snsr_{s}'][f'b_{data_type}'].append(value[1])
cutoff = len(self.dancers[k][f'snsr_{s}'][f'{data_type}']) - 500
if cutoff > 0:
del self.dancers[k][f'snsr_{s}'][f'{data_type}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}',
y=self.dancers[k][f'snsr_{s}'][f'{data_type}']
)
cutoff = len(self.dancers[k][f'snsr_{s}'][f'b_{data_type}']) - 500
if cutoff> 0:
del self.dancers[k][f'snsr_{s}'][f'b_{data_type}'][0]
if self.dancers[k][f'snsr_{s}'][f'b_{data_type}'] == 0:
dpg.configure_item(
f'b_{data_type}{k}_{s}',
y=self.dancers[k][f'snsr_{s}'][f'b_{data_type}']
)
elif self.dancers[k][f'snsr_{s}'][f'b_{data_type}'] == 1:
dpg.configure_item(
f'b_{data_type}{k}_{s}',
y=self.dancers.dancers[k][f'snsr_{s}'][f'b_{data_type}']
)
def status(self, ids=False, finished=False):
"""Sets and checks the measurement status of the sensors."""
for i, sensor in enumerate(self.sensors):
if ids:
dpg.set_value(f'snsr_id{i}', f'{sensor.deviceId()}')
if finished:
dpg.set_value(f'sensor_{i}', 'Finished')
elif not sensor.isMeasuring():
dpg.set_value(f'sensor_{i}', 'Error!')
else:
dpg.set_value(f'sensor_{i}', 'Measuring')
def plot_log(file_path, dancers, axes):
"""Plots a txt log file. One line in the txt log file written by
XdaDevice class method recording_loop() contains the data from a single
data packet sent by a sensor.
"""
# locations[id_key][0]: sensor position on the dancer.
# locations[id_key][1]: dancers 1,2 and 3.
# locations[id_key][2]: 1 for left, 2 for right, 3 for torso.
locations = {
'00B42D46' : ['left', 1, 1], # dancer 1 left etc.
'00B42D4F' : ['right', 1, 2],
'00B42D4B' : ['torso', 1, 3],
'00B42D56' : ['left', 2, 1],
'00B42D32' : ['right', 2, 2],
'00B42D44' : ['torso', 2, 3],
'00B42D54' : ['left', 3, 1],
'00B42D4E' : ['right', 3, 2],
'00B42B48' : ['torso', 3, 3]
}
with open(file_path, 'r') as log:
sensor_id = None
for line in log.readlines():
if line.split()[0] == '11:':
sensor_id = '00B42D46'
elif line.split()[0] == '12:':
sensor_id = '00B42D4F'
elif line.split()[0] == '13:':
sensor_id = '00B42D4B'
elif line.split()[0] == '21:':
sensor_id = '00B42D56'
elif line.split()[0] == '22:':
sensor_id = '00B42D32'
elif line.split()[0] == '23:':
sensor_id = '00B42D44'
elif line.split()[0] == '31:':
sensor_id = '00B42D54'
elif line.split()[0] == '32:':
sensor_id = '00B42D4E'
elif line.split()[0] == '33:':
sensor_id = '00B42B48'
acc_value = line.split(': ')[1:4]
acc_value = [float(val) for val in acc_value]
tot_a_value = [float(line.split(': ')[4]), 0]
gyr_value = line.split(': ')[5:8]
gyr_value = [float(val) for val in gyr_value]
rot_value = [float(line.split(': ')[8])]
mag_value = line.split(': ')[9:12]
mag_value = [float(val) for val in mag_value]
euler_value = line.split(': ')[12:15]
euler_value = [float(val) for val in euler_value]
send_log_data(
sensor_id, 'acc', acc_value, dancers, locations, axes
)
send_log_data(
sensor_id, 'tot_a', tot_a_value, dancers, locations, axes
)
send_log_data(
sensor_id, 'gyr', gyr_value, dancers, locations, axes
)
send_log_data(
sensor_id, 'rot', rot_value, dancers, locations, axes
)
send_log_data(
sensor_id, 'mag', mag_value, dancers, locations, axes
)
send_log_data(
sensor_id, 'ori', euler_value, dancers, locations, axes
)
def send_log_data(sensor_id, data_type, value, dancers, locations, axes):
"""Sends sensor data from a logfile to the dashboard plots."""
s = locations[sensor_id][2]
k = locations[sensor_id][1] - 1
# x, y, and z coordinate data to their plots.
if data_type in ['acc', 'gyr', 'mag']:
for i, val in enumerate(value):
dancers[k][f'snsr_{s}'][f'{data_type}_{axes[i]}'].append(val)
cutoff = len(dancers[k][f'snsr_{s}'][f'{data_type}_{axes[i]}']) - 500
if cutoff > 0:
del dancers[k][f'snsr_{s}'][f'{data_type}_{axes[i]}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}{axes[i]}',
y=dancers[k][f'snsr_{s}'][f'{data_type}_{axes[i]}']
)
# Euler angles data to its plot.
elif data_type == 'ori':
dancers[k][f'snsr_{s}'][f'{data_type}_p'].append(value[0])
dancers[k][f'snsr_{s}'][f'{data_type}_r'].append(value[1])
dancers[k][f'snsr_{s}'][f'{data_type}_y'].append(value[2])
for angle in ['p', 'y', 'r']:
cutoff = len(dancers[k][f'snsr_{s}'][f'{data_type}_{angle}']) - 500
if cutoff > 0:
del dancers[k][f'snsr_{s}'][f'{data_type}_{angle}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}{angle}',
y=dancers[k][f'snsr_{s}'][f'{data_type}_{angle}']
)
# Rate of turn data to its plot.
elif data_type == 'rot':
dancers[k][f'snsr_{s}'][f'{data_type}'].append(value[0])
cutoff = len(dancers[k][f'snsr_{s}'][f'{data_type}']) - 500
if cutoff > 0:
del dancers[k][f'snsr_{s}'][f'{data_type}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}',
y=dancers[k][f'snsr_{s}'][f'{data_type}']
)
# Total acceleration and binary value data to their plot.
elif data_type == 'tot_a':
dancers[k][f'snsr_{s}'][f'{data_type}'].append(value[0])
dancers[k][f'snsr_{s}'][f'b_{data_type}'].append(value[1])
cutoff = len(dancers[k][f'snsr_{s}'][f'{data_type}']) - 500
if cutoff > 0:
del dancers[k][f'snsr_{s}'][f'{data_type}'][0]
dpg.configure_item(
f'{data_type}{k}_{s}',
y=dancers[k][f'snsr_{s}'][f'{data_type}']
)
cutoff = len(dancers[k][f'snsr_{s}'][f'b_{data_type}']) - 500
if cutoff> 0:
del dancers[k][f'snsr_{s}'][f'b_{data_type}'][0]
if dancers[k][f'snsr_{s}'][f'b_{data_type}'] == 0:
dpg.configure_item(
f'b_{data_type}{k}_{s}',
y=dancers[k][f'snsr_{s}'][f'b_{data_type}']
)
elif dancers[k][f'snsr_{s}'][f'b_{data_type}'] == 1:
dpg.configure_item(
f'b_{data_type}{k}_{s}',
y=dancers[k][f'snsr_{s}'][f'b_{data_type}']
)