1
1
# Sensor fusion for the micropython board. 29th May 2015
2
2
# Ported to MicroPython/Pyboard by Peter Hinch.
3
+ # V0.65 waitfunc now optional
3
4
# V0.6 calibrate altered to work round MicroPython map() bug, waitfunc added
4
5
# V0.5 angles method replaced by yaw pitch and roll properties
5
6
# V0.4 calibrate method added
18
19
arguments:
19
20
getxyz must return current magnetometer (x, y, z) tuple from the sensor
20
21
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
23
24
'''
24
25
class Fusion (object ):
25
26
'''
@@ -34,11 +35,12 @@ def __init__(self):
34
35
GyroMeasError = radians (40 ) # Original code indicates this leads to a 2 sec response time
35
36
self .beta = sqrt (3.0 / 4.0 ) * GyroMeasError # compute beta (see README)
36
37
37
- def calibrate (self , getxyz , stopfunc , waitfunc ):
38
+ def calibrate (self , getxyz , stopfunc , waitfunc = None ):
38
39
magmax = list (getxyz ()) # Initialise max and min lists with current values
39
40
magmin = magmax [:]
40
41
while not stopfunc ():
41
- waitfunc ()
42
+ if waitfunc is not None :
43
+ waitfunc ()
42
44
magxyz = tuple (getxyz ())
43
45
for x in range (3 ):
44
46
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
117
119
self .q = q1 * norm , q2 * norm , q3 * norm , q4 * norm
118
120
119
121
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)
122
124
gx , gy , gz = (radians (x ) for x in gyro ) # Units deg/s
123
125
if self .start_time is None :
124
126
self .start_time = pyb .micros () # First run
0 commit comments