forked from ElvinC/gyroflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyro_integrator.py
563 lines (371 loc) · 18.8 KB
/
gyro_integrator.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
gyrointegrator
This module uses gyroscope data to compute quaternion orientations over time
"""
import numpy as np
import quaternion as quat
class GyroIntegrator:
def __init__(self, input_data, time_scaling=1, gyro_scaling=1, zero_out_time=True, initial_orientation=None, acc_data=None):
"""Initialize instance of gyroIntegrator for getting orientation from gyro data
Args:
input_data (numpy.ndarray): Nx4 array, where each row is [time, gyroX,gyroY,gyroZ]
time_scaling (int, optional): time * time_scaling should give time in second. Defaults to 1.
gyro_scaling (int, optional): gyro<xyz> * gyro_scaling should give angular velocity in rad/s. Defaults to 1.
zero_out_time (bool, optional): Always start time at 0 in the output data. Defaults to True.
initial_orientation (float[4]): Quaternion representing the starting orientation, Defaults to [1, 0.0001, 0.0001, 0.0001].
acc_data (numpy.ndarray): Nx4 array, where each row is [time, accX, accY, accZ]. TODO: Use this in orientation determination
"""
self.data = np.copy(input_data)
# scale input data
self.data[:,0] *= time_scaling
self.data[:,1:4] *= gyro_scaling
# zero out timestamps
if zero_out_time:
self.data[:,0] -= self.data[0,0]
self.num_data_points = self.data.shape[0]
# initial orientation quaternion
if type(initial_orientation) != type(None):
self.orientation = np.array(initial_orientation)
else:
self.orientation = np.array([1, 0.0001, 0.0001, 0.0001])
# Variables to save integration data
self.orientation_list = None
self.time_list = None
# IMU reference vectors
self.imuRefX = quat.vector(1,0,0)
self.imuRefY = quat.vector(0,1,0)
self.imuRefY = quat.vector(0,0,1)
self.already_integrated = False
def integrate_all(self):
"""go through each gyro sample and integrate to find orientation
Returns:
(np.ndarray, np.ndarray): tuple (time_list, quaternion orientation array)
"""
if self.already_integrated:
return (self.time_list, self.orientation_list)
# temp lists to save data
temp_orientation_list = []
temp_time_list = []
for i in range(self.num_data_points):
# angular velocity vector
omega = self.data[i][1:]
# get current and adjecent times
last_time = self.data[i-1][0] if i > 0 else self.data[i][0]
this_time = self.data[i][0]
next_time = self.data[i+1][0] if i < self.num_data_points - 1 else self.data[i][0]
# symmetrical dt calculation. Should give slightly better results when missing data
delta_time = (next_time - last_time)/2
# Only calculate if angular velocity is present
if np.any(omega):
# calculate rotation quaternion
delta_q = self.rate_to_quat(omega, delta_time)
# rotate orientation by this quaternion
self.orientation = quat.quaternion_multiply(self.orientation, delta_q) # Maybe change order
self.orientation = quat.normalize(self.orientation)
temp_orientation_list.append(np.copy(self.orientation))
temp_time_list.append(this_time)
self.orientation_list = np.array(temp_orientation_list)
self.time_list = np.array(temp_time_list)
self.already_integrated = True
return (self.time_list, self.orientation_list)
def get_orientations(self):
"""Get the processed quaternion orientations
Returns:
(np.ndarray, np.ndarray): tuple (time_list, quaternion orientation array)
"""
if self.already_integrated:
return (self.time_list, self.orientation_list)
return None
def get_smoothed_orientation(self, smooth = 0.94):
smothness = smooth**(1/6)
smoothed_orientation = np.zeros(self.orientation_list.shape)
value = self.orientation_list[0,:]
for i in range(self.num_data_points):
value = quat.slerp(value, self.orientation_list[i,:],[1-smothness])[0]
smoothed_orientation[i] = value
# reverse pass
smoothed_orientation2 = np.zeros(self.orientation_list.shape)
value2 = smoothed_orientation[-1,:]
for i in range(self.num_data_points-1, -1, -1):
value2 = quat.slerp(value2, smoothed_orientation[i,:],[(1-smothness)])[0]
smoothed_orientation2[i] = value2
# Test rotation lock (doesn't work)
#if test:
# from scipy.spatial.transform import Rotation
# for i in range(self.num_data_points):
# quat = smoothed_orientation2[i,:]
# eul = Rotation([quat[1], quat[2], quat[3], quat[0]]).as_euler("xyz")
# new_quat = Rotation.from_euler('xyz', [eul[0], eul[1], np.pi]).as_quat()
# smoothed_orientation2[i,:] = [new_quat[3], new_quat[0], new_quat[1], new_quat[2]]
return (self.time_list, smoothed_orientation2)
def get_stabilize_transform(self,smooth=0.94):
time_list, smoothed_orientation = self.get_smoothed_orientation(smooth)
# rotations that'll stabilize the camera
stab_rotations = np.zeros(self.orientation_list.shape)
for i in range(self.num_data_points):
# rotation quaternion from smooth motion -> raw motion to counteract it
stab_rotations[i,:] = quat.rot_between(smoothed_orientation[i],self.orientation_list[i])
return (self.time_list, stab_rotations)
def get_interpolated_stab_transform(self,smooth, start=0, interval=1/29.97):
time_list, smoothed_orientation = self.get_stabilize_transform(smooth)
time = start
out_times = []
slerped_rotations = []
while time < 0:
slerped_rotations.append(smoothed_orientation[0])
out_times.append(time)
time += interval
while time_list[0] >= time:
slerped_rotations.append(smoothed_orientation[0])
out_times.append(time)
time += interval
for i in range(len(time_list)-1):
if time_list[i] <= time < time_list[i+1]:
# interpolate between two quaternions
weight = (time - time_list[i])/(time_list[i+1]-time_list[i])
slerped_rotations.append(quat.slerp(smoothed_orientation[i],smoothed_orientation[i+1],[weight]))
out_times.append(time)
time += interval
return (out_times, slerped_rotations)
def get_raw_data(self, axis):
"""get a column of the raw data. Either time or gyro.
Args:
axis (string|int): Column index or keyword(t,x,y,z)
Returns:
numpy.ndarray: The selected column as numpy matrix.
"""
idx = axis if type(axis) == int else {
"t": 0,
"x": 1,
"y": 2,
"z": 3,
"xyz": slice(1,4)
}[axis]
return np.copy(self.data[:,idx])
def rate_to_quat(self, omega, dt):
"""Rotation quaternion from gyroscope sample
Args:
omega (numpy.ndarray): angular velocity vector [x,y,z]. Same as scaled gyro sample in rad/s.
dt (float): Time delta between gyro samples for angle integration.
Returns:
numpy.ndarray: Rotation quaternion corresponding to orientation change
"""
# https://stackoverflow.com/questions/24197182/efficient-quaternion-angular-velocity/24201879#24201879
# no idea how it fully works, but it does
ha = omega * dt * 0.5
l = np.sqrt(ha.dot(ha))
if l > 1.0e-12:
ha *= np.sin(l) / l
q0 = np.cos(l)
q1 = ha[0]
q2 = ha[1]
q3 = ha[2]
return quat.normalize(quat.quaternion(q0,q1,q2,q3))
else:
return quat.quaternion(1,0,0,0)
class FrameRotationIntegrator(GyroIntegrator):
def __init__(self, input_data, initial_orientation=None):
"""Initialize instance of FrameRotationIntegrator for getting orientation from frame change data
Args:
input_data (numpy.ndarray): Nx4 array, where each row is [frame num, gyroX,gyroY,gyroZ]
initial_orientation (float[4]): Quaternion representing the starting orientation, Defaults to [1, 0.0001, 0.0001, 0.0001].
"""
self.data = np.copy(input_data)
self.num_data_points = self.data.shape[0]
# initial orientation quaternion
if type(initial_orientation) != type(None):
self.orientation = np.array(initial_orientation)
else:
self.orientation = np.array([1, 0.0001, 0.0001, 0.0001])
# Variables to save integration data
self.orientation_list = None
self.time_list = None
# IMU reference vectors
self.imuRefX = quat.vector(1,0,0)
self.imuRefY = quat.vector(0,1,0)
self.imuRefY = quat.vector(0,0,1)
self.already_integrated = False
def integrate_all(self):
"""go through each sample and integrate to find orientation. Assumes sample N contains change between N and N-1
Returns:
(np.ndarray, np.ndarray): tuple (time_list, quaternion orientation array)
"""
if self.already_integrated:
return (self.time_list, self.orientation_list)
# temp lists to save data
temp_orientation_list = []
temp_time_list = []
temp_orientation_list.append(np.copy(self.orientation))
temp_time_list.append(self.data[0][0] - 1)
for i in range(self.num_data_points):
# angular velocity vector
omega = self.data[i][1:]
# get current time
this_time = self.data[i][0]
# symmetrical dt calculation. Should give slightly better results when missing data
delta_time = 1 # frame
# Only calculate if angular velocity is present
if np.any(omega):
# calculate rotation quaternion
delta_q = self.rate_to_quat(omega, delta_time)
# rotate orientation by this quaternion
self.orientation = quat.quaternion_multiply(self.orientation, delta_q) # Maybe change order
self.orientation = quat.normalize(self.orientation)
temp_orientation_list.append(np.copy(self.orientation))
temp_time_list.append(this_time)
self.orientation_list = np.array(temp_orientation_list)
self.time_list = np.array(temp_time_list)
self.already_integrated = True
return (self.time_list, self.orientation_list)
def integrate_complementary(self):
"""
TODO: Implement this
"""
# Useful ressource: https://josephmalloch.wordpress.com/portfolio/imu-sensor-fusion/
class EulerIntegrator:
def __init__(self, input_data, time_scaling=1, gyro_scaling=1, zero_out_time=True, acc_data=None):
"""Initialize instance of eulerintegrator for getting a faux orientation from gyro data (not true orientation) easier xyz stabilization
Args:
input_data (numpy.ndarray): Nx4 array, where each row is [time, gyroX,gyroY,gyroZ]
time_scaling (int, optional): time * time_scaling should give time in second. Defaults to 1.
gyro_scaling (int, optional): gyro<xyz> * gyro_scaling should give angular velocity in rad/s. Defaults to 1.
zero_out_time (bool, optional): Always start time at 0 in the output data. Defaults to True.
initial_orientation (float[4]): Quaternion representing the starting orientation, Defaults to [1, 0.0001, 0.0001, 0.0001].
acc_data (numpy.ndarray): Nx4 array, where each row is [time, accX, accY, accZ]. TODO: Use this in orientation determination
"""
self.data = np.copy(input_data)
# scale input data
self.data[:,0] *= time_scaling
self.data[:,1:4] *= gyro_scaling
# zero out timestamps
if zero_out_time:
self.data[:,0] -= self.data[0,0]
self.num_data_points = self.data.shape[0]
# Variables to save integration data
self.euler_orientation_list = None
self.time_list = None
self.already_integrated = False
def integrate_all(self):
"""go through each gyro sample and integrate to find orientation
Returns:
(np.ndarray, np.ndarray): tuple (time_list, quaternion orientation array)
"""
if self.already_integrated:
return (self.time_list, self.orientation_list)
# temp lists to save data
temp_orientation_list = []
temp_time_list = []
euler_orientation = np.array([0, 0, 0])
for i in range(self.num_data_points):
# angular velocity vector
omega = self.data[i][1:]
# get current and adjecent times
last_time = self.data[i-1][0] if i > 0 else self.data[i][0]
this_time = self.data[i][0]
next_time = self.data[i+1][0] if i < self.num_data_points - 1 else self.data[i][0]
# symmetrical dt calculation. Should give slightly better results when missing data
delta_time = (next_time - last_time)/2
# Only calculate if angular velocity is present
if np.any(omega):
euler_orientation += omega * delta_time
temp_orientation_list.append(np.copy(euler_orientation))
temp_time_list.append(this_time)
self.euler_orientation_list = np.array(temp_orientation_list)
self.time_list = np.array(temp_time_list)
self.already_integrated = True
return (self.time_list, self.orientation_list)
def get_orientations(self):
"""Get the processed quaternion orientations
Returns:
(np.ndarray, np.ndarray): tuple (time_list, quaternion orientation array)
"""
if self.already_integrated:
return (self.time_list, self.orientation_list)
return None
def get_smoothed_orientation(self, smooth = 0.94):
smothness = smooth**(1/6)
smoothed_orientation = np.zeros(self.orientation_list.shape)
value = self.orientation_list[0,:]
for i in range(self.num_data_points):
value = quat.slerp(value, self.orientation_list[i,:],[1-smothness])[0]
smoothed_orientation[i] = value
# reverse pass
smoothed_orientation2 = np.zeros(self.orientation_list.shape)
value2 = smoothed_orientation[-1,:]
for i in range(self.num_data_points-1, -1, -1):
value2 = quat.slerp(value2, smoothed_orientation[i,:],[(1-smothness)])[0]
smoothed_orientation2[i] = value2
# Test rotation lock (doesn't work)
#if test:
# from scipy.spatial.transform import Rotation
# for i in range(self.num_data_points):
# quat = smoothed_orientation2[i,:]
# eul = Rotation([quat[1], quat[2], quat[3], quat[0]]).as_euler("xyz")
# new_quat = Rotation.from_euler('xyz', [eul[0], eul[1], np.pi]).as_quat()
# smoothed_orientation2[i,:] = [new_quat[3], new_quat[0], new_quat[1], new_quat[2]]
return (self.time_list, smoothed_orientation2)
def get_stabilize_transform(self,smooth=0.94):
time_list, smoothed_orientation = self.get_smoothed_orientation(smooth)
# rotations that'll stabilize the camera
stab_rotations = np.zeros(self.orientation_list.shape)
for i in range(self.num_data_points):
# rotation quaternion from smooth motion -> raw motion to counteract it
stab_rotations[i,:] = quat.rot_between(smoothed_orientation[i],self.orientation_list[i])
return (self.time_list, stab_rotations)
def get_interpolated_stab_transform(self,smooth, start=0, interval=1/29.97):
time_list, smoothed_orientation = self.get_stabilize_transform(smooth)
time = start
out_times = []
slerped_rotations = []
while time < 0:
slerped_rotations.append(smoothed_orientation[0])
out_times.append(time)
time += interval
while time_list[0] >= time:
slerped_rotations.append(smoothed_orientation[0])
out_times.append(time)
time += interval
for i in range(len(time_list)-1):
if time_list[i] <= time < time_list[i+1]:
# interpolate between two quaternions
weight = (time - time_list[i])/(time_list[i+1]-time_list[i])
slerped_rotations.append(quat.slerp(smoothed_orientation[i],smoothed_orientation[i+1],[weight]))
out_times.append(time)
time += interval
return (out_times, slerped_rotations)
def get_raw_data(self, axis):
"""get a column of the raw data. Either time or gyro.
Args:
axis (string|int): Column index or keyword(t,x,y,z)
Returns:
numpy.ndarray: The selected column as numpy matrix.
"""
idx = axis if type(axis) == int else {
"t": 0,
"x": 1,
"y": 2,
"z": 3,
"xyz": slice(1,4)
}[axis]
return np.copy(self.data[:,idx])
def rate_to_quat(self, omega, dt):
"""Rotation quaternion from gyroscope sample
Args:
omega (numpy.ndarray): angular velocity vector [x,y,z]. Same as scaled gyro sample in rad/s.
dt (float): Time delta between gyro samples for angle integration.
Returns:
numpy.ndarray: Rotation quaternion corresponding to orientation change
"""
# https://stackoverflow.com/questions/24197182/efficient-quaternion-angular-velocity/24201879#24201879
# no idea how it fully works, but it does
ha = omega * dt * 0.5
l = np.sqrt(ha.dot(ha))
if l > 1.0e-12:
ha *= np.sin(l) / l
q0 = np.cos(l)
q1 = ha[0]
q2 = ha[1]
q3 = ha[2]
return quat.normalize(quat.quaternion(q0,q1,q2,q3))
else:
return quat.quaternion(1,0,0,0)