hal_mahonyAHRS.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //=============================================================================================
  2. // MahonyAHRS.c
  3. //=============================================================================================
  4. //
  5. // Madgwick's implementation of Mayhony's AHRS algorithm.
  6. // See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
  7. //
  8. // From the x-io website "Open-source resources available on this website are
  9. // provided under the GNU General Public Licence unless an alternative licence
  10. // is provided in source."
  11. //
  12. // Date Author Notes
  13. // 29/09/2011 SOH Madgwick Initial release
  14. // 02/10/2011 SOH Madgwick Optimised for reduced CPU load
  15. //
  16. // Algorithm paper:
  17. // http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=4608934&url=http%3A%2F%2Fieeexplore.ieee.org%2Fstamp%2Fstamp.jsp%3Ftp%3D%26arnumber%3D4608934
  18. //
  19. //=============================================================================================
  20. //-------------------------------------------------------------------------------------------
  21. // Header files
  22. #include "hal_mahonyAHRS.h"
  23. #include <math.h>
  24. #include "system.h"
  25. #include "ble_comm.h"
  26. #include "hal_ble_client.h"
  27. //-------------------------------------------------------------------------------------------
  28. // Definitions
  29. #define twoKpDef (200.0f * 0.5f) // 2 * proportional gain
  30. #define twoKiDef (0.0f * 1.0f) // 2 * integral gain
  31. //float Mahony_GetRoll(void) {return P->roll;}
  32. //float Mahony_GetPitch(void) {return P->pitch;}
  33. //float Mahony_GetYaw(void) {return P->yaw;}
  34. //void Mahony_GetAccN(float* accn){accn[0] = P->accN[1]; accn[1] = P->accN[2]; accn[2] = P->accN[3];}
  35. void Mahony_send_ANO(uint8_t fun,uint8_t* p,int len)
  36. {
  37. uint8_t buf[256];
  38. int L=0;
  39. uint8_t ver = 0;
  40. buf[L] = 0xAA; ver += buf[L++];
  41. buf[L] = 0x05; ver += buf[L++];
  42. buf[L] = 0xAF; ver += buf[L++];
  43. buf[L] = fun; ver += buf[L++];
  44. buf[L] = len; ver += buf[L++];
  45. for(int i=0;i<len;i++){
  46. buf[L] = p[i]; ver += buf[L++];
  47. }
  48. buf[L++] = ver;
  49. send_bytes_client(buf,L);
  50. }
  51. static void quaternProd(float* ab, float* a, float* b)
  52. {
  53. ab[0] = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3];
  54. ab[1] = a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2];
  55. ab[2] = a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1];
  56. ab[3] = a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0];
  57. }
  58. static void quaternConj(float* a)
  59. {
  60. a[1] = -a[1];
  61. a[2] = -a[2];
  62. a[3] = -a[3];
  63. }
  64. float Mahony_invSqrt(float x)
  65. {
  66. float halfx = 0.5f * x;
  67. float y = x;
  68. long i = *(long*)&y;
  69. i = 0x5f3759df - (i>>1);
  70. y = *(float*)&i;
  71. y = y * (1.5f - (halfx * y * y));
  72. y = y * (1.5f - (halfx * y * y));
  73. return y;
  74. }
  75. void Mahony_update(MahonyAHRS_t *P,float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz)
  76. {
  77. float recipNorm;
  78. float q0q0, q0q1, q0q2, q0q3, q1q1, q1q2, q1q3, q2q2, q2q3, q3q3;
  79. float hx, hy, bx, bz;
  80. float halfvx, halfvy, halfvz, halfwx, halfwy, halfwz;
  81. float halfex, halfey, halfez;
  82. float qa, qb, qc;
  83. //因为下面算法会改变原始值,所以先保存一份
  84. P->accN[0] = 0.0f;
  85. P->accN[1] = ax;
  86. P->accN[2] = ay;
  87. P->accN[3] = az;
  88. // Compute feedback only if accelerometer measurement valid
  89. // (avoids NaN in accelerometer normalisation)
  90. if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
  91. // Normalise accelerometer measurement
  92. recipNorm = Mahony_invSqrt(ax * ax + ay * ay + az * az);
  93. ax *= recipNorm;
  94. ay *= recipNorm;
  95. az *= recipNorm;
  96. // Normalise magnetometer measurement
  97. recipNorm = Mahony_invSqrt(mx * mx + my * my + mz * mz);
  98. mx *= recipNorm;
  99. my *= recipNorm;
  100. mz *= recipNorm;
  101. // Auxiliary variables to avoid repeated arithmetic
  102. q0q0 = P->q0 * P->q0;
  103. q0q1 = P->q0 * P->q1;
  104. q0q2 = P->q0 * P->q2;
  105. q0q3 = P->q0 * P->q3;
  106. q1q1 = P->q1 * P->q1;
  107. q1q2 = P->q1 * P->q2;
  108. q1q3 = P->q1 * P->q3;
  109. q2q2 = P->q2 * P->q2;
  110. q2q3 = P->q2 * P->q3;
  111. q3q3 = P->q3 * P->q3;
  112. // Reference direction of Earth's magnetic field
  113. hx = 2.0f * (mx * (0.5f - q2q2 - q3q3) + my * (q1q2 - q0q3) + mz * (q1q3 + q0q2));
  114. hy = 2.0f * (mx * (q1q2 + q0q3) + my * (0.5f - q1q1 - q3q3) + mz * (q2q3 - q0q1));
  115. bx = sqrtf(hx * hx + hy * hy);
  116. bz = 2.0f * (mx * (q1q3 - q0q2) + my * (q2q3 + q0q1) + mz * (0.5f - q1q1 - q2q2));
  117. // Estimated direction of gravity and magnetic field
  118. halfvx = q1q3 - q0q2;
  119. halfvy = q0q1 + q2q3;
  120. halfvz = q0q0 - 0.5f + q3q3;
  121. halfwx = bx * (0.5f - q2q2 - q3q3) + bz * (q1q3 - q0q2);
  122. halfwy = bx * (q1q2 - q0q3) + bz * (q0q1 + q2q3);
  123. halfwz = bx * (q0q2 + q1q3) + bz * (0.5f - q1q1 - q2q2);
  124. // Error is sum of cross product between estimated direction
  125. // and measured direction of field vectors
  126. halfex = (ay * halfvz - az * halfvy) + (my * halfwz - mz * halfwy);
  127. halfey = (az * halfvx - ax * halfvz) + (mz * halfwx - mx * halfwz);
  128. halfez = (ax * halfvy - ay * halfvx) + (mx * halfwy - my * halfwx);
  129. // Compute and apply integral feedback if enabled
  130. if(P->twoKi > 0.0f) {
  131. // integral error scaled by Ki
  132. P->integralFBx += P->twoKi * halfex * P->invSampleFreq;
  133. P->integralFBy += P->twoKi * halfey * P->invSampleFreq;
  134. P->integralFBz += P->twoKi * halfez * P->invSampleFreq;
  135. gx += P->integralFBx; // apply integral feedback
  136. gy += P->integralFBy;
  137. gz += P->integralFBz;
  138. } else {
  139. P->integralFBx = 0.0f; // prevent integral windup
  140. P->integralFBy = 0.0f;
  141. P->integralFBz = 0.0f;
  142. }
  143. // Apply proportional feedback
  144. gx += P->twoKp * halfex;
  145. gy += P->twoKp * halfey;
  146. gz += P->twoKp * halfez;
  147. // SEGGER_RTT_printf(0,"P->twoKp %d\n",(int)(P->twoKp*100));
  148. }
  149. // Integrate rate of change of quaternion
  150. gx *= (0.5f * P->invSampleFreq); // pre-multiply common factors
  151. gy *= (0.5f * P->invSampleFreq);
  152. gz *= (0.5f * P->invSampleFreq);
  153. qa = P->q0;
  154. qb = P->q1;
  155. qc = P->q2;
  156. P->q0 += (-qb * gx - qc * gy - P->q3 * gz);
  157. P->q1 += (qa * gx + qc * gz - P->q3 * gy);
  158. P->q2 += (qa * gy - qb * gz + P->q3 * gx);
  159. P->q3 += (qa * gz + qb * gy - qc * gx);
  160. // Normalise quaternion
  161. recipNorm = Mahony_invSqrt(P->q0 * P->q0 + P->q1 * P->q1 + P->q2 * P->q2 + P->q3 * P->q3);
  162. P->q0 *= recipNorm;
  163. P->q1 *= recipNorm;
  164. P->q2 *= recipNorm;
  165. P->q3 *= recipNorm;
  166. //计算三个角度
  167. P->roll = atan2f(P->q0*P->q1 + P->q2*P->q3, 0.5f - P->q1*P->q1 - P->q2*P->q2) * 57.29578f;;
  168. P->pitch = asinf(-2.0f * (P->q1*P->q3 - P->q0*P->q2)) * 57.29578f;;
  169. P->yaw = atan2f(P->q1*P->q2 + P->q0*P->q3, 0.5f - P->q2*P->q2 - P->q3*P->q3) * 57.29578f;;
  170. //坐标系转换
  171. P->q[0] = P->q0;
  172. P->q[1] = P->q1;
  173. P->q[2] = P->q2;
  174. P->q[3] = P->q3;
  175. float tmp_vector2[4];
  176. quaternProd(tmp_vector2, P->q, P->accN);
  177. quaternConj(P->q);
  178. quaternProd(P->accN, tmp_vector2, P->q);
  179. P->accN[3] = P->accN[3] - 1.0f;//去重力
  180. }
  181. void Mahony_SetKp(MahonyAHRS_t *P,float twoKp1)
  182. {
  183. P->twoKp = twoKp1; // 2 * integral gain (Ki
  184. }
  185. void Mahony_Init(MahonyAHRS_t *P,float sampleFrequency)
  186. {
  187. P->twoKi = twoKiDef; // 2 * integral gain (Ki)
  188. P->twoKp = twoKpDef;
  189. P->q0 = 1.0f;
  190. P->q1 = 0.0f;
  191. P->q2 = 0.0f;
  192. P->q3 = 0.0f;
  193. P->integralFBx = 0.0f;
  194. P->integralFBy = 0.0f;
  195. P->integralFBz = 0.0f;
  196. P->invSampleFreq = 1.0f / sampleFrequency;
  197. }
  198. //============================================================================================
  199. // END OF CODE
  200. //============================================================================================