1
1
#include < CurieIMU.h>
2
+ // for better performance, compile this code using at least C++ 17.
2
3
#include < MadgwickAHRS.h>
3
4
4
5
Madgwick filter;
@@ -8,11 +9,15 @@ float accelScale, gyroScale;
8
9
void setup () {
9
10
Serial.begin (9600 );
10
11
11
- // start the IMU and filter
12
+ // start the IMU
12
13
CurieIMU.begin ();
13
14
CurieIMU.setGyroRate (25 );
14
15
CurieIMU.setAccelerometerRate (25 );
15
- filter.begin (25 );
16
+
17
+ // set the Madgwick's filter parameter (beta)
18
+ filter.setBeta (0.1 );
19
+ // set the IMU update frequency in Hz
20
+ filter.setFrequency (25 );
16
21
17
22
// Set the accelerometer range to 2 g
18
23
CurieIMU.setAccelerometerRange (2 );
@@ -30,6 +35,7 @@ void loop() {
30
35
float ax, ay, az;
31
36
float gx, gy, gz;
32
37
float roll, pitch, heading;
38
+ float q[4 ];
33
39
unsigned long microsNow;
34
40
35
41
// check if it's time to read data and update the filter
@@ -48,19 +54,34 @@ void loop() {
48
54
gz = convertRawGyro (giz);
49
55
50
56
// update the filter, which computes orientation
51
- filter.updateIMU (gx, gy, gz, ax, ay, az);
57
+ // the first template parameter is the local reference frame
58
+ // NWU = 0, NED = 1, ENU = 2
59
+ // the second template parameter is the measurement unit of the gyroscope reading
60
+ // 'D' = degree per second, 'R' = radians per second
61
+ filter.updateIMU <0 ,' D' >(gx, gy, gz, ax, ay, az);
52
62
53
- // print the heading, pitch and roll
54
- roll = filter.getRoll ();
55
- pitch = filter.getPitch ();
56
- heading = filter.getYaw ();
63
+ // print the heading, pitch and roll (Tait-Bryan angle in ZYX convention)
64
+ roll = filter.getRollDegree ();
65
+ pitch = filter.getPitchDegree ();
66
+ heading = filter.getYawDegree ();
57
67
Serial.print (" Orientation: " );
58
68
Serial.print (heading);
59
69
Serial.print (" " );
60
70
Serial.print (pitch);
61
71
Serial.print (" " );
62
72
Serial.println (roll);
63
73
74
+ // get and print the quaternion
75
+ filter.getQuaternion (q);
76
+ Serial.print (" Quaternion: " );
77
+ Serial.print (q[0 ]);
78
+ Serial.print (" " );
79
+ Serial.print (q[1 ]);
80
+ Serial.print (" " );
81
+ Serial.print (q[2 ]);
82
+ Serial.print (" " );
83
+ Serial.println (q[3 ]);
84
+
64
85
// increment previous time, so we keep proper pace
65
86
microsPrevious = microsPrevious + microsPerReading;
66
87
}
@@ -82,4 +103,4 @@ float convertRawGyro(int gRaw) {
82
103
83
104
float g = (gRaw * 250.0 ) / 32768.0 ;
84
105
return g;
85
- }
106
+ }
0 commit comments