Skip to content

Commit bb5681f

Browse files
committed
Calibrate delay function now optional
1 parent da83273 commit bb5681f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ if you use a 6 degrees of freedom (DOF) sensor, yaw will be invalid.
2727

2828
For 9DOF sensors. Accepts three 3-tuples (x, y, z) of accelerometer, gyro and magnetometer data
2929
and updates the filters. This should be called periodically with a frequency depending on the
30-
required response speed. Units:
31-
accel: Typically G. Values are normalised in the algorithm so units are irrelevant.
32-
gyro: Degrees per second.
33-
magnetometer: Microtesla.
30+
required response speed. Units:
31+
accel: Typically G. Values are normalised in the algorithm: units are irrelevant.
32+
gyro: Degrees per second.
33+
magnetometer: Typically Microtesla but values are normalised in the algorithm: units are irrelevant.
3434

3535
```update_nomag(accel, gyro)```
3636

3737
For 6DOF sensors. Accepts 3-tuples (x, y, z) of accelerometer and gyro data and
3838
updates the filters. This should be called periodically, depending on the required response
39-
speed. Units:
40-
accel: Typically G. Values are normalised in the algorithm so units are irrelevant.
41-
gyro: Degrees per second.
39+
speed. Units:
40+
accel: Typically G. Values are normalised in the algorithm so units are irrelevant.
41+
gyro: Degrees per second.
4242

43-
```calibrate(getxyz, stopfunc, waitfunc)```
43+
```calibrate(getxyz, stopfunc, waitfunc = None)```
4444

45-
The first argument is a function returning a tuple of magnetic x,y,z values from the sensor.
45+
The first argument is a function returning a tuple of magnetic x,y,z values from the sensor.
4646
The second is a function returning ```True``` when calibration is deemed complete: this could
4747
be a timer or an input from the user.
4848
The third is a function providing a delay. Some hardware may require a delay between magnetometer

fusion.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Sensor fusion for the micropython board. 29th May 2015
22
# Ported to MicroPython/Pyboard by Peter Hinch.
3+
# V0.65 waitfunc now optional
34
# V0.6 calibrate altered to work round MicroPython map() bug, waitfunc added
45
# V0.5 angles method replaced by yaw pitch and roll properties
56
# V0.4 calibrate method added
@@ -18,8 +19,8 @@
1819
arguments:
1920
getxyz must return current magnetometer (x, y, z) tuple from the sensor
2021
stopfunc (responding to time or user input) tells it to stop
21-
waitfunc provides a delay between readings. typically pyb.delay(100) but can be altered in threaded environment
22-
Sets magbias to the mean values of x,y,z
22+
waitfunc provides an optional delay between readings to accommodate hardware or to avoid hogging
23+
the CPU in a threaded environment. It sets magbias to the mean values of x,y,z
2324
'''
2425
class Fusion(object):
2526
'''
@@ -34,11 +35,12 @@ def __init__(self):
3435
GyroMeasError = radians(40) # Original code indicates this leads to a 2 sec response time
3536
self.beta = sqrt(3.0 / 4.0) * GyroMeasError # compute beta (see README)
3637

37-
def calibrate(self, getxyz, stopfunc, waitfunc):
38+
def calibrate(self, getxyz, stopfunc, waitfunc = None):
3839
magmax = list(getxyz()) # Initialise max and min lists with current values
3940
magmin = magmax[:]
4041
while not stopfunc():
41-
waitfunc()
42+
if waitfunc is not None:
43+
waitfunc()
4244
magxyz = tuple(getxyz())
4345
for x in range(3):
4446
magmax[x] = max(magmax[x], magxyz[x])
@@ -117,8 +119,8 @@ def update_nomag(self, accel, gyro): # 3-tuples (x, y, z) for accel, gyro
117119
self.q = q1 * norm, q2 * norm, q3 * norm, q4 * norm
118120

119121
def update(self, accel, gyro, mag): # 3-tuples (x, y, z) for accel, gyro and mag data
120-
mx, my, mz = (mag[x] - self.magbias[x] for x in range(3)) # Units uT
121-
ax, ay, az = accel # Units G (but later normalised)
122+
mx, my, mz = (mag[x] - self.magbias[x] for x in range(3)) # Units irrelevant (normalised)
123+
ax, ay, az = accel # Units irrelevant (normalised)
122124
gx, gy, gz = (radians(x) for x in gyro) # Units deg/s
123125
if self.start_time is None:
124126
self.start_time = pyb.micros() # First run

0 commit comments

Comments
 (0)