Skip to content

Commit 5c8dc26

Browse files
committed
Test programs fixed for new MPU9x50 interface. Yaw replaced with Heading.
1 parent bb5681f commit 5c8dc26

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# micropython-fusion
22

3-
Sensor fusion calculating yaw, pitch and roll from the outputs of motion tracking devices. This
3+
Sensor fusion calculating heading, pitch and roll from the outputs of motion tracking devices. This
44
uses the Madgwick algorithm, widely used in multicopter designs for its speed and quality. An
55
update takes about 1.6mS on the Pyboard. The original Madgwick study indicated that an update
66
rate of 10-50Hz was adequate for accurate results, suggesting that the performance of this
@@ -18,8 +18,8 @@ if my observations about coordinate conventions are incorrect.
1818
## Fusion class
1919

2020
The module supports this one class. A Fusion object needs to be periodically updated with data
21-
from the sensor. It provides yaw, pitch and roll values (in degrees) as properties. Note that
22-
if you use a 6 degrees of freedom (DOF) sensor, yaw will be invalid.
21+
from the sensor. It provides heading, pitch and roll values (in degrees) as properties. Note that
22+
if you use a 6 degrees of freedom (DOF) sensor, heading will be invalid.
2323

2424
### Methods
2525

@@ -57,11 +57,10 @@ runs, the aim being to compensate for offsets caused by static local magnetic fi
5757

5858
Three read-only properties provide access to the angles. These are in degrees.
5959

60-
**yaw**
60+
**heading**
6161

62-
Angle relative to North. Better terminology is "heading" (used in the original Madgwick study)
63-
since it is ground referenced: yaw is also used to mean the angle of an aircraft's fuselage
64-
relative to its direction of motion.
62+
Angle relative to North. Note some sources use the term "yaw". As this is also used to mean
63+
the angle of an aircraft's fuselage relative to its direction of motion, I have avoided it.
6564

6665
**pitch**
6766

@@ -103,11 +102,12 @@ GC time shorter and ensures it occurs at a predictable time. See the MicroPython
103102

104103
These are blatantly plagiarised as this isn't my field. I have quoted sources.
105104

106-
### Yaw Pitch and Roll
105+
### Heading Pitch and Roll
107106

