1
1
# micropython-fusion
2
+
2
3
Sensor fusion calculating yaw, pitch and roll from the outputs of motion tracking devices. This
3
4
uses the Madgwick algorithm, widely used in multicopter designs for its speed and quality. An
4
- update takes about 1.6mS on the Pyboard
5
+ update takes about 1.6mS on the Pyboard. The code is intended to be independent of the sensor
6
+ device: testing was done with the InvenSense MPU-9150.
7
+
8
+ ## Disclaimer
9
+
10
+ I should point out that I'm unfamiliar with aircraft conventions and would appreciate feedback
11
+ if my observations about coordinate conventions are incorrect.
5
12
6
13
# fusion module
7
14
@@ -15,12 +22,12 @@ if you use a 6 degrees of freedom (DOF) sensor, yaw will be invalid.
15
22
16
23
``` update(accel, gyro, mag) ```
17
24
18
- For 9DOF sensors. Accepts 3-tuples (x, y, z) of accelerometer, gyro and magnetometer data and
19
- updates the filters. This should be called periodically, depending on the required response
20
- speed. Units:
25
+ For 9DOF sensors. Accepts three 3-tuples (x, y, z) of accelerometer, gyro and magnetometer data
26
+ and updates the filters. This should be called periodically with a frequency depending on the
27
+ required response speed. Units:
21
28
accel: Typically G. Values are normalised in the algorithm so units are irrelevant.
22
29
gyro: Degrees per second.
23
- magnetometer: Microteslas .
30
+ magnetometer: Microtesla .
24
31
25
32
``` update_nomag(accel, gyro) ```
26
33
@@ -33,8 +40,8 @@ gyro: Degrees per second.
33
40
``` calibrate(getxyz, stopfunc) ```
34
41
35
42
The first argument is a function returning a tuple of magnetic x,y,z values from the sensor.
36
- The second is a function returning ``` True ``` when calibration is deemed complete: a timer or an
37
- input from the user. It updates the ``` magbias ``` property.
43
+ The second is a function returning ``` True ``` when calibration is deemed complete: this could
44
+ be a timer or an input from the user. The method updates the ``` magbias ``` property.
38
45
39
46
Calibration is performed by rotating the unit around each orthogonal axis while the routine
40
47
runs, the aim being to compensate for offsets caused by static local magnetic fields.
@@ -45,17 +52,19 @@ Three read-only properties provide access to the angles. These are in degrees.
45
52
46
53
``` yaw ```
47
54
48
- Angle relative to North (anticlockwise)
55
+ Angle relative to North. Better terminology is "heading" since it is ground referenced: yaw
56
+ is also used to mean the angle of an aircraft's fuselage relative to its direction of motion.
49
57
50
58
``` pitch ```
51
59
52
- Angle of aircraft nose relative to ground (+ve is towards ground)
60
+ Angle of aircraft nose relative to ground (conventionally +ve is towards ground). Also known
61
+ as "elevation".
53
62
54
63
``` roll ```
55
64
56
- Angle of aircraft wings to ground anticlockwise (left wing down)
65
+ Angle of aircraft wings to ground, also known as "bank".
57
66
58
- ### Notes for beginners
67
+ ### Notes for constructors
59
68
60
69
If you're developing a machine using a motion sensing device consider the effects of vibration.
61
70
This can be surprisingly high (use the sensor to measure it). No amount of digital filtering
@@ -64,27 +73,31 @@ these will be aliased down into the filter passband and affect the results. It's
64
73
neccessary to isolate the sensor with a mechanical filter, typically a mass supported on very
65
74
soft rubber mounts.
66
75
67
- You may want to take control of garbage collection (GC). In systems with continuously running
68
- control loops there is a case for doing an explicit GC on each iteration: this tends to make the
69
- GC time shorter and ensures it occurs at a predictable time. See the MicroPython ``` gc ``` module.
70
-
71
76
If using a magnetoemeter consider the fact that the Earth's magnetic field is small: the field
72
77
detected may be influenced by ferrous metals in the machine being controlled or by currents in
73
78
nearby wires. If the latter are variable there is little chance of compensating for them, but
74
79
constant magnetic offsets may be addressed by calibration. This involves rotating the machine
75
80
around each of three orthogonal axes while running the fusion object's ``` calibrate ``` method.
76
81
77
- Update rate - TODO
82
+ The local coordinate system for the sensor is usually defined as follows, assuming the vehicle
83
+ is on the ground:
84
+ z Vertical axis, vector points towards ground
85
+ x Axis towards the front of the vehicle, vector points in normal direction of travel
86
+ y Vector points left from pilot's point of view (I think)
87
+
88
+ You may want to take control of garbage collection (GC). In systems with continuously running
89
+ control loops there is a case for doing an explicit GC on each iteration: this tends to make the
90
+ GC time shorter and ensures it occurs at a predictable time. See the MicroPython ``` gc ``` module.
78
91
79
92
# Background notes
80
93
81
- These are blatantly plagiarised as this isn't my field, but in my defence I have quoted sources.
94
+ These are blatantly plagiarised as this isn't my field. I have quoted sources.
82
95
83
96
### Yaw Pitch and Roll
84
97
85
98
Perhaps better titled heading, elevation and bank: there seems to be ambiguity about the concept
86
99
of yaw, whether this is measured relative to the aircraft's local coordinate system or that of
87
- the Earth. I gather Tait-Bryan angles are earth-relative.
100
+ the Earth. The angles emitted by the Madgwick algorithm ( Tait-Bryan angles) are earth-relative.
88
101
See http://en.wikipedia.org/wiki/Euler_angles#Tait.E2.80.93Bryan_angles
89
102
90
103
The following adapted from https://github.com/kriswiner/MPU-9250.git
@@ -97,11 +110,17 @@ is positive, up toward the sky is negative.
97
110
Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
98
111
These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
99
112
Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation
100
- the rotations must be applied in the correct order which for this configuration is yaw, pitch, and then roll.
101
- For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
113
+ the rotations must be applied in the correct order which for this configuration is yaw, pitch,
114
+ and then roll. For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
115
+ which has additional links.
116
+
117
+ I have seen sources which contradict the above directions for yaw (heading) and roll (bank).
102
118
103
119
### Beta
104
120
121
+ The Madgwick algorithm has a "magic number" Beta which determines the tradeoff between accuracy
122
+ and response speed.
123
+
105
124
Source: https://github.com/kriswiner/MPU-9250.git
106
125
There is a tradeoff in the beta parameter between accuracy and response speed.
107
126
In the original Madgwick study, beta of 0.041 (corresponding to GyroMeasError of 2.7 degrees/s)
0 commit comments