Skip to content

Commit 772aa35

Browse files
committed
downgrade to v1.1.0
1 parent 7460ed5 commit 772aa35

File tree

8 files changed

+231
-212
lines changed

8 files changed

+231
-212
lines changed

README.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
# imuFilter
2-
This library fuses the outputs of an inertial measurement unit (IMU) and stores the heading as a quaternion. It uses a _modified Mahony_ filter that replaces the PI controller with a damped 2nd-order system. The proportional term was removed and the integral term was forced to decay to damp the system. The correction steps are as follows:
32

4-
__Mahony:__
5-
$\ E = \theta_{accel} - \theta_{k-1} $
3+
This library contains a sensor fusion algorithm to combine the outputs of a 6-axis inertial measurement unit (IMU). It's based on a modified version of the Mahony filter that replaces the PI controller with something akin to a 2nd order low pass filter. The proportional term was removed and the integral term has been forced to decay in order to damp the system. The correction steps of each filter are shown below:
64

7-
$\ I_{k} = I_{k-1} + {E}{\Delta t} $
5+
- Mahony:
6+
_integral += error.dt
7+
dtheta = theta_dot.dt + kp.error + ki.integral_
88

9-
$\ \theta_{k} = \theta_{k-1} + \dot{\theta}{\Delta t} + K_{p}E + K_{i}I_{k} $
9+
- Modified Mahony:
10+
_integral += error.kp - integral.kc
11+
dtheta = theta_dot.dt + integral_
1012

11-
__Modified Mahony:__ (this filter)
12-
$\ E = \theta_{accel} - \theta_{k-1} $
13+
The behavior of the modified filter is analogous to spring-mass system. Kp (stiffness) and Kc (damping) are related by the damping ratio Q which is held constant. This allows the behavior of the filter to be controlled via a single parameter.
1314

14-
$\ I_{k} = K_{p}{E} + (1 - K_{c})I_{k-1} $
15+
The filter uses a quaternion to encode rotations. This makes it easy to perform coordinate transformations. These include:
16+
- Transfor a vector from the local frame to the global (and vice versa)
17+
- Get unit vectors of the X, Y and Z axes in the local or global frame.
1518

16-
$\ \theta_{k} = \theta_{k-1} + \dot{\theta}{\Delta t} + I_{k} $
19+
Since a 6-axis IMU has no absolute reference for heading there is a function to rotate the orientation estimate about the yaw axis. Basic vector operations have been included to easily implement a heading correction algorithm should one have an additional sensor (such a magnetometer or some other absolute heading sensor).
1720

