hal_ano.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_ano.h"
  23. #include <math.h>
  24. #include "system.h"
  25. #include "hal_mahonyAHRS.h"
  26. extern unsigned int send_bytes_client(unsigned char *bytes, uint16_t len);
  27. void ANO_Send(uint8_t fun,uint8_t* p,int len)
  28. {
  29. uint8_t buf[256];
  30. int L=0;
  31. uint8_t ver = 0;
  32. buf[L] = 0xAA; ver += buf[L++];
  33. buf[L] = 0x05; ver += buf[L++];
  34. buf[L] = 0xAF; ver += buf[L++];
  35. buf[L] = fun; ver += buf[L++];
  36. buf[L] = len; ver += buf[L++];
  37. for(int i=0;i<len;i++){
  38. buf[L] = p[i]; ver += buf[L++];
  39. }
  40. buf[L++] = ver;
  41. send_bytes_client(buf,L);
  42. }
  43. void ANO_Send_Status(float rol,float pit,float yaw,int32_t hight,int8_t mode,int8_t lock,int8_t link)
  44. {
  45. uint8_t buf[32];
  46. uint8_t L=0;
  47. int16_t rol_100 = (int16_t)(rol*100);
  48. int16_t pit_100 = (int16_t)(pit*100);
  49. int16_t yaw_100 = (int16_t)(yaw*100);
  50. buf[L++] = (uint8_t)(rol_100>>8);
  51. buf[L++] = (uint8_t)(rol_100>>0);
  52. buf[L++] = (uint8_t)(pit_100>>8);
  53. buf[L++] = (uint8_t)(pit_100>>0);
  54. buf[L++] = (uint8_t)(yaw_100>>8);
  55. buf[L++] = (uint8_t)(yaw_100>>0);
  56. buf[L++] = (uint8_t)(hight>>24);
  57. buf[L++] = (uint8_t)(hight>>16);
  58. buf[L++] = (uint8_t)(hight>>8);
  59. buf[L++] = (uint8_t)(hight>>0);
  60. buf[L++] = mode;
  61. buf[L++] = lock;
  62. buf[L++] = link;
  63. // SEGGER_RTT_printf(0,"rol=%d,pitch=%d,yaw=%d,hight=%d,mode=%d,lock=%d,link=%d\n",rol_100,pit_100,yaw_100,hight,mode,lock,link);
  64. ANO_Send(0x01,buf,L);
  65. }
  66. void ANO_Send_Senser(int16_t ax, int16_t ay, int16_t az, int16_t gx, int16_t gy, int16_t gz, int16_t mx, int16_t my, int16_t mz)
  67. {
  68. uint8_t buf[32];
  69. uint8_t L=0;
  70. buf[L++] = (uint8_t)(ax>>8);
  71. buf[L++] = (uint8_t)(ax>>0);
  72. buf[L++] = (uint8_t)(ay>>8);
  73. buf[L++] = (uint8_t)(ay>>0);
  74. buf[L++] = (uint8_t)(az>>8);
  75. buf[L++] = (uint8_t)(az>>0);
  76. buf[L++] = (uint8_t)(gx>>8);
  77. buf[L++] = (uint8_t)(gx>>0);
  78. buf[L++] = (uint8_t)(gy>>8);
  79. buf[L++] = (uint8_t)(gy>>0);
  80. buf[L++] = (uint8_t)(gz>>8);
  81. buf[L++] = (uint8_t)(gz>>0);
  82. buf[L++] = (uint8_t)(mx>>8);
  83. buf[L++] = (uint8_t)(mx>>0);
  84. buf[L++] = (uint8_t)(my>>8);
  85. buf[L++] = (uint8_t)(my>>0);
  86. buf[L++] = (uint8_t)(mz>>8);
  87. buf[L++] = (uint8_t)(mz>>0);
  88. ANO_Send(0x02,buf,L);
  89. }
  90. void ANO_Send_RCData(int16_t thr, int16_t yaw, int16_t rol, int16_t pit, int16_t aux1, int16_t aux2, int16_t aux3, int16_t aux4, int16_t aux5, int16_t aux6)
  91. {
  92. uint8_t buf[32];
  93. uint8_t L=0;
  94. buf[L++] = (uint8_t)(thr>>8);
  95. buf[L++] = (uint8_t)(thr>>0);
  96. buf[L++] = (uint8_t)(yaw>>8);
  97. buf[L++] = (uint8_t)(yaw>>0);
  98. buf[L++] = (uint8_t)(rol>>8);
  99. buf[L++] = (uint8_t)(rol>>0);
  100. buf[L++] = (uint8_t)(pit>>8);
  101. buf[L++] = (uint8_t)(pit>>0);
  102. buf[L++] = (uint8_t)(aux1>>8);
  103. buf[L++] = (uint8_t)(aux1>>0);
  104. buf[L++] = (uint8_t)(aux2>>8);
  105. buf[L++] = (uint8_t)(aux2>>0);
  106. buf[L++] = (uint8_t)(aux3>>8);
  107. buf[L++] = (uint8_t)(aux3>>0);
  108. buf[L++] = (uint8_t)(aux4>>8);
  109. buf[L++] = (uint8_t)(aux4>>0);
  110. buf[L++] = (uint8_t)(aux5>>8);
  111. buf[L++] = (uint8_t)(aux5>>0);
  112. buf[L++] = (uint8_t)(aux6>>8);
  113. buf[L++] = (uint8_t)(aux6>>0);
  114. ANO_Send(0x03,buf,L);
  115. }
  116. void ANO_Send_Distance(uint8_t num,int16_t dis1_100,int16_t dis2_100,int16_t dis3_100,int16_t dis4_100,int16_t dis5_100,int16_t dis6_100)
  117. {
  118. uint8_t buf[32];
  119. uint8_t L=0;
  120. buf[L++] = (uint8_t)(num>>0);
  121. buf[L++] = (uint8_t)(dis1_100>>8);
  122. buf[L++] = (uint8_t)(dis1_100>>0);
  123. buf[L++] = (uint8_t)(dis2_100>>8);
  124. buf[L++] = (uint8_t)(dis2_100>>0);
  125. buf[L++] = (uint8_t)(dis3_100>>8);
  126. buf[L++] = (uint8_t)(dis3_100>>0);
  127. buf[L++] = (uint8_t)(dis4_100>>8);
  128. buf[L++] = (uint8_t)(dis4_100>>0);
  129. buf[L++] = (uint8_t)(dis5_100>>8);
  130. buf[L++] = (uint8_t)(dis5_100>>0);
  131. buf[L++] = (uint8_t)(dis6_100>>8);
  132. buf[L++] = (uint8_t)(dis6_100>>0);
  133. ANO_Send(0x31,buf,L);
  134. }
  135. void ANO_Send_Location(uint8_t num,int16_t locX_100,int16_t locY_100,int16_t locZ_100,int16_t locDX_100,int16_t locDY_100,int16_t locDZ_100)
  136. {
  137. uint8_t buf[32];
  138. uint8_t L=0;
  139. buf[L++] = (uint8_t)(num>>0);
  140. buf[L++] = (uint8_t)(locX_100>>8);
  141. buf[L++] = (uint8_t)(locX_100>>0);
  142. buf[L++] = (uint8_t)(locY_100>>8);
  143. buf[L++] = (uint8_t)(locY_100>>0);
  144. buf[L++] = (uint8_t)(locZ_100>>8);
  145. buf[L++] = (uint8_t)(locZ_100>>0);
  146. buf[L++] = (uint8_t)(locDX_100>>8);
  147. buf[L++] = (uint8_t)(locDX_100>>0);
  148. buf[L++] = (uint8_t)(locDY_100>>8);
  149. buf[L++] = (uint8_t)(locDY_100>>0);
  150. buf[L++] = (uint8_t)(locDZ_100>>8);
  151. buf[L++] = (uint8_t)(locDZ_100>>0);
  152. ANO_Send(0x32,buf,L);
  153. }
  154. //============================================================================================
  155. // END OF CODE
  156. //============================================================================================