Skip to content

Commit 016d742

Browse files
Fix roll, pitch, yaw and use degrees
1 parent 099d023 commit 016d742

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/MadgwickAHRS.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Madgwick::Madgwick() {
4242
q2 = 0.0f;
4343
q3 = 0.0f;
4444
invSampleFreq = 1.0f / sampleFreqDef;
45+
anglesComputed = 0;
4546
}
4647

4748
void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
@@ -138,6 +139,7 @@ void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az
138139
q1 *= recipNorm;
139140
q2 *= recipNorm;
140141
q3 *= recipNorm;
142+
anglesComputed = 0;
141143
}
142144

143145
//-------------------------------------------------------------------------------------------
@@ -209,6 +211,7 @@ void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float
209211
q1 *= recipNorm;
210212
q2 *= recipNorm;
211213
q3 *= recipNorm;
214+
anglesComputed = 0;
212215
}
213216

214217
//-------------------------------------------------------------------------------------------
@@ -225,6 +228,16 @@ float Madgwick::invSqrt(float x) {
225228
return y;
226229
}
227230

228-
//============================================================================================
229-
// END OF CODE
230-
//============================================================================================
231+
//-------------------------------------------------------------------------------------------
232+
233+
void Madgwick::computeAngles()
234+
{
235+
float x = 2.0f * (q1*q3 - q0*q2);
236+
float y = 2.0f * (q0*q1 + q2*q3);
237+
float z = q0*q0 - q1*q1 - q2*q2 + q3*q3;
238+
pitch = atan2f(x, sqrtf(y*y + z*z));
239+
roll = atan2f(y, sqrtf(x*x + z*z));
240+
yaw = atan2f(2.0f*q1*q2 - 2.0f*q0*q3, 2.0f*q0*q0 + 2.0f*q1*q1 - 1.0f);
241+
anglesComputed = 1;
242+
}
243+

src/MadgwickAHRS.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Madgwick{
2929
float q2;
3030
float q3; // quaternion of sensor frame relative to auxiliary frame
3131
float invSampleFreq;
32+
float roll;
33+
float pitch;
34+
float yaw;
35+
char anglesComputed;
36+
void computeAngles();
3237

3338
//-------------------------------------------------------------------------------------------
3439
// Function declarations
@@ -37,9 +42,33 @@ class Madgwick{
3742
void begin(float sampleFrequency) { invSampleFreq = 1.0f / sampleFrequency; }
3843
void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
3944
void updateIMU(float gx, float gy, float gz, float ax, float ay, float az);
40-
float getPitch(){return atan2f(2.0f * q2 * q3 - 2.0f * q0 * q1, 2.0f * q0 * q0 + 2.0f * q3 * q3 - 1.0f);};
41-
float getRoll(){return -1.0f * asinf(2.0f * q1 * q3 + 2.0f * q0 * q2);};
42-
float getYaw(){return atan2f(2.0f * q1 * q2 - 2.0f * q0 * q3, 2.0f * q0 * q0 + 2.0f * q1 * q1 - 1.0f);};
45+
//float getPitch(){return atan2f(2.0f * q2 * q3 - 2.0f * q0 * q1, 2.0f * q0 * q0 + 2.0f * q3 * q3 - 1.0f);};
46+
//float getRoll(){return -1.0f * asinf(2.0f * q1 * q3 + 2.0f * q0 * q2);};
47+
//float getYaw(){return atan2f(2.0f * q1 * q2 - 2.0f * q0 * q3, 2.0f * q0 * q0 + 2.0f * q1 * q1 - 1.0f);};
48+
float getRoll() {
49+
if (!anglesComputed) computeAngles();
50+
return roll * 57.29578f;
51+
}
52+
float getPitch() {
53+
if (!anglesComputed) computeAngles();
54+
return pitch * 57.29578f;
55+
}
56+
float getYaw() {
57+
if (!anglesComputed) computeAngles();
58+
return yaw * 57.29578f + 180.0f;
59+
}
60+
float getRollRadians() {
61+
if (!anglesComputed) computeAngles();
62+
return roll;
63+
}
64+
float getPitchRadians() {
65+
if (!anglesComputed) computeAngles();
66+
return pitch;
67+
}
68+
float getYawRadians() {
69+
if (!anglesComputed) computeAngles();
70+
return yaw;
71+
}
4372
};
4473
#endif
4574

0 commit comments

Comments
 (0)