18-
The behavior of the modified filter is analogous to spring-mass system. Kp (stiffness) and Kc (damping) are related by the [Q-factor](https://en.wikipedia.org/wiki/Q_factor). This value is held constant, so the behavior of the filter is controlled by a single parameter (Kp):
19-
20-
$\ K_{c} = \sqrt{ K_{p}/Q } $
21-
22-
As the filter uses a quaternion to encode rotations, it's easy to perform coordinate transformations. The library has functions to:
23-
- Transfor a vector to the local or global frame.
24-
- Get the unit vectors of the X, Y and Z axes in the local or global frame.
21+
For more information on the Mahony filter see these links:
22+
- [IMU Data Fusing: Complementary, Kalman, and Mahony Filter](http://www.olliw.eu/2013/imu-data-fusing/#chapter23)
23+
- [Mahony Filter](https://nitinjsanket.github.io/tutorials/attitudeest/mahony)
2524

26-
Moreover, since a 6-axis IMU (gyro-accelerometer) cannot measure an absolute heading, a function is included to rotate the orientation about the vertical (yaw) axis. One can use vector operations to correct the heading with an additional sensor such a magnetometer.
25+
## Dependencies
2726

28-
# Dependecies
2927
This library depends on the [vector_datatype](https://github.com/RCmags/vector_datatype) library.
30-
31-
# References
32-
See these links for more information on the Mahony filter:
33-
- [Nonlinear Complementary Filters on the Special
34-
Orthogonal Group](https://hal.archives-ouvertes.fr/hal-00488376/document) (original paper)
35-
- [IMU Data Fusing: Complementary, Kalman, and Mahony Filter](http://www.olliw.eu/2013/imu-data-fusing/#chapter23)
36-
- [Mahony Filter](https://nitinjsanket.github.io/tutorials/attitudeest/mahony)

examples/heading/heading.ino

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/*
2-
This sketch shows to rotate the heading (yaw angle) of the estimated orientation.
2+
This sketch shows to perform vector products and rotate heading (yaw angle) of the estimated orientation.
33
*/
44

5-
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
65
#include <imuFilter.h>
6+
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
77

8-
basicMPU6050<> imu;
8+
// Sensor fusion
9+
constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines response of heading correction with respect to gravity.
10+
imuFilter <&GAIN> fusion;
911

10-
imuFilter fusion;
12+
// Imu sensor
13+
basicMPU6050<> imu;
1114

1215
void setup() {
1316
// Initialize filter:
@@ -16,12 +19,12 @@ void setup() {
1619
// Calibrate imu
1720
imu.setup();
1821
imu.setBias();
22+
23+
Serial.begin(38400);
1924

2025
// Rotate heading:
2126
float angle = 45 * DEG_TO_RAD; // angle in radians to rotate heading about z-axis
2227
fusion.rotateHeading( angle, LARGE_ANGLE ); // Can choose LARGE_ANGLE or SMALL_ANGLE approximation
23-
24-
Serial.begin(38400);
2528
}
2629

2730
void loop() {

examples/output/output.ino

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
This sketch shows to initialize the filter and update it with the imu output. It also shows how to get the euler angles of the estimated heading orientation.
33
*/
44

5-
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
65
#include <imuFilter.h>
6+
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
77

8-
basicMPU6050<> imu;
9-
10-
// =========== Settings ===========
11-
imuFilter fusion;
8+
// Sensor fusion
9+
constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines orientation correction with respect to gravity vector.
10+
imuFilter <&GAIN> fusion; // If set to 1 the gyroscope is dissabled. If set to 0 the accelerometer is dissabled (equivant to gyro-only).
1211

13-
#define GAIN 0.1 /* Fusion gain, value between 0 and 1 - Determines orientation correction with respect to gravity vector.
14-
If set to 1 the gyroscope is dissabled. If set to 0 the accelerometer is dissabled (equivant to gyro-only) */
12+
// Imu sensor
13+
basicMPU6050<> imu;
1514

16-
#define SD_ACCEL 0.1 /* Standard deviation of acceleration. Accelerations relative to (0,0,1)g outside of this band are suppresed.
17-
Accelerations within this band are used to update the orientation. [Measured in g-force] */
18-
19-
#define FUSION true /* Enable sensor fusion. Setting to "true" enables gravity correction */
15+
// Enable sensor fusion. Setting to "true" enables gravity correction.
16+
#define FUSION false
2017

2118
void setup() {
2219
#if FUSION
@@ -38,8 +35,8 @@ void loop() {
3835
// Update filter:
3936

4037
#if FUSION
41-
/*NOTE: GAIN and SD_ACCEL are optional parameters */
42-
fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az(), GAIN, SD_ACCEL );
38+
//Fuse gyro and accelerometer
39+
fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az() );
4340
#else
4441
// Only use gyroscope
4542
fusion.update( imu.gx(), imu.gy(), imu.gz() );

examples/vector_output/vector_output.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
This sketch shows to get the axis projections from the orientation estimate. It also shows how to project a vector in the global or local reference frame.
33
*/
44

5-
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
65
#include <imuFilter.h>
6+
#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050
77

8-
basicMPU6050<> imu;
9-
10-
imuFilter fusion;
8+
// Sensor fusion
9+
constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines response of heading correction with respect to gravity.
10+
imuFilter <&GAIN> fusion;
1111

12-
// ========= functions ===========
12+
// Imu sensor
13+
basicMPU6050<> imu;
1314

15+
// Display functions:
1416
void printVector( vec3_t r ) {
1517
Serial.print( r.x, 2 );
1618
Serial.print( "," );

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=imuFilter
2-
version=1.2.0
2+
version=1.1.0
33
author=RCmags <[email protected]>
44
maintainer=RCmags <[email protected]>
55
sentence=Sensor fusion for IMU with quaternion based filter.
6-
paragraph=Library to fuse the data of an inertial measurement unit (IMU). It uses a quaternion to encode the rotation and uses a modified Mahony filter to correct the gyroscope with the accelerometer.
6+
paragraph=Library that fuses the accelerometer and gyroscope outputs of an IMU. It uses a quaternion to encode the rotation and uses a modified Mahony filter to correct the gyro with the accelerometer.
77
category=Device Control
88
url=https://github.com/RCmags/imuFilter
99
architectures=*

src/imuFilter.cpp

Lines changed: 0 additions & 150 deletions
This file was deleted.

src/imuFilter.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77
//------------------ Coefficients --------------------
88

9-
#define INV_Q_FACTOR 2 // Filter damping. A smaller value leads to faster response but more oscillations.
10-
#define DEFAULT_GAIN 0.1 // Default filter gain. A faster value
11-
#define DEFAULT_SD 0.1 // Default standard deviation in acceleration. [g-force]
9+
#define INV_Q_VAL 1.414213 // Damping behavior of filter. A larger value leads to faster response but more oscillations.
10+
11+
//-------------------- Parameters -------------------- [ No characters after backlash! ]
12+
13+
#define TEMPLATE_TYPE const float *ALPHA
14+
15+
#define TEMPLATE_INPUTS ALPHA
1216

1317
//---------------- Class definition ------------------
1418

19+
template<TEMPLATE_TYPE>
1520
class imuFilter {
1621
private:
1722
vec3_t s;
1823
quat_t q;
19-
float var;
2024
uint32_t last_time;
21-
2225
float updateTimer();
2326

2427
public:
@@ -28,12 +31,7 @@ class imuFilter {
2831

2932
// Heading estimate:
3033
void update( float, float, float );
31-
32-
void update( float, float, float,
33-
float, float, float,
34-
const float=DEFAULT_GAIN,
35-
const float=DEFAULT_SD );
36-
34+
void update( float, float, float, float, float, float );
3735
void rotateHeading( float, const bool );
3836

3937
//-- Fusion outputs:
@@ -53,4 +51,11 @@ class imuFilter {
5351
float yaw();
5452
};
5553

54+
#include "imuFilter.tpp"
55+
56+
//----------------- Clearing labels ------------------
57+
58+
#undef TEMPLATE_TYPE
59+
#undef TEMPLATE_INPUTS
60+
5661
#endif

0 commit comments

Comments
 (0)