11#include < CurieIMU.h>
2+ // for better performance, compile this code using at least C++ 17.
23#include < MadgwickAHRS.h>
34
45Madgwick filter;
@@ -8,11 +9,15 @@ float accelScale, gyroScale;
89void setup () {
910 Serial.begin (9600 );
1011
11- // start the IMU and filter
12+ // start the IMU
1213 CurieIMU.begin ();
1314 CurieIMU.setGyroRate (25 );
1415 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 );
1621
1722 // Set the accelerometer range to 2 g
1823 CurieIMU.setAccelerometerRange (2 );
@@ -30,6 +35,7 @@ void loop() {
3035 float ax, ay, az;
3136 float gx, gy, gz;
3237 float roll, pitch, heading;
38+ float q[4 ];
3339 unsigned long microsNow;
3440
3541 // check if it's time to read data and update the filter
@@ -48,19 +54,34 @@ void loop() {
4854 gz = convertRawGyro (giz);
4955
5056 // 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);
5262
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 ();
5767 Serial.print (" Orientation: " );
5868 Serial.print (heading);
5969 Serial.print (" " );
6070 Serial.print (pitch);
6171 Serial.print (" " );
6272 Serial.println (roll);
6373
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+
6485 // increment previous time, so we keep proper pace
6586 microsPrevious = microsPrevious + microsPerReading;
6687 }
@@ -82,4 +103,4 @@ float convertRawGyro(int gRaw) {
82103
83104 float g = (gRaw * 250.0 ) / 32768.0 ;
84105 return g;
85- }
106+ }
0 commit comments