-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
133 lines (95 loc) · 2.95 KB
/
utils.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
# Developed by Paulo Chiliguano
# Directed by Dr Roberto Alonso Trillo
# Department of Music - Hong Kong Baptist University
# 2024
import quaternion
from datetime import datetime
from os import makedirs
from os.path import expandvars, isdir
from pathlib import Path
from platform import system
from typing import ByteString, List
Array = List[int]
def bytearray_to_fusion_data(data: ByteString) -> List[float]:
"""Converts a bytearray to a list of float numbers
Parameters
----------
data : bytearray
bytes from BLE device
Returns
-------
list
A list of floats representing the timestamp and coordinates values
"""
raw_list = _int_list_from_bytearray(data)
raw_list[4:7] = _scale_gyroscope_coordinates(raw_list[4:7])
return [float(f'{i:.1f}') for i in raw_list]
def _scale_gyroscope_coordinates(data: Array) -> Array:
"""Scale gyroscope data 1/10x as it comes multiplied
Parameters
----------
data : list
X, Y, Z coordinates data (int16 each)
Returns
-------
list
A list of floats corresponding to coordinates data of the Blue ST gyroscope
"""
return [0.1*i for i in data]
def _int_list_from_bytearray(data: ByteString) -> Array:
"""Converts a bytearray to a list of signed integers
Parameters
----------
data : bytearray
bytes from BLE device
Returns
-------
list
A list of integers representing the timestamp and coordinates values
"""
step = 2
timestamp = [uint16_from_bytes(data[0:step]),]
sensor_data = [int16_from_bytes(data[i:i+step]) for i in range(step, len(data) - 1, step)]
return timestamp + sensor_data
def int16_from_bytes(data: ByteString) -> int:
"""Converts Little Indian (LE) bytes into integer
Parameters
----------
data : bytearray
bytes subset in LE order
Returns
-------
int
Equivalent int16 signed integer
"""
return int.from_bytes(data, byteorder='little', signed=True)
def uint16_from_bytes(data: ByteString) -> int:
"""Converts Little Indian (LE) bytes into unsigned integer
Parameters
----------
data : bytearray
bytes subset in LE order
Returns
-------
int
Equivalent uint16 integer
"""
return int.from_bytes(data, byteorder='little', signed=False)
def _create_folder_in_desktop(dir: str):
"""
"""
if not isdir(dir):
makedirs(dir)
def get_home_folder() -> str:
if system() == "Windows":
return expandvars(r"$HOMEPATH")
return expandvars(r"$HOME")
def log_file_path() -> str:
log_dir = Path(get_home_folder()) / "Desktop" / "Metabow Logs"
_create_folder_in_desktop(log_dir)
now = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = log_dir / f"mb_{now}.csv"
return filename
def pyquaternion_as_spherical_coords(elements):
elems = quaternion.from_float_array(elements)
return quaternion.as_spherical_coords(elems)