MadgwickAHRS.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //=====================================================================================================
  2. // MadgwickAHRS.c
  3. //=====================================================================================================
  4. //
  5. // Implementation of Madgwick's IMU and AHRS algorithms.
  6. // See: http://www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms
  7. //
  8. // Date Author Notes
  9. // 29/09/2011 SOH Madgwick Initial release
  10. // 02/10/2011 SOH Madgwick Optimised for reduced CPU load
  11. // 19/02/2012 SOH Madgwick Magnetometer measurement is normalised
  12. //
  13. //=====================================================================================================
  14. //---------------------------------------------------------------------------------------------------
  15. // Header files
  16. #include "MadgwickAHRS.h"
  17. #include <math.h>
  18. //---------------------------------------------------------------------------------------------------
  19. // Definitions
  20. #define RAD_TO_DEG 57.295779513082320876798154814105
  21. #define sampleFreq 500.0f // sample frequency in Hz
  22. #define betaDef 10.0f // 2 * proportional gain
  23. //---------------------------------------------------------------------------------------------------
  24. // Variable definitions
  25. volatile float beta = betaDef; // 2 * proportional gain (Kp)
  26. volatile static float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f; // quaternion of sensor frame relative to auxiliary frame
  27. //---------------------------------------------------------------------------------------------------
  28. // Function declarations
  29. static float invSqrt(float x);
  30. //====================================================================================================
  31. // Functions
  32. void MadgwickAHRSetBeta(float beta_in) {
  33. beta = beta_in;
  34. }
  35. //---------------------------------------------------------------------------------------------------
  36. // AHRS algorithm update
  37. void MadgwickAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
  38. float recipNorm;
  39. float s0, s1, s2, s3;
  40. float qDot1, qDot2, qDot3, qDot4;
  41. float hx, hy;
  42. 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;
  43. // Use IMU algorithm if magnetometer measurement invalid (avoids NaN in magnetometer normalisation)
  44. // if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) {
  45. // MadgwickAHRSupdateIMU(gx, gy, gz, ax, ay, az);
  46. // return;
  47. // }
  48. // Rate of change of quaternion from gyroscope
  49. qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz);
  50. qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy);
  51. qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx);
  52. qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx);
  53. // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
  54. if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  55. // Normalise accelerometer measurement
  56. recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  57. ax *= recipNorm;
  58. ay *= recipNorm;
  59. az *= recipNorm;
  60. // Normalise magnetometer measurement
  61. recipNorm = invSqrt(mx * mx + my * my + mz * mz);
  62. mx *= recipNorm;
  63. my *= recipNorm;
  64. mz *= recipNorm;
  65. // Auxiliary variables to avoid repeated arithmetic
  66. _2q0mx = 2.0f * q0 * mx;
  67. _2q0my = 2.0f * q0 * my;
  68. _2q0mz = 2.0f * q0 * mz;
  69. _2q1mx = 2.0f * q1 * mx;
  70. _2q0 = 2.0f * q0;
  71. _2q1 = 2.0f * q1;
  72. _2q2 = 2.0f * q2;
  73. _2q3 = 2.0f * q3;
  74. _2q0q2 = 2.0f * q0 * q2;
  75. _2q2q3 = 2.0f * q2 * q3;
  76. q0q0 = q0 * q0;
  77. q0q1 = q0 * q1;
  78. q0q2 = q0 * q2;
  79. q0q3 = q0 * q3;
  80. q1q1 = q1 * q1;
  81. q1q2 = q1 * q2;
  82. q1q3 = q1 * q3;
  83. q2q2 = q2 * q2;
  84. q2q3 = q2 * q3;
  85. q3q3 = q3 * q3;
  86. // Reference direction of Earth's magnetic field
  87. hx = mx * q0q0 - _2q0my * q3 + _2q0mz * q2 + mx * q1q1 + _2q1 * my * q2 + _2q1 * mz * q3 - mx * q2q2 - mx * q3q3;
  88. hy = _2q0mx * q3 + my * q0q0 - _2q0mz * q1 + _2q1mx * q2 - my * q1q1 + my * q2q2 + _2q2 * mz * q3 - my * q3q3;
  89. _2bx = sqrt(hx * hx + hy * hy);
  90. _2bz = -_2q0mx * q2 + _2q0my * q1 + mz * q0q0 + _2q1mx * q3 - mz * q1q1 + _2q2 * my * q3 - mz * q2q2 + mz * q3q3;
  91. _4bx = 2.0f * _2bx;
  92. _4bz = 2.0f * _2bz;
  93. // Gradient decent algorithm corrective step
  94. 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);
  95. 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);
  96. 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);
  97. 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);
  98. recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
  99. s0 *= recipNorm;
  100. s1 *= recipNorm;
  101. s2 *= recipNorm;
  102. s3 *= recipNorm;
  103. // Apply feedback step
  104. qDot1 -= beta * s0;
  105. qDot2 -= beta * s1;
  106. qDot3 -= beta * s2;
  107. qDot4 -= beta * s3;
  108. }
  109. // Integrate rate of change of quaternion to yield quaternion
  110. q0 += qDot1 * (1.0f / sampleFreq);
  111. q1 += qDot2 * (1.0f / sampleFreq);
  112. q2 += qDot3 * (1.0f / sampleFreq);
  113. q3 += qDot4 * (1.0f / sampleFreq);
  114. // Normalise quaternion
  115. recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
  116. q0 *= recipNorm;
  117. q1 *= recipNorm;
  118. q2 *= recipNorm;
  119. q3 *= recipNorm;
  120. }
  121. //---------------------------------------------------------------------------------------------------
  122. // IMU algorithm update
  123. void MadgwickAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az, float *pitch,float *roll,float *yaw) {
  124. float recipNorm;
  125. float s0, s1, s2, s3;
  126. float qDot1, qDot2, qDot3, qDot4;
  127. float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2 ,_8q1, _8q2, q0q0, q1q1, q2q2, q3q3;
  128. // Rate of change of quaternion from gyroscope
  129. qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz);
  130. qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy);
  131. qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx);
  132. qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx);
  133. // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
  134. if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  135. // Normalise accelerometer measurement
  136. recipNorm = invSqrt(ax * ax + ay * ay + az * az);
  137. ax *= recipNorm;
  138. ay *= recipNorm;
  139. az *= recipNorm;
  140. // Auxiliary variables to avoid repeated arithmetic
  141. _2q0 = 2.0f * q0;
  142. _2q1 = 2.0f * q1;
  143. _2q2 = 2.0f * q2;
  144. _2q3 = 2.0f * q3;
  145. _4q0 = 4.0f * q0;
  146. _4q1 = 4.0f * q1;
  147. _4q2 = 4.0f * q2;
  148. _8q1 = 8.0f * q1;
  149. _8q2 = 8.0f * q2;
  150. q0q0 = q0 * q0;
  151. q1q1 = q1 * q1;
  152. q2q2 = q2 * q2;
  153. q3q3 = q3 * q3;
  154. // Gradient decent algorithm corrective step
  155. s0 = _4q0 * q2q2 + _2q2 * ax + _4q0 * q1q1 - _2q1 * ay;
  156. s1 = _4q1 * q3q3 - _2q3 * ax + 4.0f * q0q0 * q1 - _2q0 * ay - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * az;
  157. s2 = 4.0f * q0q0 * q2 + _2q0 * ax + _4q2 * q3q3 - _2q3 * ay - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * az;
  158. s3 = 4.0f * q1q1 * q3 - _2q1 * ax + 4.0f * q2q2 * q3 - _2q2 * ay;
  159. recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
  160. s0 *= recipNorm;
  161. s1 *= recipNorm;
  162. s2 *= recipNorm;
  163. s3 *= recipNorm;
  164. // Apply feedback step
  165. qDot1 -= beta * s0;
  166. qDot2 -= beta * s1;
  167. qDot3 -= beta * s2;
  168. qDot4 -= beta * s3;
  169. }
  170. // Integrate rate of change of quaternion to yield quaternion
  171. q0 += qDot1 * (1.0f / sampleFreq);
  172. q1 += qDot2 * (1.0f / sampleFreq);
  173. q2 += qDot3 * (1.0f / sampleFreq);
  174. q3 += qDot4 * (1.0f / sampleFreq);
  175. // Normalise quaternion
  176. recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
  177. q0 *= recipNorm;
  178. q1 *= recipNorm;
  179. q2 *= recipNorm;
  180. q3 *= recipNorm;
  181. *pitch = asin(-2 * q1 * q3 + 2 * q0* q2); // pitch
  182. *roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1); // roll
  183. *yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3); //yaw
  184. *pitch *= RAD_TO_DEG;
  185. *yaw *= RAD_TO_DEG;
  186. // Declination of SparkFun Electronics (40°05'26.6"N 105°11'05.9"W) is
  187. // 8° 30' E ± 0° 21' (or 8.5°) on 2016-07-19
  188. // - http://www.ngdc.noaa.gov/geomag-web/#declination
  189. *yaw -= 8.5;
  190. *roll *= RAD_TO_DEG;
  191. }
  192. //---------------------------------------------------------------------------------------------------
  193. // Fast inverse square-root
  194. // See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
  195. static float invSqrt(float x) {
  196. float halfx = 0.5f * x;
  197. float y = x;
  198. long i = *(long*)&y;
  199. i = 0x5f3759df - (i>>1);
  200. y = *(float*)&i;
  201. y = y * (1.5f - (halfx * y * y));
  202. return y;
  203. }
  204. //====================================================================================================
  205. // END OF CODE
  206. //====================================================================================================