-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetabow_bridge.py
443 lines (374 loc) · 17.6 KB
/
metabow_bridge.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/python
# Developed by Paulo Chiliguano and KA HO Wong
# Directed by Dr Roberto Alonso Trillo
# HKBU Academy of Music
# 2024
import asyncio
import csv
import struct
import threading
import tomllib
import tkinter as tk
from datetime import datetime
from functools import partial
from pathlib import Path
from tkinter import ttk
from tkinter.messagebox import showerror, askyesno
from bleak import (
BleakClient,
BleakScanner,
)
from bleak.exc import BleakError
from pythonosc import udp_client
from gesture_model import GestureModel
from utils import bytearray_to_fusion_data, log_file_path, pyquaternion_as_spherical_coords
class FusionThread(threading.Thread):
def __init__(self, target, args=()):
super().__init__(target=target, args=args)
self._fusion_data = None
def run(self):
self._fusion_data = self._target(self._args)
def get_fusion_data(self):
return self._fusion_data
class Window(tk.Tk):
def __init__(self, loop):
self.root = tk.Tk()
self.root.title("Metabow OSC bridge")
self.root.resizable(False, False)
self.root.protocol("WM_DELETE_WINDOW", self.on_exit)
self.loop = loop
self.port0 = tk.IntVar()
self.port0.set(8888)
self.port1 = tk.IntVar()
self.port1.set(8889)
self.ports_frame = self.create_ports_frame(self.root)
self.ports_frame.grid(column=0, row=0, padx=10, pady=10, sticky=tk.NW)
self.devices_frame = self.create_scanner_frame(self.root)
self.devices_frame.grid(column=1, row=0, padx=10, pady=10)
self.option_address = tk.StringVar()
self.option_address.set(0)
self.options_frame = self.create_options_frame(self.root)
self.options_frame.grid(column=0, row=1, padx=10, pady=10, sticky=tk.NW)
self.animation = "░▒▒▒▒▒▒▒▒▒"
self.monitoring_frame = self.create_monitoring_frame(self.root)
self.monitoring_frame.grid(column=1, row=1, padx=10, pady=10, sticky=tk.NW)
self.refresh_listbox = False
self.selected_devices_keys = []
self.is_notify_loop = False
self.is_destroyed = False
self.configuration_path = Path("metabow.toml")
try:
self.configuration_dict = tomllib.loads(self.configuration_path.read_text())
except Exception as e:
self.configuration_dict = {
'device': {
'characteristic-uuid': "6e400003-b5a3-f393-e0a9-e50e24dcca9e",
'name': "metabow"
}
}
self.characteristic_uuid = self.configuration_dict['device']['characteristic-uuid']
self.device_name = self.configuration_dict['device']['name']
self.IMU_devices = {}
self.model = GestureModel()
self._instantiate_scanner()
self.csv_header = [
'ID',
'timestamp',
'accl_x',
'accl_y',
'accl_z',
'gyro_x',
'gyro_y',
'gyro_z',
'magn_x',
'magn_y',
'magn_z',
'q_w',
'q_x',
'q_y',
'q_z',
'accl_sensor_frame_x',
'accl_sensor_frame_y',
'accl_sensor_frame_z',
'accl_derivative_x',
'accl_derivative_y',
'accl_derivative_z',
'accl_vel_x',
'accl_vel_y',
'accl_vel_z',
'accl_skewness',
'accl_tilt',
'accl_roll'
]
self.DATA_POINT_SIZE = 4 # NOTE: Corresponds to floating-point standard size in bytes
self.NORDIC_IMU_SENSOR_DATA_POINTS = 13
self.NORDIC_IMU_SENSOR_DATA_LEN = self.NORDIC_IMU_SENSOR_DATA_POINTS * self.DATA_POINT_SIZE
self.NORDIC_IMU_FLAG_SIZE = 1
self.NORDIC_IMU_PACK_FORMAT = 'f' * self.NORDIC_IMU_SENSOR_DATA_POINTS
def _instantiate_scanner(self):
try:
self.scanner = BleakScanner(self.device_detected)
except BleakError:
showerror("Error", "Bluetooth device is turned off.")
self.is_destroyed = True
def _instantiate_udp_client(self):
localhost = "127.0.0.1"
self.udp_client = udp_client.SimpleUDPClient(localhost,
self.port0.get())
self.udp_client_mirror = udp_client.SimpleUDPClient(localhost,
self.port1.get())
def on_exit(self):
if askyesno("Exit", "Do you want to quit the application?"):
self.is_destroyed = True
def create_ports_frame(self, container):
label_frame = ttk.Labelframe(container, text='UDP Ports', relief=tk.RIDGE)
label_frame.grid(row=0, column=0, sticky=tk.W)
title = ttk.Label(label_frame, text="Device Port:")
title.grid(row=0, column=0, sticky=tk.W)
self.port0_spinbox = ttk.Spinbox(label_frame,
from_=1024,
to=49151,
textvariable=self.port0,
width=10)
self.port0_spinbox.grid(row=0, column=1, sticky=tk.W)
title1 = ttk.Label(label_frame, text="Mirror Port:")
title1.grid(row=1, column=0, sticky=tk.W)
self.port1_spinbox = ttk.Spinbox(label_frame,
from_=1024,
to=49151,
textvariable=self.port1,
width=10)
self.port1_spinbox.grid(row=1, column=1, sticky=tk.W)
return label_frame
def create_options_frame(self, container):
label_frame = ttk.Labelframe(container, text='Options', relief=tk.RIDGE)
label_frame.grid(row=0, column=0, sticky=tk.W)
self.address_checkbox = ttk.Checkbutton(label_frame, text="Use Device Identifier for OSC", variable=self.option_address)
self.address_checkbox.grid(row=0, column=0, sticky=tk.W)
return label_frame
def create_monitoring_frame(self, container):
label_frame = ttk.Labelframe(container, text='Monitoring', relief=tk.RIDGE)
label_frame.grid(row=0, column=0, sticky=tk.W)
self.monitoring_label = ttk.Label(label_frame, text="")
self.monitoring_label.grid(row=0, column=0, sticky=tk.W)
self.monitoring_label.state(['disabled'])
return label_frame
def items_selected(self, event):
selected_ix = self.devices_listbox.curselection()
selected_devices_keys = [self.devices_listbox.get(i) for i in selected_ix]
self.selected_devices = [self.IMU_devices[i] for i in selected_devices_keys]
def create_scanner_frame(self, container):
label_frame = ttk.Labelframe(container, text='Devices', relief=tk.RIDGE)
label_frame.grid(row=0, column=0, sticky=tk.W)
self.start_scan_button = ttk.Button(
label_frame,
text="Scan",
width=10,
command=lambda: self.loop.create_task(self.start_scan()),
)
self.start_scan_button.grid(row=0, column=0, sticky=tk.W)
# self.start_scan_button.focus()
self.stop_scan_button = ttk.Button(
label_frame,
text="Stop Scan",
width=10,
command=lambda: self.loop.create_task(self.stop_scan()),
)
self.stop_scan_button.state(['disabled'])
self.stop_scan_button.grid(row=0, column=1, sticky=tk.W)
self.connect_button = ttk.Button(
label_frame,
text="Connect",
width=10,
command=lambda: self.loop.create_task(self.connect()),
)
self.connect_button.state(['disabled'])
self.connect_button.grid(row=0, column=2, sticky=tk.W)
self.disconnect_button = ttk.Button(
label_frame,
text="Disconnect",
width=10,
command=lambda: self.loop.create_task(self.disconnect()),
)
self.disconnect_button.state(['disabled'])
self.disconnect_button.grid(row=0, column=3, sticky=tk.W)
self.devices_listbox = tk.Listbox(label_frame, width=50, selectmode='extended')
self.devices_listbox.grid(row=1, columnspan=4, sticky=tk.W)
self.devices_listbox.insert(0, "Please press Scan...")
self.devices_listbox.bind('<<ListboxSelect>>', self.items_selected)
self.devices_listbox.config(state=tk.DISABLED)
return label_frame
async def device_detected(self, device, _):
if device.name == self.device_name:
self.IMU_devices[device.address] = device # bleak.backends.device.BLEDevice
await asyncio.sleep(1.0)
async def start_scan(self):
await self.scanner.start()
self.devices_listbox.config(state=tk.NORMAL)
self.devices_listbox.delete(0, 'end')
self.start_scan_button.state(['disabled'])
self.stop_scan_button.state(['!disabled'])
self.connect_button.state(['disabled'])
self.refresh_listbox = True
async def stop_scan(self):
self.start_scan_button.state(['!disabled'])
self.stop_scan_button.state(['disabled'])
self.connect_button.state(['!disabled'])
self.refresh_listbox = False
await self.scanner.stop()
async def connect(self):
if len(self.selected_devices) == 0:
return
self.is_notify_loop = True
self.port0_spinbox.state(['disabled'])
self.port1_spinbox.state(['disabled'])
self.address_checkbox.state(['disabled'])
self._instantiate_udp_client()
self.start_scan_button.state(['disabled'])
self.connect_button.state(['disabled'])
self.disconnect_button.state(['!disabled'])
self.devices_listbox.config(state=tk.DISABLED)
self.monitoring_label.state(['!disabled'])
self._create_csv_file()
await asyncio.gather(*(self.notify_nordic(i, device) for i, device in enumerate(self.selected_devices)))
async def notify_nordic(self, i, device):
"""
This is similar as self.notify except it has not been tested for simultaneous devices
"""
async with BleakClient(device) as client:
while not client.is_connected:
await asyncio.sleep(0.1)
if self.option_address.get() == "0":
await client.start_notify(self.characteristic_uuid, partial(self.notification_handler_for_nordic, i))
elif self.option_address.get() == "1":
await client.start_notify(self.characteristic_uuid, partial(self.notification_handler_for_nordic, str(device.address)))
while self.is_notify_loop:
await asyncio.sleep(1.0)
await client.stop_notify(self.characteristic_uuid)
async def notify(self, i, device):
async with BleakClient(device) as client:
if self.option_address.get() == "0":
await client.start_notify(self.characteristic_uuid, partial(self.notification_handler, i))
elif self.option_address.get() == "1":
await client.start_notify(self.characteristic_uuid, partial(self.notification_handler, str(device.address)))
while self.is_notify_loop:
await asyncio.sleep(1.0)
await client.stop_notify(self.characteristic_uuid)
async def disconnect(self):
self.is_notify_loop = False
self.port0_spinbox.state(['!disabled'])
self.port1_spinbox.state(['!disabled'])
self.disconnect_button.state(['disabled'])
self.start_scan_button.state(['!disabled'])
self.address_checkbox.state(['!disabled'])
self.monitoring_label.state(['disabled'])
await asyncio.sleep(1.0)
def notification_handler_for_nordic(self, device_number: int | str, sender: int, data: bytearray):
"""
Async callback for Nordic IMU
"""
data_len = len(data) # TODO: Once the value is fixed on the board, assign as a constant in the constructor
upper_limit = data_len - self.NORDIC_IMU_FLAG_SIZE
lower_limit = upper_limit - self.NORDIC_IMU_SENSOR_DATA_LEN
if data[upper_limit] == 1: # NOTE: We check the flag value
sensor_data_unpacked = struct.unpack(
self.NORDIC_IMU_PACK_FORMAT,
data[lower_limit:upper_limit]
)
address_raw = f"/{device_number}/raw"
self.udp_client.send_message(address_raw, sensor_data_unpacked)
def notification_handler(self, device_number: int | str, sender: int, data: bytearray):
"""Simple notification handler
"""
fusion_thread = FusionThread(target=bytearray_to_fusion_data,
args=data)
fusion_thread.start()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S.%f")
address_raw = f"/{device_number}/raw"
fusion_thread.join()
fusion_data = fusion_thread.get_fusion_data()
model_thread = threading.Thread(target=self.model.tick,
args=(fusion_data[1:4],
fusion_data[4:7],
fusion_data[7:10]))
model_thread.start()
self.udp_client.send_message(address_raw, fusion_data)
self.udp_client_mirror.send_message(address_raw, fusion_data)
model_thread.join()
write_thread = threading.Thread(target=self._write_log,
args=(timestamp, device_number, fusion_data, self.model))
write_thread.start()
if self.port0 != self.port1:
send_mirror_thread = threading.Thread(target=self._send_messages,
args=(device_number, self.udp_client_mirror, self.model))
send_mirror_thread.start()
self._send_messages(device_number, self.udp_client, self.model)
send_mirror_thread.join()
else:
self._send_messages(device_number, self.udp_client, self.model)
write_thread.join()
def _write_log(self, timestamp, device_number, fusion_data, model):
data = [device_number,
timestamp,
*fusion_data[1:],
*model.quaternion.elements.tolist(),
*model.movement_acceleration.tolist(),
*model.acceleration_derivative.tolist(),
*model.movement_velocity.tolist(),
model.skewness,
model.tilt,
model.roll]
with open(self.log_name, 'a', encoding='UTF8') as f:
writer = csv.writer(f)
writer.writerow(data)
def _send_messages(self, device_number, udp_client, model):
address_quaternion = f"/{device_number}/quaternion"
elements = model.quaternion.elements
quaternion = elements.tolist()
udp_client.send_message(address_quaternion, quaternion)
spherical_coords = pyquaternion_as_spherical_coords(elements)
udp_client.send_message(f"/{device_number}/spherical_coords", spherical_coords.tolist())
address_sensor_frame = f"/{device_number}/motion_acceleration/sensor_frame"
movement_accl = model.movement_acceleration.tolist()
udp_client.send_message(address_sensor_frame, movement_accl)
address_sensor_derivative = f"/{device_number}/motion_acceleration/sensor_derivative"
accl_derivative = model.acceleration_derivative.tolist()
udp_client.send_message(address_sensor_derivative, accl_derivative)
address_sensor_velocity = f"/{device_number}/motion_acceleration/sensor_velocity"
movement_vel = model.movement_velocity.tolist()
udp_client.send_message(address_sensor_velocity, movement_vel)
address_sensor_skewness = f"/{device_number}/motion_acceleration/skewness"
skewness = model.skewness
udp_client.send_message(address_sensor_skewness, skewness)
address_sensor_tilt = f"/{device_number}/motion_acceleration/tilt"
tilt = model.tilt
udp_client.send_message(address_sensor_tilt, tilt)
address_sensor_roll = f"/{device_number}/motion_acceleration/roll"
roll = model.roll
udp_client.send_message(address_sensor_roll, roll)
def populate_devices(self):
if len(self.IMU_devices):
self.devices_listbox.delete(0, 'end')
for k, v in self.IMU_devices.items():
self.devices_listbox.insert('end', k)
def _create_csv_file(self):
self.log_name = log_file_path()
with open(self.log_name, 'w', encoding='UTF8') as f:
writer = csv.writer(f)
writer.writerow(self.csv_header)
async def show(self):
while not self.is_destroyed:
if self.refresh_listbox:
self.populate_devices()
if self.is_notify_loop:
self.monitoring_label["text"] = self.animation
self.animation = self.animation[-1] + self.animation[:-1]
self.root.update()
await asyncio.sleep(0.1)
self.root.destroy()
class App:
async def metabow(self):
self.window = Window(asyncio.get_running_loop())
await self.window.show()
if __name__ == '__main__':
asyncio.run(App().metabow())