Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IMU_ethernet IMU_serial_test IMU_serial
MadgwickAHRS.cpp
00001 //===================================================================================================== 00002 // MadgwickAHRS.c 00003 //===================================================================================================== 00004 // 00005 // Implementation of Madgwick's IMU and AHRS algorithms. 00006 // See: http://www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms 00007 // 00008 // Date Author Notes 00009 // 29/09/2011 SOH Madgwick Initial release 00010 // 02/10/2011 SOH Madgwick Optimised for reduced CPU load 00011 // 19/02/2012 SOH Madgwick Magnetometer measurement is normalised 00012 // 00013 //===================================================================================================== 00014 00015 //--------------------------------------------------------------------------------------------------- 00016 // Header files 00017 00018 #include "MadgwickAHRS.h" 00019 #include <math.h> 00020 00021 //--------------------------------------------------------------------------------------------------- 00022 // Definitions 00023 00024 #define sampleFreq 28.0f // sample frequency in Hz 00025 #define betaDef 0.9f // 2 * proportional gain 00026 00027 //--------------------------------------------------------------------------------------------------- 00028 // Variable definitions 00029 00030 volatile float beta = betaDef; // 2 * proportional gain (Kp) 00031 //volatile float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f; // quaternion of sensor frame relative to auxiliary frame 00032 00033 //--------------------------------------------------------------------------------------------------- 00034 // Function declarations 00035 00036 float invSqrt(float x); 00037 00038 //==================================================================================================== 00039 // Functions 00040 00041 //--------------------------------------------------------------------------------------------------- 00042 // AHRS algorithm update 00043 00044 void MadgwickAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, float &q0, float &q1, float &q2, float &q3) 00045 { 00046 float recipNorm; 00047 float s0, s1, s2, s3; 00048 float qDot1, qDot2, qDot3, qDot4; 00049 float hx, hy; 00050 float _2q0mx, _2q0my, _2q0mz, _2q1mx, _2bx, _2bz, _4bx, _4bz, _2q0, _2q1, _2q2, _2q3, _2q0q2, _2q2q3, q0q0, q0q1, q0q2, q0q3, q1q1, q1q2, q1q3, q2q2, q2q3, q3q3; 00051 00052 // Use IMU algorithm if magnetometer measurement invalid (avoids NaN in magnetometer normalisation) 00053 if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) { 00054 MadgwickAHRSupdateIMU(gx, gy, gz, ax, ay, az, q0, q1, q2, q3); 00055 return; 00056 } 00057 00058 // Rate of change of quaternion from gyroscope 00059 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz); 00060 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy); 00061 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx); 00062 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx); 00063 00064 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) 00065 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) { 00066 00067 // Normalise accelerometer measurement 00068 recipNorm = invSqrt(ax * ax + ay * ay + az * az); 00069 ax *= recipNorm; 00070 ay *= recipNorm; 00071 az *= recipNorm; 00072 00073 // Normalise magnetometer measurement 00074 recipNorm = invSqrt(mx * mx + my * my + mz * mz); 00075 mx *= recipNorm; 00076 my *= recipNorm; 00077 mz *= recipNorm; 00078 00079 // Auxiliary variables to avoid repeated arithmetic 00080 _2q0mx = 2.0f * q0 * mx; 00081 _2q0my = 2.0f * q0 * my; 00082 _2q0mz = 2.0f * q0 * mz; 00083 _2q1mx = 2.0f * q1 * mx; 00084 _2q0 = 2.0f * q0; 00085 _2q1 = 2.0f * q1; 00086 _2q2 = 2.0f * q2; 00087 _2q3 = 2.0f * q3; 00088 _2q0q2 = 2.0f * q0 * q2; 00089 _2q2q3 = 2.0f * q2 * q3; 00090 q0q0 = q0 * q0; 00091 q0q1 = q0 * q1; 00092 q0q2 = q0 * q2; 00093 q0q3 = q0 * q3; 00094 q1q1 = q1 * q1; 00095 q1q2 = q1 * q2; 00096 q1q3 = q1 * q3; 00097 q2q2 = q2 * q2; 00098 q2q3 = q2 * q3; 00099 q3q3 = q3 * q3; 00100 00101 // Reference direction of Earth's magnetic field 00102 hx = mx * q0q0 - _2q0my * q3 + _2q0mz * q2 + mx * q1q1 + _2q1 * my * q2 + _2q1 * mz * q3 - mx * q2q2 - mx * q3q3; 00103 hy = _2q0mx * q3 + my * q0q0 - _2q0mz * q1 + _2q1mx * q2 - my * q1q1 + my * q2q2 + _2q2 * mz * q3 - my * q3q3; 00104 _2bx = sqrt(hx * hx + hy * hy); 00105 _2bz = -_2q0mx * q2 + _2q0my * q1 + mz * q0q0 + _2q1mx * q3 - mz * q1q1 + _2q2 * my * q3 - mz * q2q2 + mz * q3q3; 00106 _4bx = 2.0f * _2bx; 00107 _4bz = 2.0f * _2bz; 00108 00109 // Gradient decent algorithm corrective step 00110 s0 = -_2q2 * (2.0f * q1q3 - _2q0q2 - ax) + _2q1 * (2.0f * q0q1 + _2q2q3 - ay) - _2bz * q2 * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (-_2bx * q3 + _2bz * q1) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + _2bx * q2 * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz); 00111 s1 = _2q3 * (2.0f * q1q3 - _2q0q2 - ax) + _2q0 * (2.0f * q0q1 + _2q2q3 - ay) - 4.0f * q1 * (1 - 2.0f * q1q1 - 2.0f * q2q2 - az) + _2bz * q3 * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (_2bx * q2 + _2bz * q0) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + (_2bx * q3 - _4bz * q1) * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz); 00112 s2 = -_2q0 * (2.0f * q1q3 - _2q0q2 - ax) + _2q3 * (2.0f * q0q1 + _2q2q3 - ay) - 4.0f * q2 * (1 - 2.0f * q1q1 - 2.0f * q2q2 - az) + (-_4bx * q2 - _2bz * q0) * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (_2bx * q1 + _2bz * q3) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + (_2bx * q0 - _4bz * q2) * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz); 00113 s3 = _2q1 * (2.0f * q1q3 - _2q0q2 - ax) + _2q2 * (2.0f * q0q1 + _2q2q3 - ay) + (-_4bx * q3 + _2bz * q1) * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (-_2bx * q0 + _2bz * q2) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + _2bx * q1 * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz); 00114 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude 00115 s0 *= recipNorm; 00116 s1 *= recipNorm; 00117 s2 *= recipNorm; 00118 s3 *= recipNorm; 00119 00120 // Apply feedback step 00121 qDot1 -= beta * s0; 00122 qDot2 -= beta * s1; 00123 qDot3 -= beta * s2; 00124 qDot4 -= beta * s3; 00125 } 00126 00127 // Integrate rate of change of quaternion to yield quaternion 00128 q0 += qDot1 * (1.0f / sampleFreq); 00129 q1 += qDot2 * (1.0f / sampleFreq); 00130 q2 += qDot3 * (1.0f / sampleFreq); 00131 q3 += qDot4 * (1.0f / sampleFreq); 00132 00133 // Normalise quaternion 00134 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); 00135 q0 *= recipNorm; 00136 q1 *= recipNorm; 00137 q2 *= recipNorm; 00138 q3 *= recipNorm; 00139 } 00140 00141 //--------------------------------------------------------------------------------------------------- 00142 // IMU algorithm update 00143 00144 void MadgwickAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az, float &q0, float &q1, float &q2, float &q3) 00145 { 00146 float recipNorm; 00147 float s0, s1, s2, s3; 00148 float qDot1, qDot2, qDot3, qDot4; 00149 float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2 ,_8q1, _8q2, q0q0, q1q1, q2q2, q3q3; 00150 00151 // Rate of change of quaternion from gyroscope 00152 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz); 00153 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy); 00154 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx); 00155 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx); 00156 00157 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) 00158 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) { 00159 00160 // Normalise accelerometer measurement 00161 recipNorm = invSqrt(ax * ax + ay * ay + az * az); 00162 ax *= recipNorm; 00163 ay *= recipNorm; 00164 az *= recipNorm; 00165 00166 // Auxiliary variables to avoid repeated arithmetic 00167 _2q0 = 2.0f * q0; 00168 _2q1 = 2.0f * q1; 00169 _2q2 = 2.0f * q2; 00170 _2q3 = 2.0f * q3; 00171 _4q0 = 4.0f * q0; 00172 _4q1 = 4.0f * q1; 00173 _4q2 = 4.0f * q2; 00174 _8q1 = 8.0f * q1; 00175 _8q2 = 8.0f * q2; 00176 q0q0 = q0 * q0; 00177 q1q1 = q1 * q1; 00178 q2q2 = q2 * q2; 00179 q3q3 = q3 * q3; 00180 00181 // Gradient decent algorithm corrective step 00182 s0 = _4q0 * q2q2 + _2q2 * ax + _4q0 * q1q1 - _2q1 * ay; 00183 s1 = _4q1 * q3q3 - _2q3 * ax + 4.0f * q0q0 * q1 - _2q0 * ay - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * az; 00184 s2 = 4.0f * q0q0 * q2 + _2q0 * ax + _4q2 * q3q3 - _2q3 * ay - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * az; 00185 s3 = 4.0f * q1q1 * q3 - _2q1 * ax + 4.0f * q2q2 * q3 - _2q2 * ay; 00186 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude 00187 s0 *= recipNorm; 00188 s1 *= recipNorm; 00189 s2 *= recipNorm; 00190 s3 *= recipNorm; 00191 00192 // Apply feedback step 00193 qDot1 -= beta * s0; 00194 qDot2 -= beta * s1; 00195 qDot3 -= beta * s2; 00196 qDot4 -= beta * s3; 00197 } 00198 00199 // Integrate rate of change of quaternion to yield quaternion 00200 q0 += qDot1 * (1.0f / sampleFreq); 00201 q1 += qDot2 * (1.0f / sampleFreq); 00202 q2 += qDot3 * (1.0f / sampleFreq); 00203 q3 += qDot4 * (1.0f / sampleFreq); 00204 00205 // Normalise quaternion 00206 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); 00207 q0 *= recipNorm; 00208 q1 *= recipNorm; 00209 q2 *= recipNorm; 00210 q3 *= recipNorm; 00211 } 00212 00213 //--------------------------------------------------------------------------------------------------- 00214 // Fast inverse square-root 00215 // See: http://en.wikipedia.org/wiki/Fast_inverse_square_root 00216 00217 float invSqrt(float x) 00218 { 00219 float halfx = 0.5f * x; 00220 float y = x; 00221 long i = *(long*)&y; 00222 i = 0x5f3759df - (i>>1); 00223 y = *(float*)&i; 00224 y = y * (1.5f - (halfx * y * y)); 00225 return y; 00226 } 00227 00228 //==================================================================================================== 00229 // END OF CODE 00230 //====================================================================================================
Generated on Fri Jul 15 2022 02:35:37 by
