hal_imu.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "hal_imu.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "bsp_spi.h"
  5. #include "vl53l1.h"
  6. #include "drv_qmc5883L_f.h"
  7. #include "nrf_drv_timer.h"
  8. #include "nrf_delay.h"
  9. #include "hal_mt.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "main.h"
  13. #include "app_game.h"
  14. #include "ble_comm.h"
  15. #include "hal_flash.h"
  16. #include "app_err.h"
  17. #include "hal_led.h"
  18. #include "hal_mempress.h"
  19. #include "motion_interface.h"
  20. #include "app_connect_manage.h"
  21. #include "IOI2C.h"
  22. #include "detect_step_by_acc.h"
  23. #include "arm_math.h"
  24. #include "tool.h"
  25. #include "hal_step.h"
  26. #include "hal_mag.h"
  27. #include "hal_attitude.h"
  28. #include "app_ImuCalibration.h"
  29. #include "drv_qmc6310.h"
  30. #include "hal_Realstep.h"
  31. #include "detect_zero_vel.h"
  32. #include "drv_mpu9250.h"
  33. #define ACC_FIFO_SIZE 10
  34. /********************** 变量区 *************************/
  35. static volatile uint8_t rssi = 0;
  36. static volatile int32_t distand = 0;
  37. static uint8_t isGameMode = 0;
  38. static uint8_t sensorEnable = 0;
  39. /********************** 外部接口声明区 *************************/
  40. void send_protocol(uint8_t index,uint8_t cmd,uint8_t* p,uint8_t datLen)
  41. {
  42. BLE_Client_Tx_Send(0,cmd,p,datLen);
  43. }
  44. void IMU_SetGameMode(uint8_t isgame)
  45. {
  46. isGameMode = isgame;
  47. }
  48. uint8_t IMU_GetGameMode(void)
  49. {
  50. return sensorEnable;
  51. }
  52. uint8_t IMU_GetHostRssi(void)
  53. {
  54. return rssi;
  55. }
  56. __weak void process_motion(int16_t* _acc, int16_t* _gry, int16_t* front_mag, int16_t* back_mag, uint8_t _rssi)
  57. {
  58. static uint32_t tim=0;
  59. if(TIME_GetTicks()-tim>=1000){ tim = TIME_GetTicks();
  60. SEGGER_RTT_printf(0,"__weak void process_motion\n");
  61. }
  62. }
  63. uint8_t left_buff[64];
  64. uint8_t left_len;
  65. uint8_t right_buff[64];
  66. uint8_t right_len;
  67. uint8_t inter_buff[128];
  68. uint8_t inter_buff_len;
  69. void IMU_SetSlaveData(uint8_t* pdat,uint8_t len)
  70. {
  71. if(pdat[3]==BLE_Host_R_TRACK){
  72. IMU_Rec_data(pdat,len);
  73. }
  74. app_game_SetClientGameMode();
  75. }
  76. void IMU_GetSensorData(void)
  77. {
  78. hal_att_read_Acc_Gry_temp();
  79. hal_Mag_ReadXYZ();
  80. #if DEBUG_IMU
  81. int16_t *acc = hal_att_GetAcc();
  82. int16_t *gry = hal_att_GetGry();
  83. SEGGER_RTT_printf(0,"h_ax=%d\r,h_ay=%d\r,h_az=%d\r\n",acc[0],acc[1],acc[2]);
  84. SEGGER_RTT_printf(0,"h_gx=%d\r,h_gy=%d\r,h_gz=%d\r\n",gry[0],gry[1],gry[2]);
  85. float *p1 =NULL,*p2=NULL;
  86. p1 =hal_Mag_GetXYZ(Mag_Back);
  87. p2 =hal_Mag_GetXYZ(Mag_Front);
  88. static char string[200];
  89. sprintf(string,"Mag_Front=%f,%f,%f,Mag_Back=%f,%f,%f\n",p2[0],p2[1],p2[2],p1[0],p1[1],p1[2]);
  90. SEGGER_RTT_printf(0,"%s",string);
  91. SEGGER_RTT_printf(0,"h_rssi=%d\n",rssi);
  92. #endif
  93. }
  94. void IMU_GetSensorDataLowPower(void)
  95. {
  96. static uint32_t tim=0;
  97. if(TIME_GetTicks()-tim>=50){ tim = TIME_GetTicks();
  98. hal_att_read_acc();
  99. hal_Mag_ReadXYZ();
  100. #if DEBUG_IMU
  101. int16_t *acc = hal_att_GetAcc();
  102. SEGGER_RTT_printf(0,"MPU9250 h_ax[1]=%d\r,h_ay[1]=%d\r,h_az[1]=%d\n",acc[0],acc[1],acc[2]);
  103. float *p1 =NULL,*p2=NULL;
  104. p1 =hal_Mag_GetXYZ(Mag_Back);
  105. p2 =hal_Mag_GetXYZ(Mag_Front);
  106. static char string[200];
  107. sprintf(string,"Mag_Front=%f,%f,%f,Mag_Back=%f,%f,%f\n",p2[0],p2[1],p2[2],p1[0],p1[1],p1[2]);
  108. SEGGER_RTT_printf(0,"%s",string);
  109. #endif
  110. }
  111. }
  112. /********************** 函数声明区 *************************/
  113. void IMU_TimerCounter(void* t)
  114. {
  115. static int16_t front_mag_int[3];
  116. static int16_t back_mag_int[3];
  117. float *p1 =NULL,*p2=NULL;
  118. int16_t *acc =NULL,*gry=NULL;
  119. if(1 == time_10ms_flag){
  120. #if CALIBRATION_ENANBLE
  121. if(app_imucal_getstate())IMU_GetSensorData();
  122. #else
  123. if(sensorEnable>0 || hal_Realstep_GetState()){
  124. IMU_GetSensorData();
  125. p1= hal_Mag_GetXYZ(Mag_Front);
  126. p2= hal_Mag_GetXYZ(Mag_Back);
  127. acc = hal_att_GetAcc();
  128. gry = hal_att_GetGry();
  129. for(uint8_t i = 0; i < 3; i ++)
  130. {
  131. front_mag_int[i] = (int16_t) (p1[i] *10);
  132. back_mag_int[i] = (int16_t) (p2[i] *10);
  133. }
  134. if(sensorEnable>0 ){
  135. if(mFlash.isHost){
  136. IMU_Process_motion_queue(mFlash.isHost,(int16_t*)acc,(int16_t*)gry,(int16_t*)front_mag_int,(int16_t*)back_mag_int,rssi);
  137. }else if(Slave_Get7_5ms_interval()){
  138. IMU_Process_motion_queue(mFlash.isHost,(int16_t*)acc,(int16_t*)gry,(int16_t*)front_mag_int,(int16_t*)back_mag_int,rssi);
  139. }
  140. }
  141. else start_cal_step((int16_t*)front_mag_int, (int16_t*)back_mag_int, (int16_t*)acc);
  142. }
  143. #endif
  144. }
  145. }
  146. static void hal_imu_readrssi_process(void){
  147. if(sensorEnable>0 || hal_Realstep_GetState()){
  148. rssi = 0-host_get_rssi();
  149. }
  150. }
  151. static void IMU_process(void)
  152. {
  153. enum {
  154. STATE_lOWPOWERMODE = 0,
  155. STATE_ENTER_GAMEMODE,
  156. STATE_GAMEMODE,
  157. STATE_EXIT_PRE,
  158. STATE_EXIT_GAMEMODE,
  159. };
  160. static uint8_t state = 0;
  161. static uint32_t tim =0;
  162. static uint8_t n = 0;
  163. switch(state){
  164. case STATE_lOWPOWERMODE:{
  165. if(isGameMode >0 ){ Process_SetHoldOn(IMU_process,1);
  166. state = STATE_ENTER_GAMEMODE;
  167. hal_attitude_setMode(Att_GameMode);
  168. hal_mag_SetState(Mag_All_l,Mag_Mode_100Hz);
  169. Process_UpdatePeroid(IMU_process,200);
  170. break;
  171. }
  172. #if CALIBRATION_ENANBLE
  173. if(!app_imucal_getstate())IMU_GetSensorDataLowPower();
  174. #else
  175. if(!hal_Realstep_GetState())IMU_GetSensorDataLowPower();
  176. #endif
  177. break;}
  178. case STATE_ENTER_GAMEMODE:{
  179. if(1 == hal_attitude_GetMode(Att_GameMode)){
  180. if(Mag_Mode_100Hz == hal_mag_GetState(Mag_Front) && Mag_Mode_100Hz == hal_mag_GetState(Mag_Back)){
  181. sensorEnable = 1;
  182. n = 0;
  183. Process_SetHoldOn(IMU_process,1);
  184. state = STATE_GAMEMODE;
  185. Process_UpdatePeroid(IMU_process,0);
  186. #if LOSSPACK_ENANBLE
  187. if(mFlash.isHost)Process_Start(10,"LossPack_process",LossPack_process);
  188. #endif
  189. Process_Start(10,"ReadRSSI",hal_imu_readrssi_process);
  190. SEGGER_RTT_printf(0,"into game mode\n");
  191. }
  192. else {
  193. if(n++ >= 20){
  194. SEGGER_RTT_printf(0,"ERR_NUM_IMU_QMC6310\n");
  195. isGameMode =0;
  196. app_err_Set(ERR_NUM_IMU_QMC6310,1);
  197. Process_SetHoldOn(IMU_process,0);
  198. state = STATE_lOWPOWERMODE;
  199. Process_UpdatePeroid(IMU_process,0);
  200. }
  201. }
  202. }
  203. else {
  204. // SEGGER_RTT_printf(0,"ERR_NUM_IMU_BMI160\n");
  205. if(n++ >= 20){
  206. app_err_Set(ERR_NUM_IMU_BMI160,1);
  207. Process_SetHoldOn(IMU_process,0);
  208. state = STATE_lOWPOWERMODE;
  209. Process_UpdatePeroid(IMU_process,0);
  210. }
  211. }
  212. break;}
  213. case STATE_GAMEMODE:{
  214. #if !GAME_ENANBLE
  215. if(isGameMode ==0 ){
  216. sensorEnable = 0;
  217. n =0;
  218. state = STATE_EXIT_PRE;
  219. hal_attitude_setMode(Att_Lowpower);
  220. hal_mag_SetState(Mag_Front,Mag_Mode_10Hz);
  221. hal_mag_SetState(Mag_Back,Mag_Mode_Suspend);
  222. Process_UpdatePeroid(IMU_process,500);
  223. #if LOSSPACK_ENANBLE
  224. if(mFlash.isHost)Process_Stop(LossPack_process);
  225. #endif
  226. #if LASER_ENABLE
  227. hal_laser_Enable(0);
  228. #endif
  229. break;
  230. }
  231. #endif
  232. if(TIME_GetTicks()-tim>=500){tim = TIME_GetTicks();
  233. if( 0 == slave_isconnect())advertising_start();
  234. if(mFlash.isHost && 0 == host_isconnect())scan_start();
  235. }
  236. IMU_Dtalige();
  237. break;}
  238. case STATE_EXIT_PRE:{
  239. if(hal_attitude_GetMode(Att_Lowpower)){
  240. state = STATE_EXIT_GAMEMODE;
  241. Process_UpdatePeroid(IMU_process,500);
  242. n = 0;
  243. }
  244. else if(++n >10){ n = 0;
  245. Process_SetHoldOn(IMU_process,0);
  246. app_err_Set(ERR_NUM_IMU_BMI160,1);
  247. state = STATE_EXIT_GAMEMODE;
  248. Process_UpdatePeroid(IMU_process,500);
  249. }
  250. break;}
  251. case STATE_EXIT_GAMEMODE:{
  252. if(Mag_Mode_Suspend == hal_mag_GetState(Mag_Back) && Mag_Mode_10Hz == hal_mag_GetState(Mag_Front)){
  253. n = 0;
  254. Process_SetHoldOn(IMU_process,0);
  255. Process_UpdatePeroid(IMU_process,0);
  256. state = STATE_lOWPOWERMODE;
  257. Process_Stop(hal_imu_readrssi_process);
  258. SEGGER_RTT_printf(0,"out game mode\n");
  259. }else if(++n>10){ n=0;
  260. app_err_Set(ERR_NUM_IMU_QMC5883L,1);
  261. state = STATE_lOWPOWERMODE;
  262. Process_UpdatePeroid(IMU_process,0);
  263. Process_SetHoldOn(IMU_process,0);
  264. Process_Stop(hal_imu_readrssi_process);
  265. }
  266. break;}
  267. default:state = 0;break;
  268. }
  269. }
  270. void IMU_CancelTimerCounter(void)
  271. {
  272. TIME_UnRegist(IMU_TimerCounter);
  273. }
  274. void IMU_Initialize(void)
  275. {
  276. hal_step_init();
  277. hal_Realstep_Init();
  278. hal_attitude_init();
  279. hal_mag_init();
  280. #if LASER_ENABLE
  281. hal_laser_init();
  282. #endif
  283. Process_Start(10,"IMU_process",IMU_process);
  284. TIME_Regist(IMU_TimerCounter);
  285. }