-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathimu.py
More file actions
606 lines (508 loc) · 21.9 KB
/
imu.py
File metadata and controls
606 lines (508 loc) · 21.9 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# LSM6DSO 3D accelerometer and 3D gyroscope seneor micropython drive
# ver: 1.0
# License: MIT
# Author: shaoziyang (shaoziyang@micropython.org.cn)
# v1.0 2019.7
from .imu_defs import *
from uctypes import struct, addressof
from machine import I2C, Pin, Timer, disable_irq, enable_irq
import time, math
class IMU():
_DEFAULT_IMU_INSTANCE = None
@classmethod
def get_default_imu(cls):
"""
Get the default XRP v2 IMU instance. This is a singleton, so only one instance of the drivetrain will ever exist.
"""
if cls._DEFAULT_IMU_INSTANCE is None:
cls._DEFAULT_IMU_INSTANCE = cls(
scl_pin=19,
sda_pin=18,
addr=LSM_ADDR_PRIMARY
)
cls._DEFAULT_IMU_INSTANCE.calibrate()
return cls._DEFAULT_IMU_INSTANCE
def __init__(self, scl_pin: int, sda_pin: int, addr):
# I2C values
self.i2c = I2C(id=1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=400000)
self.addr = addr
# Initialize member variables
self._reset_member_variables()
# Transmit and recieve buffers
self.tb = bytearray(1)
self.rb = bytearray(1)
# Copies of registers. Bytes and structs share the same memory
# addresses, so changing one changes the other
self.reg_ctrl1_xl_byte = bytearray(1)
self.reg_ctrl2_g_byte = bytearray(1)
self.reg_ctrl3_c_byte = bytearray(1)
self.reg_ctrl1_xl_bits = struct(addressof(self.reg_ctrl1_xl_byte), LSM_REG_LAYOUT_CTRL1_XL)
self.reg_ctrl2_g_bits = struct(addressof(self.reg_ctrl2_g_byte), LSM_REG_LAYOUT_CTRL2_G)
self.reg_ctrl3_c_bits = struct(addressof(self.reg_ctrl3_c_byte), LSM_REG_LAYOUT_CTRL3_C)
# Create timer
self.update_timer = Timer(-1)
# Check if the IMU is connected
if not self.is_connected():
# TODO - do somehting intelligent here
pass
# Reset sensor to clear any previous configuration
self.reset()
# Enable block data update
self._set_bdu()
# Set default scale for each sensor
self.acc_scale('16g')
self.gyro_scale('2000dps')
# Set default rate for each sensor
self.acc_rate('208Hz')
self.gyro_rate('208Hz')
"""
The following are private helper methods to read and write registers, as well as to convert the read values to the correct unit.
"""
def _reset_member_variables(self):
# Vector of IMU measurements
self.irq_v = [[0, 0, 0], [0, 0, 0]]
# Sensor offsets
self.gyro_offsets = [0,0,0]
self.acc_offsets = [0,0,0]
# Scale factors when ranges are changed
self._acc_scale_factor = 1
self._gyro_scale_factor = 1
# Angles
self.pitch = 0
self.yaw = 0
self.roll = 0
self.pitchComputed = True
self.yawComputed = True
self.rollComputed = True
# Madgwick values
self.beta = 0.1
self.q = [1, 0, 0, 0]
def _int16(self, d):
return d if d < 0x8000 else d - 0x10000
def _setreg(self, reg, dat):
self.tb[0] = dat
self.i2c.writeto_mem(self.addr, reg, self.tb)
def _getreg(self, reg):
self.i2c.readfrom_mem_into(self.addr, reg, self.rb)
return self.rb[0]
def _getregs(self, reg, num_bytes):
rx_buf = bytearray(num_bytes)
self.i2c.readfrom_mem_into(self.addr, reg, rx_buf)
return rx_buf
def _get2reg(self, reg):
return self._getreg(reg) + self._getreg(reg+1) * 256
def _r_w_reg(self, reg, dat, mask):
self._getreg(reg)
self.rb[0] = (self.rb[0] & mask) | dat
self._setreg(reg, self.rb[0])
def _set_bdu(self, bdu = True):
"""
Sets Block Data Update bit
"""
self.reg_ctrl3_c_byte[0] = self._getreg(LSM_REG_CTRL3_C)
self.reg_ctrl3_c_bits.BDU = bdu
self._setreg(LSM_REG_CTRL3_C, self.reg_ctrl3_c_byte[0])
def _set_if_inc(self, if_inc = True):
"""
Sets InterFace INCrement bit
"""
self.reg_ctrl3_c_byte[0] = self._getreg(LSM_REG_CTRL3_C)
self.reg_ctrl3_c_bits.IF_INC = if_inc
self._setreg(LSM_REG_CTRL3_C, self.reg_ctrl3_c_byte[0])
def _raw_to_mg(self, raw):
return self._int16((raw[1] << 8) | raw[0]) * LSM_MG_PER_LSB_2G * self._acc_scale_factor
def _raw_to_mdps(self, raw):
return self._int16((raw[1] << 8) | raw[0]) * LSM_MDPS_PER_LSB_125DPS * self._gyro_scale_factor
"""
Public facing API Methods
"""
def is_connected(self):
"""
Checks whether the IMU is connected
:return: True if WHO_AM_I value is correct, otherwise False
:rtype: bool
"""
who_am_i = self._getreg(LSM_REG_WHO_AM_I)
return who_am_i == LSM_WHO_AM_I_VALUE
def reset(self, wait_for_reset = True, wait_timeout_ms = 100):
"""
Resets the IMU, and restores all registers to their default values
:param wait_for_reset: Whether to wait for reset to complete
:type wait_for_reset: bool
:param wait_timeout_ms: Timeout in milliseconds when waiting for reset
:type wait_timeout_ms: int
:return: False if timeout occurred, otherwise True
:rtype: bool
"""
# Stop timer
self._stop_timer()
# Reset member variables
self._reset_member_variables()
# Set BOOT and SW_RESET bits
self.reg_ctrl3_c_byte[0] = self._getreg(LSM_REG_CTRL3_C)
self.reg_ctrl3_c_bits.BOOT = 1
self.reg_ctrl3_c_bits.SW_RESET = 1
self._setreg(LSM_REG_CTRL3_C, self.reg_ctrl3_c_byte[0])
# Wait for reset to complete, if requested
if wait_for_reset:
# Loop with timeout
t0 = time.ticks_ms()
while time.ticks_ms() < (t0 + wait_timeout_ms):
# Check if register has returned to default value (0x04)
self.reg_ctrl3_c_byte[0] = self._getreg(LSM_REG_CTRL3_C)
if self.reg_ctrl3_c_byte[0] == 0x04:
return True
# Timeout occurred
return False
else:
return True
def get_acc_x(self):
"""
:return: The current reading for the accelerometer's X-axis, in mg
:rtype: int
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTX_L_A, 2)
# Convert raw data to mg's
return self._raw_to_mg(raw_bytes[0:2]) - self.acc_offsets[0]
def get_acc_y(self):
"""
:return: The current reading for the accelerometer's Y-axis, in mg
:rtype: int
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTY_L_A, 2)
# Convert raw data to mg's
return self._raw_to_mg(raw_bytes[0:2]) - self.acc_offsets[1]
def get_acc_z(self):
"""
:return: The current reading for the accelerometer's Z-axis, in mg
:rtype: int
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTZ_L_A, 2)
# Convert raw data to mg's
return self._raw_to_mg(raw_bytes[0:2]) - self.acc_offsets[2]
def get_acc_rates(self):
"""
:return: the list of readings from the Accelerometer, in mg. The order of the values is x, y, z.
:rtype: list<int>
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTX_L_A, 6)
# Convert raw data to mg's
self.irq_v[0][0] = self._raw_to_mg(raw_bytes[0:2]) - self.acc_offsets[0]
self.irq_v[0][1] = self._raw_to_mg(raw_bytes[2:4]) - self.acc_offsets[1]
self.irq_v[0][2] = self._raw_to_mg(raw_bytes[4:6]) - self.acc_offsets[2]
return self.irq_v[0]
def get_gyro_x_rate(self):
"""
Individual axis read for the Gyroscope's X-axis, in mg
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTX_L_G, 2)
# Convert raw data to mdps
return self._raw_to_mdps(raw_bytes[0:2]) - self.gyro_offsets[0]
def get_gyro_y_rate(self):
"""
Individual axis read for the Gyroscope's Y-axis, in mg
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTY_L_G, 2)
# Convert raw data to mdps
return self._raw_to_mdps(raw_bytes[0:2]) - self.gyro_offsets[1]
def get_gyro_z_rate(self):
"""
Individual axis read for the Gyroscope's Z-axis, in mg
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTZ_L_G, 2)
# Convert raw data to mdps
return self._raw_to_mdps(raw_bytes[0:2]) - self.gyro_offsets[2]
def get_gyro_rates(self):
"""
Retrieves the array of readings from the Gyroscope, in mdps
The order of the values is x, y, z.
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTX_L_G, 6)
# Convert raw data to mdps
self.irq_v[1][0] = self._raw_to_mdps(raw_bytes[0:2]) - self.gyro_offsets[0]
self.irq_v[1][1] = self._raw_to_mdps(raw_bytes[2:4]) - self.gyro_offsets[1]
self.irq_v[1][2] = self._raw_to_mdps(raw_bytes[4:6]) - self.gyro_offsets[2]
return self.irq_v[1]
def get_acc_gyro_rates(self):
"""
Get the accelerometer and gyroscope values in mg and mdps in the form of a 2D array.
The first row is the acceleration values, the second row is the gyro values.
The order of the values is x, y, z.
"""
# Burst read data registers
raw_bytes = self._getregs(LSM_REG_OUTX_L_G, 12)
# Convert raw data to mg's and mdps
self.irq_v[0][0] = self._raw_to_mg(raw_bytes[6:8]) - self.acc_offsets[0]
self.irq_v[0][1] = self._raw_to_mg(raw_bytes[8:10]) - self.acc_offsets[1]
self.irq_v[0][2] = self._raw_to_mg(raw_bytes[10:12]) - self.acc_offsets[2]
self.irq_v[1][0] = self._raw_to_mdps(raw_bytes[0:2]) - self.gyro_offsets[0]
self.irq_v[1][1] = self._raw_to_mdps(raw_bytes[2:4]) - self.gyro_offsets[1]
self.irq_v[1][2] = self._raw_to_mdps(raw_bytes[4:6]) - self.gyro_offsets[2]
return self.irq_v
def get_pitch(self):
"""
Get the pitch of the IMU in degrees. Unbounded in range
:return: The pitch of the IMU in degrees
:rtype: float
"""
if self.pitchComputed == False:
self.pitch = math.asin(-2 * (self.q[1]*self.q[3] - self.q[0]*self.q[2])) * 57.23
self.pitchComputed = True
return self.pitch
def get_yaw(self):
"""
Get the yaw (heading) of the IMU in degrees. Unbounded in range
:return: The yaw (heading) of the IMU in degrees
:rtype: float
"""
if self.yawComputed == False:
self.yaw = math.atan2(self.q[1]*self.q[2] + self.q[0]*self.q[3], 0.5 - self.q[2]*self.q[2] - self.q[3]*self.q[3]) * 57.23
self.yawComputed = True
return self.yaw
def get_heading(self):
"""
Get's the heading of the IMU, but bounded between [0, 360)
:return: The heading of the IMU in degrees, bound between [0, 360)
:rtype: float
"""
return self.get_yaw() % 360
def get_roll(self):
"""
Get the roll of the IMU in degrees. Unbounded in range
:return: The roll of the IMU in degrees
:rtype: float
"""
if self.rollComputed == False:
self.roll = math.atan2(self.q[0]*self.q[1] + self.q[2]*self.q[3], 0.5 - self.q[1]*self.q[1] - self.q[2]*self.q[2]) * 57.23
self.rollComputed = True
return self.roll
def reset_angles(self):
"""
Reset all angles to 0
"""
self.q = [1, 0, 0, 0]
def temperature(self):
"""
Read the temperature of the LSM6DSO in degrees Celsius
:return: The temperature of the LSM6DSO in degrees Celsius
:rtype: float
"""
# The LSM6DSO's temperature can be read from the OUT_TEMP_L register
# We use OUT_TEMP_L+1 if OUT_TEMP_L cannot be read
try:
return self._int16(self._get2reg(LSM_REG_OUT_TEMP_L))/256 + 25
except MemoryError:
return self._temperature_irq()
def _temperature_irq(self):
# Helper function for temperature() to read the alternate temperature register
self._getreg(LSM_REG_OUT_TEMP_L+1)
if self.rb[0] & 0x80:
self.rb[0] -= 256
return self.rb[0] + 25
def acc_scale(self, value=None):
"""
Set the accelerometer scale in g. The scale can be:
'2g', '4g', '8g', or '16g'
Pass in no parameters to retrieve the current value
"""
# Get register value
self.reg_ctrl1_xl_byte[0] = self._getreg(LSM_REG_CTRL1_XL)
# Check if the provided value is in the dictionary
if value not in LSM_ACCEL_FS:
# Return string representation of this value
index = list(LSM_ACCEL_FS.values()).index(self.reg_ctrl1_xl_bits.FS_XL)
return list(LSM_ACCEL_FS.keys())[index]
else:
# Set value as requested
self.reg_ctrl1_xl_bits.FS_XL = LSM_ACCEL_FS[value]
self._setreg(LSM_REG_CTRL1_XL, self.reg_ctrl1_xl_byte[0])
# Update scale factor for converting raw data
self._acc_scale_factor = int(value.rstrip('g')) // 2
def gyro_scale(self, value=None):
"""
Set the gyroscope scale in dps. The scale can be:
'125', '250', '500', '1000', or '2000'
Pass in no parameters to retrieve the current value
"""
# Get register value
self.reg_ctrl2_g_byte[0] = self._getreg(LSM_REG_CTRL2_G)
# Check if the provided value is in the dictionary
if value not in LSM_GYRO_FS:
# Return string representation of this value
index = list(LSM_GYRO_FS.values()).index(self.reg_ctrl2_g_bits.FS_G)
return list(LSM_GYRO_FS.keys())[index]
else:
# Set value as requested
self.reg_ctrl2_g_bits.FS_G = LSM_GYRO_FS[value]
self._setreg(LSM_REG_CTRL2_G, self.reg_ctrl2_g_byte[0])
# Update scale factor for converting raw data
self._gyro_scale_factor = int(value.rstrip('dps')) // 125
def acc_rate(self, value=None):
"""
Set the accelerometer rate in Hz. The rate can be:
'0Hz', '12.5Hz', '26Hz', '52Hz', '104Hz', '208Hz', '416Hz', '833Hz', '1660Hz', '3330Hz', '6660Hz'
Pass in no parameters to retrieve the current value
"""
# Get register value
self.reg_ctrl1_xl_byte[0] = self._getreg(LSM_REG_CTRL1_XL)
# Check if the provided value is in the dictionary
if value not in LSM_ODR:
# Return string representation of this value
index = list(LSM_ODR.values()).index(self.reg_ctrl1_xl_bits.ODR_XL)
return list(LSM_ODR.keys())[index]
else:
# Set value as requested
self.reg_ctrl1_xl_bits.ODR_XL = LSM_ODR[value]
self._setreg(LSM_REG_CTRL1_XL, self.reg_ctrl1_xl_byte[0])
def gyro_rate(self, value=None):
"""
Set the gyroscope rate in Hz. The rate can be:
'0Hz', '12.5Hz', '26Hz', '52Hz', '104Hz', '208Hz', '416Hz', '833Hz', '1660Hz', '3330Hz', '6660Hz'
Pass in no parameters to retrieve the current value
"""
# Get register value
self.reg_ctrl2_g_byte[0] = self._getreg(LSM_REG_CTRL2_G)
# Check if the provided value is in the dictionary
if value not in LSM_ODR:
# Return string representation of this value
index = list(LSM_ODR.values()).index(self.reg_ctrl1_xl_bits.ODR_G)
return list(LSM_ODR.keys())[index]
else:
# Set value as requested
self.reg_ctrl2_g_bits.ODR_G = LSM_ODR[value]
self._setreg(LSM_REG_CTRL2_G, self.reg_ctrl2_g_byte[0])
# Update timer frequency
self.timer_frequency = int(value.rstrip('Hz'))
self.timer_period = 1 / self.timer_frequency
self._start_timer()
def calibrate(self, calibration_time:float=1, vertical_axis:int= 2):
"""
Collect readings for [calibration_time] seconds and calibrate the IMU based on those readings
Do not move the robot during this time
Assumes the board to be parallel to the ground. Please use the vertical_axis parameter if that is not correct
:param calibration_time: The time in seconds to collect readings for
:type calibration_time: float
:param vertical_axis: The axis that is vertical. 0 for X, 1 for Y, 2 for Z
:type vertical_axis: int
"""
self._stop_timer()
self.acc_offsets = [0,0,0]
self.gyro_offsets = [0,0,0]
avg_vals = [[0,0,0],[0,0,0]]
num_vals = 0
# Wait a bit for sensor to start measuring (data registers may default to something nonsensical)
time.sleep(.1)
start_time = time.ticks_ms()
while time.ticks_ms() < start_time + calibration_time*1000:
cur_vals = self.get_acc_gyro_rates()
# Accelerometer averages
avg_vals[0][0] += cur_vals[0][0]
avg_vals[0][1] += cur_vals[0][1]
avg_vals[0][2] += cur_vals[0][2]
# Gyroscope averages
avg_vals[1][0] += cur_vals[1][0]
avg_vals[1][1] += cur_vals[1][1]
avg_vals[1][2] += cur_vals[1][2]
# Increment counter and wait for next loop
num_vals += 1
time.sleep(1 / self.timer_frequency)
# Compute averages
avg_vals[0][0] /= num_vals
avg_vals[0][1] /= num_vals
avg_vals[0][2] /= num_vals
avg_vals[1][0] /= num_vals
avg_vals[1][1] /= num_vals
avg_vals[1][2] /= num_vals
avg_vals[0][vertical_axis] -= 1000 #in mg
self.acc_offsets = avg_vals[0]
self.gyro_offsets = avg_vals[1]
self._start_timer()
def _start_timer(self):
self.update_timer.init(freq=self.timer_frequency, callback=lambda t:self._update_imu_readings())
def _stop_timer(self):
self.update_timer.deinit()
def _invSqrt(self, x):
return x ** -0.5
def _update_imu_readings(self):
# Called every tick through a callback timer
# Function converted to Python from https://github.com/arduino-libraries/MadgwickAHRS
# Get IMU measurements
cur_vals = self.get_acc_gyro_rates()
# Acceleration, from mg's to g's
ax = cur_vals[0][0] * 0.001
ay = cur_vals[0][1] * 0.001
az = cur_vals[0][2] * 0.001
# Rotation rate, from mdps to rad/sec
gx = cur_vals[1][0] * 0.0000174533
gy = cur_vals[1][1] * 0.0000174533
gz = cur_vals[1][2] * 0.0000174533
# Roll and pitch are measured about the x and y axes respectively. The
# IMU on the XRP has the x and y axes pionting to the right and forwards,
# meaning roll and pitch are swapped from what's intuitive. This axis swap
# effectively rotates the x and y axes by -90 degrees
ax, ay = (-ay, ax)
gx, gy = (-gy, gx)
# Rate of change of quaternion from gyroscope
qDot1 = 0.5 * (-self.q[1] * gx - self.q[2] * gy - self.q[3] * gz)
qDot2 = 0.5 * (self.q[0] * gx + self.q[2] * gz - self.q[3] * gy)
qDot3 = 0.5 * (self.q[0] * gy - self.q[1] * gz + self.q[3] * gx)
qDot4 = 0.5 * (self.q[0] * gz + self.q[1] * gy - self.q[2] * gx)
# Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
if(not ((ax == 0) and (ay == 0) and (az == 0))):
# Norm`alise accelerometer measurement
recipNorm = self._invSqrt(ax * ax + ay * ay + az * az)
ax *= recipNorm
ay *= recipNorm
az *= recipNorm
# Auxiliary variables to avoid repeated arithmetic
_2q0 = 2 * self.q[0]
_2q1 = 2 * self.q[1]
_2q2 = 2 * self.q[2]
_2q3 = 2 * self.q[3]
_4q0 = 4 * self.q[0]
_4q1 = 4 * self.q[1]
_4q2 = 4 * self.q[2]
_8q1 = 8 * self.q[1]
_8q2 = 8 * self.q[2]
q0q0 = self.q[0] * self.q[0]
q1q1 = self.q[1] * self.q[1]
q2q2 = self.q[2] * self.q[2]
q3q3 = self.q[3] * self.q[3]
# Gradient decent algorithm corrective step
s0 = _4q0 * q2q2 + _2q2 * ax + _4q0 * q1q1 - _2q1 * ay
s1 = _4q1 * q3q3 - _2q3 * ax + 4 * q0q0 * self.q[1] - _2q0 * ay - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * az
s2 = 4 * q0q0 * self.q[2] + _2q0 * ax + _4q2 * q3q3 - _2q3 * ay - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * az
s3 = 4 * q1q1 * self.q[3] - _2q1 * ax + 4 * q2q2 * self.q[3] - _2q2 * ay
recipNorm = self._invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3) # normalise step magnitude
s0 *= recipNorm
s1 *= recipNorm
s2 *= recipNorm
s3 *= recipNorm
# Apply feedback step
qDot1 -= self.beta * s0
qDot2 -= self.beta * s1
qDot3 -= self.beta * s2
qDot4 -= self.beta * s3
# Integrate rate of change of quaternion to yield quaternion
self.q[0] += qDot1 * self.timer_period
self.q[1] += qDot2 * self.timer_period
self.q[2] += qDot3 * self.timer_period
self.q[3] += qDot4 * self.timer_period
# Normalise quaternion
recipNorm = self._invSqrt(self.q[0] * self.q[0] + self.q[1] * self.q[1] + self.q[2] * self.q[2] + self.q[3] * self.q[3])
self.q[0] *= recipNorm
self.q[1] *= recipNorm
self.q[2] *= recipNorm
self.q[3] *= recipNorm
# Update RPY flags
self.pitchComputed = False
self.yawComputed = False
self.rollComputed = False