|
1 | | -/* |
2 | | - 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. |
3 | | - */ |
4 | | - |
5 | | -#include <imuFilter.h> |
6 | | -#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050 |
7 | | - |
8 | | -// Sensor fusion |
9 | | -constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines heading correction with respect to gravity vector. |
10 | | -imuFilter <&GAIN> fusion; |
11 | | - |
12 | | -// Imu sensor |
13 | | -basicMPU6050<> imu; |
14 | | - |
15 | | -// Enable sensor fusion [ 1 = yes; 0 = no] |
16 | | -#define FUSION 1 |
17 | | - |
18 | | -void setup() { |
19 | | - #if FUSION |
20 | | - // Set quaternion with gravity vector |
21 | | - fusion.setup( imu.ax(), imu.ay(), imu.az() ); |
22 | | - #else |
23 | | - // Just use gyro |
24 | | - fusion.setup(); |
25 | | - #endif |
26 | | - |
27 | | - // Calibrate imu |
28 | | - imu.setup(); |
29 | | - imu.setBias(); |
30 | | - |
31 | | - Serial.begin(38400); |
32 | | -} |
33 | | - |
34 | | -void loop() { |
35 | | - // Update filter: |
36 | | - |
37 | | - #if FUSION |
38 | | - //Fuse gyro and accelerometer |
39 | | - fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az() ); |
40 | | - #else |
41 | | - // Only use gyroscope |
42 | | - fusion.update( imu.gx(), imu.gy(), imu.gz() ); |
43 | | - #endif |
44 | | - |
45 | | - // Display angles: |
46 | | - Serial.print( fusion.pitch() ); |
47 | | - Serial.print( " " ); |
48 | | - Serial.print( fusion.yaw() ); |
49 | | - Serial.print( " " ); |
50 | | - Serial.print( fusion.roll() ); |
51 | | - Serial.println(); |
52 | | -} |
| 1 | +/* |
| 2 | + 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. |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050 |
| 6 | +#include <imuFilter.h> |
| 7 | + |
| 8 | +// Gyro settings: |
| 9 | +#define LP_FILTER 3 // Low pass filter. Value from 0 to 6 |
| 10 | +#define GYRO_SENS 0 // Gyro sensitivity. Value from 0 to 3 |
| 11 | +#define ACCEL_SENS 0 // Accelerometer sensitivity. Value from 0 to 3 |
| 12 | +#define ADDRESS_A0 LOW // I2C address from state of A0 pin. A0 -> GND : ADDRESS_A0 = LOW |
| 13 | + // A0 -> 5v : ADDRESS_A0 = HIGH |
| 14 | +// Accelerometer offset: |
| 15 | +constexpr int AX_OFFSET = 552; // Use these values to calibrate the accelerometer. The sensor should output 1.0g if held level. |
| 16 | +constexpr int AY_OFFSET = -241; // These values are unlikely to be zero. |
| 17 | +constexpr int AZ_OFFSET = -3185; |
| 18 | + |
| 19 | +// Output scale: |
| 20 | +constexpr float AX_SCALE = 1.00457; // Multiplier for accelerometer outputs. Use this to calibrate the sensor. If unknown set to 1. |
| 21 | +constexpr float AY_SCALE = 0.99170; |
| 22 | +constexpr float AZ_SCALE = 0.98317; |
| 23 | + |
| 24 | +constexpr float GX_SCALE = 0.99764; // Multiplier to gyro outputs. Use this to calibrate the sensor. If unknown set to 1. |
| 25 | +constexpr float GY_SCALE = 1.0; |
| 26 | +constexpr float GZ_SCALE = 1.01037; |
| 27 | + |
| 28 | +// Bias estimate: |
| 29 | +#define GYRO_BAND 35 // Standard deviation of the gyro signal. Gyro signals within this band (relative to the mean) are suppresed. |
| 30 | +#define BIAS_COUNT 5000 // Samples of the mean of the gyro signal. Larger values provide better calibration but delay suppression response. |
| 31 | + |
| 32 | +//-- Set the template parameters: |
| 33 | + |
| 34 | +basicMPU6050<LP_FILTER, GYRO_SENS, ACCEL_SENS, ADDRESS_A0, |
| 35 | + AX_OFFSET, AY_OFFSET, AZ_OFFSET, |
| 36 | + &AX_SCALE, &AY_SCALE, &AZ_SCALE, |
| 37 | + &GX_SCALE, &GY_SCALE, &GZ_SCALE, |
| 38 | + GYRO_BAND, BIAS_COUNT |
| 39 | + >imu; |
| 40 | + |
| 41 | +// =========== Settings =========== |
| 42 | +imuFilter fusion; |
| 43 | + |
| 44 | +#define GAIN 0.5 /* Fusion gain, value between 0 and 1 - Determines orientation correction with respect to gravity vector. |
| 45 | + If set to 1 the gyroscope is dissabled. If set to 0 the accelerometer is dissabled (equivant to gyro-only) */ |
| 46 | + |
| 47 | +#define SD_ACCEL 0.2 /* Standard deviation of acceleration. Accelerations relative to (0,0,1)g outside of this band are suppresed. |
| 48 | + Accelerations within this band are used to update the orientation. [Measured in g-force] */ |
| 49 | + |
| 50 | +#define FUSION true /* Enable sensor fusion. Setting to "true" enables gravity correction */ |
| 51 | + |
| 52 | +void setup() { |
| 53 | + Serial.begin(38400); |
| 54 | + |
| 55 | + // Calibrate imu |
| 56 | + imu.setup(); |
| 57 | + imu.setBias(); |
| 58 | + |
| 59 | + #if FUSION |
| 60 | + Set quaternion with gravity vector |
| 61 | + fusion.setup( imu.ax(), imu.ay(), imu.az() ); |
| 62 | + #else |
| 63 | + Just use gyro |
| 64 | + fusion.setup(); |
| 65 | + #endif |
| 66 | +} |
| 67 | + |
| 68 | +void loop() { |
| 69 | + // Update filter: |
| 70 | + |
| 71 | + #if FUSION |
| 72 | + /*NOTE: GAIN and SD_ACCEL are optional parameters */ |
| 73 | + fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az(), GAIN, SD_ACCEL ); |
| 74 | + #else |
| 75 | + // Only use gyroscope |
| 76 | + fusion.update( imu.gx(), imu.gy(), imu.gz() ); |
| 77 | + #endif |
| 78 | + |
| 79 | + // Display angles: |
| 80 | + Serial.print( fusion.pitch() ); |
| 81 | + Serial.print( " " ); |
| 82 | + Serial.print( fusion.yaw() ); |
| 83 | + Serial.print( " " ); |
| 84 | + Serial.print( fusion.roll() ); |
| 85 | + Serial.println(); |
| 86 | +} |
0 commit comments