dBHz commited on
Commit
784266f
·
verified ·
1 Parent(s): 1de88a2

Upload core/imu_data.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. core/imu_data.py +96 -0
core/imu_data.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import numpy as np
5
+ from multipledispatch import dispatch
6
+
7
+ class IMUData():
8
+ @dispatch(float, float, float, float, float, float, int)
9
+ def __init__(self, acc_x:float, acc_y:float, acc_z:float,
10
+ gyr_x:float, gyr_y:float, gyr_z:float, timestamp:int):
11
+ self.acc_x = acc_x
12
+ self.acc_y = acc_y
13
+ self.acc_z = acc_z
14
+ self.gyr_x = gyr_x
15
+ self.gyr_y = gyr_y
16
+ self.gyr_z = gyr_z
17
+ self.timestamp = timestamp
18
+ self.acc_np = np.array([self.acc_x, self.acc_y, self.acc_z])
19
+ self.gyr_np = np.array([self.gyr_x, self.gyr_y, self.gyr_z])
20
+ self.imu_np = np.concatenate((self.acc_np, self.gyr_np), axis=0)
21
+ self.plane_directions = np.array([
22
+ [-0.23709712, 0.20552186, -0.93578157],
23
+ [-0.94889871, 0.04797022, 0.28491208],
24
+ [-0.0617075, 0.95126743, 0.29782995],
25
+ [ 0.99204434, -0.06982192, 0.00740044]
26
+ ])
27
+
28
+ @dispatch(dict)
29
+ def __init__(self, data:dict):
30
+ self.__init__(data['acc_x'], data['acc_y'], data['acc_z'],
31
+ data['gyr_x'], data['gyr_y'], data['gyr_z'], data['timestamp'])
32
+
33
+ def __getitem__(self, index):
34
+ return [self.acc_x, self.acc_y, self.acc_z, self.gyr_x, self.gyr_y, self.gyr_z][index]
35
+
36
+ def __str__(self):
37
+ return 'Acc [x: {:.2f} y: {:.2f} z: {:.2f}] Gyr[x: {:.2f} y: {:.2f} z: {:.2f}] Timestamp {}'.format(
38
+ self.acc_x, self.acc_y, self.acc_z, self.gyr_x, self.gyr_y, self.gyr_z, self.timestamp
39
+ )
40
+
41
+ @property
42
+ def gyr_norm(self):
43
+ return math.sqrt(self.gyr_x * self.gyr_x + self.gyr_y * self.gyr_y + self.gyr_z * self.gyr_z)
44
+
45
+ @property
46
+ def acc_norm(self):
47
+ return math.sqrt(self.acc_x * self.acc_x + self.acc_y * self.acc_y + self.acc_z * self.acc_z)
48
+
49
+ def scale(self) -> IMUData:
50
+ return IMUData(self.acc_x / 9.8, -self.acc_y / 9.8, -self.acc_z / 9.8,
51
+ self.gyr_x / math.pi * 180, -self.gyr_y / math.pi * 180, -self.gyr_z / math.pi * 180, self.timestamp)
52
+
53
+ def direction(self) -> int: # TODO: -> str
54
+ for i in range(len(self.plane_directions)):
55
+ if np.dot(self.acc_np / self.acc_norm, self.plane_directions[i]) >= math.sqrt(2) / 2:
56
+ return i
57
+ return -1
58
+
59
+ def to_numpy(self):
60
+ return np.array([self.acc_x, self.acc_y, self.acc_z, self.gyr_x, self.gyr_y, self.gyr_z])
61
+
62
+ def to_numpy_with_timestamp(self):
63
+ return np.array([self.acc_x, self.acc_y, self.acc_z, self.gyr_x, self.gyr_y, self.gyr_z, self.timestamp], dtype=np.float64)
64
+
65
+ def assigned_by(self, data):
66
+ # remain timestamp
67
+ self.acc_x = data.acc_x
68
+ self.acc_y = data.acc_y
69
+ self.acc_z = data.acc_z
70
+ self.gyr_x = data.gyr_x
71
+ self.gyr_y = data.gyr_y
72
+ self.gyr_z = data.gyr_z
73
+ self.acc_np = np.array([self.acc_x, self.acc_y, self.acc_z])
74
+ self.gyr_np = np.array([self.gyr_x, self.gyr_y, self.gyr_z])
75
+ self.imu_np = np.concatenate((self.acc_np, self.gyr_np), axis=0)
76
+
77
+
78
+ class IMUDataGroup():
79
+ def __init__(self, data:list[IMUData]):
80
+ self.data = data
81
+
82
+ def __getitem__(self, index) -> IMUData:
83
+ return self.data[index]
84
+
85
+ def __str__(self):
86
+ return '\n'.join(map(str, self.data)) + '\n'
87
+
88
+ def scale(self) -> IMUDataGroup:
89
+ return IMUDataGroup([x.scale() for x in self.data])
90
+
91
+ def to_numpy(self):
92
+ return np.array([x[j] for x in self.data for j in range(6)])
93
+
94
+ @property
95
+ def timestamp(self):
96
+ return self.data[0].timestamp