-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
305 lines (287 loc) · 14.3 KB
/
dashboard.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
"""
Jonne Klockars 2023
HUMEA Lab
Class for Sonic Move Biodata Sonata dashboard.
"""
import sys
import threading
# https://dearpygui.readthedocs.io/en/latest/
import dearpygui.dearpygui as dpg
from sensors import dancers, plot_log
import xda_device as xd
class Dashboard:
def __init__(self, device, path):
"""
Parameters
----------
device : str
'dongle' or 'station' Xsens main device. Default is 'station'.
path : str
File path for saving log files. Recommendation is to create a
folder just for the logs.
Attributes
----------
main_device : XdaDevice
The main Xsens device used for recording.
"""
self.setup()
self.main_device = xd.XdaDevice(device, path)
def set_threshold(self, sender, app_data):
"""Sets a threshold for acceleration."""
self.main_device.acc_threshold = dpg.get_value(sender)
def show_file_dialog(self, sender, app_data):
"""Shows log file selection window."""
dpg.show_item('file_dialog')
def file_dialog_callback(self, sender, app_data):
"""Callback for plotting a log file."""
log_file_path = app_data['file_path_name']
data_dicts = dancers()
axes = ['x', 'y', 'z']
plot_log(log_file_path, data_dicts, axes)
def recording_switch(self, sender, app_data):
"""Callback for switching recording on and off."""
self.main_device.recording = not self.main_device.recording
if self.main_device.recording:
self.main_device.create_control_object()
self.main_device.open_device()
self.main_device.configure_device()
self.main_device.go_to_recording_mode()
recording_loop_thread = threading.Thread(
target=self.main_device.recording_loop, daemon=True
)
recording_loop_thread.start()
def exit_program(self, sender, app_data):
"""Callback for exitng the program."""
dpg.set_value(
'program_status', 'Program exited by user.\n\n'
f'{dpg.get_value("program_status")}'
)
print('Program exited by user.')
try:
self.main_device.device.disableRadio()
except Exception as e:
dpg.set_value(
'program_status', 'Radio disabling failed.'
' Perhaps it was not on.'
f'{dpg.get_value("program_status")}'
)
print('Radio disabling failed. Perhaps it was not on.')
sys.exit(0)
def setup(self):
"""Creates a Dear PyGui dashboard. """
initial_data = [0] * 500
dpg.create_context()
vp = dpg.create_viewport(title='Sonify dashboard')
vp_width = dpg.get_viewport_width()
window_lbl = ['one', 'two', 'three']
window_pos = [(45,300), (25,320), (5,340)]
x_axis = list(range(500))
# Program status display window.
with dpg.window(label='Recording panel', pos=(25,0), width=vp_width):
dpg.add_button(
label='Recording on/off', callback=self.recording_switch
)
dpg.add_button(
label='Plot txt log file', callback=self.show_file_dialog
)
dpg.add_button(label='Quit', callback=self.exit_program)
dpg.add_text(
'Use mouse to scroll down program message history. First'
' message is in the bottom row.'
)
dpg.add_input_text(
tag='program_status', width=vp_width, multiline=True
)
# Sensor status display window.
with dpg.window(
label='Sensor status panel', pos=(25,195), width=vp_width
):
with dpg.table(header_row=False):
for i in range(9):
dpg.add_table_column()
with dpg.table_row():
for i in range(9):
dpg.add_text('Waiting for id', tag=f'snsr_id{i}')
with dpg.table_row():
for i in range(9):
dpg.add_text('No signal', tag=f'sensor_{i}')
dpg.add_text(
'Click on plot legend variables to show or hide related data'
)
# A window for sensors of each dancer.
for i in range(3):
with dpg.window(
label=f'Dancer {window_lbl[i]}', pos=window_pos[i],
width=vp_width, collapsed=True
):
with dpg.table(header_row=False):
# Three columns and two rows for six data types.
for _ in range(3):
dpg.add_table_column()
for j in range(1,4):
# Plot tag is 'data_typedancer_sensorcoordinate'
with dpg.table_row():
# Plot for acceleration data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_acc',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Acceleration data in x axis direction.
dpg.add_line_series(
x_axis, initial_data, label='x',
tag=f'acc{i}_{j}x', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Acceleration data in y axis direction.
dpg.add_line_series(
x_axis, initial_data, label='y',
tag=f'acc{i}_{j}y', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Acceleration data in z axis direction.
dpg.add_line_series(
x_axis, initial_data, label='z',
tag=f'acc{i}_{j}z', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
# Plot for total acceleration data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_tot_a',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Total acceleration data.
dpg.add_line_series(
x_axis, initial_data, label='acc',
tag=f'tot_a{i}_{j}', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 50)
dpg.add_drag_line(
default_value=30, vertical=False,
label="threshold", color=[255, 0, 0, 255],
callback=self.set_threshold
)
item = dpg.add_plot_axis(
dpg.mvYAxis, label='bin'
)
# Total acceleration binary data.
dpg.add_line_series(
x_axis, initial_data, label='0/1',
tag=f'b_tot_a{i}_{j}',
parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
# Plot for orientation data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_ori',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Orientation data in x axis direction.
dpg.add_line_series(
x_axis, initial_data, label='pitch',
tag=f'ori{i}_{j}p', parent=dpg.last_item()
)
dpg.set_axis_limits(item, -180, 180)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Orientation data in y axis direction.
dpg.add_line_series(
x_axis, initial_data, label='roll',
tag=f'ori{i}_{j}r', parent=dpg.last_item()
)
dpg.set_axis_limits(item, -180, 180)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Orientation data in z axis direction.
dpg.add_line_series(
x_axis, initial_data, label='yaw',
tag=f'ori{i}_{j}y', parent=dpg.last_item()
)
dpg.set_axis_limits(item, -180, 180)
with dpg.table_row():
# Plot for gyroscope data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_gyr',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Gyroscope data in x axis direction.
dpg.add_line_series(
x_axis, initial_data, label='x',
tag=f'gyr{i}_{j}x', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Gyroscope data in y axis direction.
dpg.add_line_series(
x_axis, initial_data, label='y',
tag=f'gyr{i}_{j}y', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Gyroscope data in z axis direction.
dpg.add_line_series(
x_axis, initial_data, label='z',
tag=f'gyr{i}_{j}z', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
# Plot for rate of turn data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_rot',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Rate of turn data.
dpg.add_line_series(
x_axis, initial_data, label='rot',
tag=f'rot{i}_{j}', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
# Plot for magnetomter data.
with dpg.plot(
tag=f'dncr{i+1}_snsr{j}_mag',
label='Waiting for id...'
):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis)
item = dpg.add_plot_axis(dpg.mvYAxis)
# X-axis magnetomter data.
dpg.add_line_series(
x_axis, initial_data, label='x',
tag=f'mag{i}_{j}x', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Y-axis magnetomter data.
dpg.add_line_series(
x_axis, initial_data, label='y',
tag=f'mag{i}_{j}y', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
item = dpg.add_plot_axis(dpg.mvYAxis)
# Z-axis magnetomter data.
dpg.add_line_series(
x_axis, initial_data, label='z',
tag=f'mag{i}_{j}z', parent=dpg.last_item()
)
dpg.set_axis_limits(item, 0, 1)
# File dialog for selecting a log file for plotting.
with dpg.file_dialog(
directory_selector=False, show=False,
callback=self.file_dialog_callback,
tag='file_dialog',
width=800 ,height=500
):
dpg.add_file_extension('.txt', color=(0, 255, 0, 255))