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: NerfGun_nRF24L01P_TX_9d0f
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 "mbed.h" 00019 #include "MadgwickAHRS.h" 00020 #include <math.h> 00021 00022 //--------------------------------------------------------------------------------------------------- 00023 // Definitions 00024 00025 //#define sampleFreq 512.0f // sample frequency in Hz 00026 #define betaDef 0.2f //2* proportional gain 00027 #define PI 3.14159265359f 00028 00029 //--------------------------------------------------------------------------------------------------- 00030 00031 MadgwickAHRS::MadgwickAHRS(float Freq){ 00032 00033 sampleFreq = Freq; 00034 00035 } 00036 00037 float beta = betaDef; // 2 * proportional gain (Kp) 00038 float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f; // quaternion of sensor frame relative to auxiliary frame 00039 00040 float invSqrt(float x); 00041 00042 //==================================================================================================== 00043 // Functions 00044 00045 //--------------------------------------------------------------------------------------------------- 00046 // AHRS algorithm update 00047 00048 void MadgwickAHRS::update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) { 00049 float recipNorm; 00050 float s0, s1, s2, s3; 00051 float qDot1, qDot2, qDot3, qDot4; 00052 float hx, hy; 00053 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; 00054 00055 // Use IMU algorithm if magnetometer measurement invalid (avoids NaN in magnetometer normalisation) 00056 if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) { 00057 MadgwickAHRS::updateIMU(gx, gy, gz, ax, ay, az); 00058 return; 00059 } 00060 00061 // Rate of change of quaternion from gyroscope 00062 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz); 00063 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy); 00064 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx); 00065 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx); 00066 00067 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) 00068 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) { 00069 00070 // Normalise accelerometer measurement 00071 recipNorm = invSqrt(ax * ax + ay * ay + az * az); 00072 ax *= recipNorm; 00073 ay *= recipNorm; 00074 az *= recipNorm; 00075 00076 // Normalise magnetometer measurement 00077 recipNorm = invSqrt(mx * mx + my * my + mz * mz); 00078 mx *= recipNorm; 00079 my *= recipNorm; 00080 mz *= recipNorm; 00081 00082 // Auxiliary variables to avoid repeated arithmetic 00083 _2q0mx = 2.0f * q0 * mx; 00084 _2q0my = 2.0f * q0 * my; 00085 _2q0mz = 2.0f * q0 * mz; 00086 _2q1mx = 2.0f * q1 * mx; 00087 _2q0 = 2.0f * q0; 00088 _2q1 = 2.0f * q1; 00089 _2q2 = 2.0f * q2; 00090 _2q3 = 2.0f * q3; 00091 _2q0q2 = 2.0f * q0 * q2; 00092 _2q2q3 = 2.0f * q2 * q3; 00093 q0q0 = q0 * q0; 00094 q0q1 = q0 * q1; 00095 q0q2 = q0 * q2; 00096 q0q3 = q0 * q3; 00097 q1q1 = q1 * q1; 00098 q1q2 = q1 * q2; 00099 q1q3 = q1 * q3; 00100 q2q2 = q2 * q2; 00101 q2q3 = q2 * q3; 00102 q3q3 = q3 * q3; 00103 00104 // Reference direction of Earth's magnetic field 00105 hx = mx * q0q0 - _2q0my * q3 + _2q0mz * q2 + mx * q1q1 + _2q1 * my * q2 + _2q1 * mz * q3 - mx * q2q2 - mx * q3q3; 00106 hy = _2q0mx * q3 + my * q0q0 - _2q0mz * q1 + _2q1mx * q2 - my * q1q1 + my * q2q2 + _2q2 * mz * q3 - my * q3q3; 00107 _2bx = sqrt(hx * hx + hy * hy); 00108 _2bz = -_2q0mx * q2 + _2q0my * q1 + mz * q0q0 + _2q1mx * q3 - mz * q1q1 + _2q2 * my * q3 - mz * q2q2 + mz * q3q3; 00109 _4bx = 2.0f * _2bx; 00110 _4bz = 2.0f * _2bz; 00111 00112 // Gradient decent algorithm corrective step 00113 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); 00114 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); 00115 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); 00116 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); 00117 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude 00118 s0 *= recipNorm; 00119 s1 *= recipNorm; 00120 s2 *= recipNorm; 00121 s3 *= recipNorm; 00122 00123 // Apply feedback step 00124 qDot1 -= beta * s0; 00125 qDot2 -= beta * s1; 00126 qDot3 -= beta * s2; 00127 qDot4 -= beta * s3; 00128 } 00129 00130 // Integrate rate of change of quaternion to yield quaternion 00131 q0 += qDot1 * (1.0f / sampleFreq); 00132 q1 += qDot2 * (1.0f / sampleFreq); 00133 q2 += qDot3 * (1.0f / sampleFreq); 00134 q3 += qDot4 * (1.0f / sampleFreq); 00135 00136 // Normalise quaternion 00137 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); 00138 q0 *= recipNorm; 00139 q1 *= recipNorm; 00140 q2 *= recipNorm; 00141 q3 *= recipNorm; 00142 } 00143 00144 //--------------------------------------------------------------------------------------------------- 00145 // IMU algorithm update 00146 00147 void MadgwickAHRS::updateIMU(float gx, float gy, float gz, float ax, float ay, float az) { 00148 float recipNorm; 00149 float s0, s1, s2, s3; 00150 float qDot1, qDot2, qDot3, qDot4; 00151 float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2 ,_8q1, _8q2, q0q0, q1q1, q2q2, q3q3; 00152 00153 // Rate of change of quaternion from gyroscope 00154 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz); 00155 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy); 00156 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx); 00157 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx); 00158 00159 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) 00160 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) { 00161 00162 // Normalise accelerometer measurement 00163 recipNorm = invSqrt(ax * ax + ay * ay + az * az); 00164 ax *= recipNorm; 00165 ay *= recipNorm; 00166 az *= recipNorm; 00167 00168 // Auxiliary variables to avoid repeated arithmetic 00169 _2q0 = 2.0f * q0; 00170 _2q1 = 2.0f * q1; 00171 _2q2 = 2.0f * q2; 00172 _2q3 = 2.0f * q3; 00173 _4q0 = 4.0f * q0; 00174 _4q1 = 4.0f * q1; 00175 _4q2 = 4.0f * q2; 00176 _8q1 = 8.0f * q1; 00177 _8q2 = 8.0f * q2; 00178 q0q0 = q0 * q0; 00179 q1q1 = q1 * q1; 00180 q2q2 = q2 * q2; 00181 q3q3 = q3 * q3; 00182 00183 // Gradient decent algorithm corrective step 00184 s0 = _4q0 * q2q2 + _2q2 * ax + _4q0 * q1q1 - _2q1 * ay; 00185 s1 = _4q1 * q3q3 - _2q3 * ax + 4.0f * q0q0 * q1 - _2q0 * ay - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * az; 00186 s2 = 4.0f * q0q0 * q2 + _2q0 * ax + _4q2 * q3q3 - _2q3 * ay - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * az; 00187 s3 = 4.0f * q1q1 * q3 - _2q1 * ax + 4.0f * q2q2 * q3 - _2q2 * ay; 00188 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude 00189 s0 *= recipNorm; 00190 s1 *= recipNorm; 00191 s2 *= recipNorm; 00192 s3 *= recipNorm; 00193 00194 // Apply feedback step 00195 qDot1 -= beta * s0; 00196 qDot2 -= beta * s1; 00197 qDot3 -= beta * s2; 00198 qDot4 -= beta * s3; 00199 } 00200 00201 // Integrate rate of change of quaternion to yield quaternion 00202 q0 += qDot1 * (1.0f / sampleFreq); 00203 q1 += qDot2 * (1.0f / sampleFreq); 00204 q2 += qDot3 * (1.0f / sampleFreq); 00205 q3 += qDot4 * (1.0f / sampleFreq); 00206 00207 // Normalise quaternion 00208 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); 00209 q0 *= recipNorm; 00210 q1 *= recipNorm; 00211 q2 *= recipNorm; 00212 q3 *= recipNorm; 00213 } 00214 00215 //--------------------------------------------------------------------------------------------------- 00216 // Fast inverse square-root 00217 // See: http://en.wikipedia.org/wiki/Fast_inverse_square_root 00218 00219 float invSqrt(float x) { 00220 float halfx = 0.5f * x; 00221 float y = x; 00222 long i = *(long*)&y; 00223 i = 0x5f3759df - (i>>1); 00224 y = *(float*)&i; 00225 y = y * (1.5f - (halfx * y * y)); 00226 return y; 00227 //return 1.0/sqrt(x); 00228 } 00229 00230 00231 void MadgwickAHRS::getEuler(){ 00232 00233 float gx = 2*(q1*q3 - q0*q2); 00234 float gy = 2 * (q0*q1 + q2*q3); 00235 float gz = q0*q0 - q1*q1 - q2*q2 + q3*q3; 00236 00237 //roll = atan2(2*(q0*q1 + q2*q3), 1 - 2*(q1*q1 + q2*q2)); 00238 //pitch = asin(2*(q0*q2 - q3*q1)); 00239 //yaw = atan2(2*(q0*q3 + q1*q2), 1 - 2*(q2*q2 + q3*q3));; 00240 00241 roll = atan(gy / sqrt(gx*gx + gz*gz)); 00242 pitch = atan(gx / sqrt(gy*gy + gz*gz)); 00243 yaw = atan2(2 * q1 * q2 - 2 * q0 * q3, 2 * q0*q0 + 2 * q1 * q1 - 1); 00244 00245 roll = roll*180/PI; 00246 pitch = pitch*180/PI; 00247 yaw = yaw*180/PI; 00248 00249 /*roll = roll*1000; 00250 pitch = pitch*1000; 00251 yaw = yaw*1000;*/ 00252 00253 if (ceil(roll) - roll <= .5){ 00254 roll = ceil(roll); 00255 } 00256 else{ 00257 roll = floor(roll); 00258 } 00259 00260 if (ceil(pitch) - pitch <= .5){ 00261 pitch = ceil(pitch); 00262 } 00263 else{ 00264 pitch = floor(pitch); 00265 } 00266 00267 if (ceil(yaw) - yaw <= .5){ 00268 yaw = ceil(yaw); 00269 } 00270 else{ 00271 yaw = floor(yaw); 00272 } 00273 00274 //printf("Roll: %6.2f, Pitch: %6.2f, Yaw: %6.2f\r\n", roll, pitch, yaw); 00275 } 00276 00277 int16_t MadgwickAHRS::getRoll(){ 00278 return (int16_t)roll; 00279 } 00280 00281 int16_t MadgwickAHRS::getPitch(){ 00282 return (int16_t)pitch; 00283 } 00284 00285 int16_t MadgwickAHRS::getYaw(){ 00286 return (int16_t)yaw; 00287 } 00288 00289 //==================================================================================================== 00290 // END OF CODE 00291 //====================================================================================================
Generated on Tue Jul 19 2022 20:48:36 by
1.7.2