108107
Perhaps better titled heading, elevation and bank: there seems to be ambiguity about the concept
109108
of yaw, whether this is measured relative to the aircraft's local coordinate system or that of
110-
the Earth. The angles emitted by the Madgwick algorithm (Tait-Bryan angles) are earth-relative.
109+
the Earth: the original Madgwick study uses the term "heading", a convention I have retained as
110+
the angles emitted by the Madgwick algorithm (Tait-Bryan angles) are earth-relative.
111111
See [Wikipedia article](http://en.wikipedia.org/wiki/Euler_angles#Tait.E2.80.93Bryan_angles)
112112

113113
The following adapted from https://github.com/kriswiner/MPU-9250.git

fusion.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Sensor fusion for the micropython board. 29th May 2015
1+
# Sensor fusion for the micropython board. 25th June 2015
22
# Ported to MicroPython/Pyboard by Peter Hinch.
3+
# V0.7 Yaw replaced with heading
34
# V0.65 waitfunc now optional
45
# V0.6 calibrate altered to work round MicroPython map() bug, waitfunc added
5-
# V0.5 angles method replaced by yaw pitch and roll properties
6+
# V0.5 angles method replaced by heading pitch and roll properties
67
# V0.4 calibrate method added
78

89
import pyb
@@ -12,7 +13,7 @@
1213
Source https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU.git
1314
also https://github.com/kriswiner/MPU-9250.git
1415
Ported to Python. Integrator timing adapted for pyboard.
15-
User should repeatedly call the appropriate 6 or 9 DOF update method and extract yaw pitch and roll angles as
16+
User should repeatedly call the appropriate 6 or 9 DOF update method and extract heading pitch and roll angles as
1617
required.
1718
Calibrate method:
1819
The sensor should be slowly rotated around each orthogonal axis while this runs.
@@ -24,10 +25,10 @@
2425
'''
2526
class Fusion(object):
2627
'''
27-
Class provides sensor fusion allowing yaw, pitch and roll to be extracted. This uses the Madgwick algorithm.
28+
Class provides sensor fusion allowing heading, pitch and roll to be extracted. This uses the Madgwick algorithm.
2829
The update method must be called peiodically. The calculations take 1.6mS on the Pyboard.
2930
'''
30-
declination = 0 # Optional offset for true north. A +ve value adds to yaw
31+
declination = 0 # Optional offset for true north. A +ve value adds to heading
3132
def __init__(self):
3233
self.magbias = (0, 0, 0) # local magnetic bias factors: set from calibration
3334
self.start_time = None # Time between updates
@@ -48,7 +49,7 @@ def calibrate(self, getxyz, stopfunc, waitfunc = None):
4849
self.magbias = tuple(map(lambda a, b: (a +b)/2, magmin, magmax))
4950

5051
@property
51-
def yaw(self):
52+
def heading(self):
5253
return self.declination + degrees(atan2(2.0 * (self.q[1] * self.q[2] + self.q[0] * self.q[3]),
5354
self.q[0] * self.q[0] + self.q[1] * self.q[1] - self.q[2] * self.q[2] - self.q[3] * self.q[3]))
5455

fusionlcd.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Fusionlcd.py Test for sensor fusion on Pyboard. Uses LCD display and threaded library.
22
# Author Peter Hinch
3-
# V0.65 2nd June 2015
3+
# For libraries see https://github.com/peterhinch/Micropython-scheduler.git
4+
# V0.7 25th June 2015 Adapted for new MPU9x50 interface
45

56
import pyb
67
from mpu9150 import MPU9150
@@ -20,9 +21,7 @@
2021
"""
2122
switch = pyb.Pin('Y7', pyb.Pin.IN, pull=pyb.Pin.PULL_UP) # Switch to ground on Y7
2223

23-
imu = MPU9150('X', 1, True) # Attached to 'X' bus, 1 device, disable interruots
24-
imu.gyro_range(0)
25-
imu.accel_range(0)
24+
imu = MPU9150('X') # Attached to 'X' bus, 1 device, disable interruots
2625

2726
fuse = Fusion()
2827

@@ -34,16 +33,16 @@ def lcd_thread(mylcd, imu, sw):
3433
mylcd[0] = "Calibrate. Push switch"
3534
mylcd[1] = "when done"
3635
yield from wait(0.1)
37-
fuse.calibrate(imu.get_mag, lambda : not sw.value(), waitfunc)
36+
fuse.calibrate(imu.mag.xyz, lambda : not sw.value(), waitfunc)
3837
print(fuse.magbias)
3938
mylcd[0] = "{:5s}{:5s} {:5s}".format("Yaw","Pitch","Roll")
4039
count = 0
4140
while True:
4241
yield from wait(0.02) # IMU updates at 50Hz
4342
count += 1
4443
if count % 25 == 0:
45-
mylcd[1] = "{:4.0f} {:4.0f} {:4.0f}".format(fuse.yaw, fuse.pitch, fuse.roll)
46-
fuse.update(imu.get_accel(), imu.get_gyro(), imu.get_mag())
44+
mylcd[1] = "{:4.0f} {:4.0f} {:4.0f}".format(fuse.heading, fuse.pitch, fuse.roll)
45+
fuse.update(imu.accel.xyz, imu.gyro.xyz, imu.mag.xyz)
4746
# For 6DOF sensors
4847
# fuse.update_nomag(imu.get_accel(), imu.get_gyro())
4948

fusiontest.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
# Simple test program for sensor fusion on Pyboard
22
# Author Peter Hinch
3-
# 29th May 2015
4-
# V0.6
3+
# V0.7 25th June 2015 Adapted for new MPU9x50 interface
4+
55
import pyb
66
from mpu9150 import MPU9150
77
from fusion import Fusion
88

9-
imu = MPU9150('X', 1, False)
10-
imu.gyro_range(0)
11-
imu.accel_range(0)
9+
imu = MPU9150('X')
1210

1311
fuse = Fusion()
1412

1513
# Choose test to run
1614
Calibrate = True
1715
Timing = False
1816

17+
def getmag(): # Return (x, y, z) tuple (blocking read)
18+
return imu.mag.xyz
19+
1920
if Calibrate:
2021
print("Calibrating. Press switch when done.")
2122
sw = pyb.Switch()
22-
fuse.calibrate(imu.get_mag, sw, lambda : pyb.delay(100))
23+
fuse.calibrate(getmag, sw, lambda : pyb.delay(100))
2324
print(fuse.magbias)
2425

2526
if Timing:
26-
mag = imu.get_mag() # Don't include blocking read in time
27-
accel = imu.get_accel() # or i2c
28-
gyro = imu.get_gyro()
27+
mag = imu.mag.xyz # Don't include blocking read in time
28+
accel = imu.accel.xyz # or i2c
29+
gyro = imu.gyro.xyz
2930
start = pyb.micros()
3031
fuse.update(accel, gyro, mag) # 1.65mS on Pyboard
3132
t = pyb.elapsed_micros(start)
3233
print("Update time (uS):", t)
3334

3435
count = 0
3536
while True:
36-
fuse.update(imu.get_accel(), imu.get_gyro(), imu.get_mag()) # Note blocking mag read
37+
fuse.update(imu.accel.xyz, imu.gyro.xyz, imu.mag.xyz) # Note blocking mag read
3738
if count % 50 == 0:
38-
print("Yaw, Pitch, Roll: {:7.3f} {:7.3f} {:7.3f}".format(fuse.yaw, fuse.pitch, fuse.roll))
39+
print("Heading, Pitch, Roll: {:7.3f} {:7.3f} {:7.3f}".format(fuse.heading, fuse.pitch, fuse.roll))
3940
pyb.delay(20)
4041
count += 1

0 commit comments

Comments
 (0)