Skip to content

Commit 099d023

Browse files
Ability to set the sample frequency
1 parent ab62100 commit 099d023

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/MadgwickAHRS.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//=====================================================================================================
1+
//=============================================================================================
22
// MadgwickAHRS.c
3-
//=====================================================================================================
3+
//=============================================================================================
44
//
55
// Implementation of Madgwick's IMU and AHRS algorithms.
66
// See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
@@ -14,23 +14,25 @@
1414
// 02/10/2011 SOH Madgwick Optimised for reduced CPU load
1515
// 19/02/2012 SOH Madgwick Magnetometer measurement is normalised
1616
//
17-
//=====================================================================================================
17+
//=============================================================================================
1818

19-
//---------------------------------------------------------------------------------------------------
19+
//-------------------------------------------------------------------------------------------
2020
// Header files
2121

2222
#include "MadgwickAHRS.h"
2323
#include <math.h>
2424

25-
//---------------------------------------------------------------------------------------------------
25+
//-------------------------------------------------------------------------------------------
2626
// Definitions
2727

28+
#define sampleFreqDef 512.0f // sample frequency in Hz
29+
#define betaDef 0.1f // 2 * proportional gain
2830

2931

30-
//====================================================================================================
32+
//============================================================================================
3133
// Functions
3234

33-
//---------------------------------------------------------------------------------------------------
35+
//-------------------------------------------------------------------------------------------
3436
// AHRS algorithm update
3537

3638
Madgwick::Madgwick() {
@@ -39,6 +41,7 @@ Madgwick::Madgwick() {
3941
q1 = 0.0f;
4042
q2 = 0.0f;
4143
q3 = 0.0f;
44+
invSampleFreq = 1.0f / sampleFreqDef;
4245
}
4346

4447
void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
@@ -67,7 +70,7 @@ void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az
6770
recipNorm = invSqrt(ax * ax + ay * ay + az * az);
6871
ax *= recipNorm;
6972
ay *= recipNorm;
70-
az *= recipNorm;
73+
az *= recipNorm;
7174

7275
// Normalise magnetometer measurement
7376
recipNorm = invSqrt(mx * mx + my * my + mz * mz);
@@ -124,10 +127,10 @@ void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az
124127
}
125128

126129
// Integrate rate of change of quaternion to yield quaternion
127-
q0 += qDot1 * (1.0f / sampleFreq);
128-
q1 += qDot2 * (1.0f / sampleFreq);
129-
q2 += qDot3 * (1.0f / sampleFreq);
130-
q3 += qDot4 * (1.0f / sampleFreq);
130+
q0 += qDot1 * invSampleFreq;
131+
q1 += qDot2 * invSampleFreq;
132+
q2 += qDot3 * invSampleFreq;
133+
q3 += qDot4 * invSampleFreq;
131134

132135
// Normalise quaternion
133136
recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
@@ -137,7 +140,7 @@ void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az
137140
q3 *= recipNorm;
138141
}
139142

140-
//---------------------------------------------------------------------------------------------------
143+
//-------------------------------------------------------------------------------------------
141144
// IMU algorithm update
142145

143146
void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float az) {
@@ -195,10 +198,10 @@ void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float
195198
}
196199

197200
// Integrate rate of change of quaternion to yield quaternion
198-
q0 += qDot1 * (1.0f / sampleFreq);
199-
q1 += qDot2 * (1.0f / sampleFreq);
200-
q2 += qDot3 * (1.0f / sampleFreq);
201-
q3 += qDot4 * (1.0f / sampleFreq);
201+
q0 += qDot1 * invSampleFreq;
202+
q1 += qDot2 * invSampleFreq;
203+
q2 += qDot3 * invSampleFreq;
204+
q3 += qDot4 * invSampleFreq;
202205

203206
// Normalise quaternion
204207
recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
@@ -208,7 +211,7 @@ void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float
208211
q3 *= recipNorm;
209212
}
210213

211-
//---------------------------------------------------------------------------------------------------
214+
//-------------------------------------------------------------------------------------------
212215
// Fast inverse square-root
213216
// See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
214217

@@ -222,6 +225,6 @@ float Madgwick::invSqrt(float x) {
222225
return y;
223226
}
224227

225-
//====================================================================================================
228+
//============================================================================================
226229
// END OF CODE
227-
//====================================================================================================
230+
//============================================================================================

src/MadgwickAHRS.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//=====================================================================================================
1+
//=============================================================================================
22
// MadgwickAHRS.h
3-
//=====================================================================================================
3+
//=============================================================================================
44
//
55
// Implementation of Madgwick's IMU and AHRS algorithms.
66
// See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
@@ -13,14 +13,12 @@
1313
// 29/09/2011 SOH Madgwick Initial release
1414
// 02/10/2011 SOH Madgwick Optimised for reduced CPU load
1515
//
16-
//=====================================================================================================
16+
//=============================================================================================
1717
#ifndef MadgwickAHRS_h
1818
#define MadgwickAHRS_h
1919
#include <math.h>
20-
#define sampleFreq 512.0f // sample frequency in Hz
21-
#define betaDef 0.1f // 2 * proportional gain
2220

23-
//----------------------------------------------------------------------------------------------------
21+
//--------------------------------------------------------------------------------------------
2422
// Variable declaration
2523
class Madgwick{
2624
private:
@@ -30,11 +28,13 @@ class Madgwick{
3028
float q1;
3129
float q2;
3230
float q3; // quaternion of sensor frame relative to auxiliary frame
31+
float invSampleFreq;
3332

34-
//---------------------------------------------------------------------------------------------------
33+
//-------------------------------------------------------------------------------------------
3534
// Function declarations
3635
public:
3736
Madgwick(void);
37+
void begin(float sampleFrequency) { invSampleFreq = 1.0f / sampleFrequency; }
3838
void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
3939
void updateIMU(float gx, float gy, float gz, float ax, float ay, float az);
4040
float getPitch(){return atan2f(2.0f * q2 * q3 - 2.0f * q0 * q1, 2.0f * q0 * q0 + 2.0f * q3 * q3 - 1.0f);};
@@ -43,6 +43,3 @@ class Madgwick{
4343
};
4444
#endif
4545

46-
//=====================================================================================================
47-
// End of file
48-
//=====================================================================================================

0 commit comments

Comments
 (0)