app_math.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "app_math.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_ser_imu_mode_manage.h"
  5. #include "math.h"
  6. #include "ble_comm.h"
  7. #include "app_flash.h"
  8. #include "detect_zero_vel.h"
  9. #include "tool.h"
  10. #include "app_switchimu.h"
  11. #include "detect_step_by_mag.h"
  12. #include "pdrStatus.h"
  13. #include "detect_step_by_mag.h"
  14. #include "app_client_step.h"
  15. #define IMU_MAX_GROUP_NUM 20
  16. static int16_t acc_front[IMU_MAX_GROUP_NUM][3];
  17. static int16_t gry_front[IMU_MAX_GROUP_NUM][3];
  18. static int16_t mag6310_front[IMU_MAX_GROUP_NUM][3];
  19. static int16_t mag6310_back[3];
  20. static int32_t timestamp_front[IMU_MAX_GROUP_NUM];
  21. static uint8_t rssi;
  22. static int16_t IMU_STATUS; //记录状态用来重新记录时间戳
  23. static int32_t timestamp;
  24. static int32_t last_timestamp;
  25. void process_imu_data_front(int front_index)
  26. {
  27. if(IMU_STATUS != 1)
  28. {
  29. IMU_STATUS = 1;
  30. last_timestamp = timestamp_front[0];
  31. timestamp = 0;
  32. }
  33. if(front_index >IMU_MAX_GROUP_NUM)return;
  34. for(int i = 0; i < front_index; i++)
  35. {
  36. int32_t dt = timestamp_front[i] - last_timestamp;
  37. if(dt > 20000 || dt < 0)
  38. {
  39. dt = 10000;
  40. }
  41. timestamp += dt;
  42. SEGGER_RTT_printf(0,"timestamp_front[i] - last_timestamp : %d; i = %d NRF_RTC0->COUNTER:%d\r\n", timestamp_front[i] - last_timestamp, i,NRF_RTC0->COUNTER);
  43. last_timestamp = timestamp_front[i];
  44. // SEGGER_RTT_printf(0,"timestamp_front[i] : %d; i = %d\r\n", timestamp_front[i], i);
  45. IMU_Process_motion_queue(mFlash.isHost, timestamp, acc_front[i],
  46. gry_front[i],mag6310_front[i], mag6310_back, rssi);
  47. }
  48. }
  49. void app_math_TimerCounter(int front_num, int back_num)
  50. {
  51. ser_imu_data_t data;
  52. int16_t group_num = 0;
  53. //游戏模式
  54. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1){
  55. rssi = 0-host_get_rssi();
  56. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  57. if(group_num >IMU_MAX_GROUP_NUM || front_num == 0)return;
  58. for(int i=0;i<group_num;i++){
  59. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  60. gry_front[i][0] = data.gry[0];gry_front[i][1] = data.gry[1];gry_front[i][2] = data.gry[2];
  61. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  62. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  63. timestamp_front[i] = data.fifo_timestamp;
  64. // JS_RTT_Print_06(acc_front[i][0],acc_front[i][1],acc_front[i][2],gry_front[i][0],gry_front[i][1],gry_front[i][2]);
  65. // JS_RTT_Print_06(mag6310_front[i][0],mag6310_front[i][1],mag6310_front[i][2],timestamp_front[i],0,0);
  66. }
  67. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_BACK) >= 1){
  68. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_BACK, 0, &data);
  69. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  70. }
  71. if(mFlash.isHost){
  72. process_imu_data_front(group_num);
  73. }else if(Slave_Get7_5ms_interval()){
  74. process_imu_data_front(group_num);
  75. }
  76. }else{
  77. //将状态重设为0
  78. IMU_STATUS = 0;
  79. set_pdr_status();
  80. }
  81. //实时计步模式
  82. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != -1){
  83. rssi = 0-host_get_rssi();
  84. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  85. if(group_num >IMU_MAX_GROUP_NUM || front_num == 0)return;
  86. for(int i=0;i<group_num;i++){
  87. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  88. gry_front[i][0] = data.gry[0];gry_front[i][1] = data.gry[1];gry_front[i][2] = data.gry[2];
  89. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  90. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  91. timestamp_front[i] = data.fifo_timestamp;
  92. }
  93. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_BACK) >= 1){
  94. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_BACK, 0, &data);
  95. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  96. }
  97. // SEGGER_RTT_printf(0,"======RealTimeStep=====%d\r\n",TIME_GetTicks());
  98. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  99. mFlash.mStep.stepCur[0]++;
  100. SEGGER_RTT_printf(0,"======RealTimeStep=====step %d\r\n",mFlash.mStep.stepCur[0]);
  101. }
  102. }
  103. }
  104. static void app_math_DailyStep_Process(void)
  105. {
  106. int16_t acc[3];
  107. int16_t mag6310[3];
  108. int16_t group_num = 0;
  109. ser_imu_data_t data;
  110. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL) != -1 && hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) == -1){
  111. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  112. for(int i=0; i < group_num; i++){
  113. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  114. mag6310[0] = data.mag[0];mag6310[1] = data.mag[1];mag6310[2] = data.mag[2];
  115. acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
  116. // SEGGER_RTT_printf(0,"f_mx=%d\r,f_my=%d\r,f_mz=%d\r\n",mag6310[0],mag6310[1],mag6310[2]);
  117. if(1 == detect_step_by_mag(mag6310,acc[2])){
  118. mFlash.mStep.stepCur[0]++;
  119. SEGGER_RTT_printf(0,"DailyStep current step:%d\r\n",mFlash.mStep.stepCur[0]);
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. static uint32_t timeCNT = 0;
  126. static uint8_t FlagFix_process = 0;
  127. //一小时计时
  128. static void app_math_Hour_process(void){
  129. uint32_t sec = 0;
  130. static uint32_t cnt_b=0;
  131. uint32_t cnt = NRF_RTC0->COUNTER;
  132. if(cnt<cnt_b) cnt += 16777216;
  133. timeCNT += cnt - cnt_b;
  134. sec = timeCNT/32768;
  135. if(sec >= 130000 )SEGGER_RTT_printf(0,"!!!!!!!!!!!!!!! sec over,%d,%d,%d\n",cnt,cnt_b,sec);
  136. if(cnt >16777216)cnt_b = cnt - 16777216;
  137. else cnt_b = cnt;
  138. if(sec>=60*60){
  139. app_client_step_SetIsScan();
  140. FlagFix_process =1;
  141. timeCNT =0;
  142. SEGGER_RTT_printf(0,"timeCNT(%d)(%d)\n",timeCNT,sec);
  143. }
  144. }
  145. static void app_gyro_Fix_process(void){//陀螺仪零偏矫正
  146. static uint8_t state =0;
  147. static int16_t sample_count =0;
  148. static uint32_t tim =0;
  149. int16_t f_gry[3];
  150. ser_imu_data_t data;
  151. switch(state){
  152. case 0:
  153. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) && 1 == FlagFix_process){
  154. Process_SetHoldOn(app_gyro_Fix_process,1);
  155. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_CALIBRATION, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  156. state =1;
  157. FlagFix_process =0;
  158. tim = TIME_GetTicks();
  159. // SEGGER_RTT_printf(0,"====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  160. }
  161. break;
  162. case 1:
  163. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_CALIBRATION)){
  164. state =2;
  165. Process_UpdatePeroid(app_gyro_Fix_process,10);
  166. sample_count =0;
  167. tim = TIME_GetTicks();
  168. // SEGGER_RTT_printf(0,"====INTO HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  169. }else{
  170. if(TIME_GetTicks()-tim>=30000){tim = TIME_GetTicks();//进入游戏模式30秒失败
  171. // SEGGER_RTT_printf(0,"====cnt >= 3000\r\n");
  172. state =3;
  173. }
  174. }
  175. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME))state =3;//游戏模式下退出
  176. break;
  177. case 2:
  178. //读取ACC值
  179. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  180. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  181. f_gry[0] = data.gry[0];f_gry[1] = data.gry[1];f_gry[2] = data.gry[2];
  182. estimate_gyr_bias_interface(f_gry,sample_count);
  183. // SEGGER_RTT_printf(0,"====>>>>gry:%d,%d,%d,tim:%d\r\n",f_gry[0],f_gry[1],f_gry[2],sample_count);
  184. sample_count++;
  185. }
  186. if(TIME_GetTicks()-tim>=10000){ //跑完10秒退出
  187. state =3;
  188. // SEGGER_RTT_printf(0,"====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  189. }
  190. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME))state =3;//游戏模式下退出
  191. break;
  192. case 3:
  193. Process_UpdatePeroid(app_gyro_Fix_process,1000);
  194. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_CALIBRATION, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  195. Process_SetHoldOn(app_gyro_Fix_process,0);
  196. state =0;
  197. // SEGGER_RTT_printf(0,"====OUT app_gyro_Fix_process\r\n");
  198. break;
  199. }
  200. }
  201. void app_math_Init(void)
  202. {
  203. Process_Start(100,"app_math_DailyStep_Process",app_math_DailyStep_Process);
  204. // Process_Start(10,"app_math_TimerCounter",app_math_TimerCounter);
  205. hal_ser_imu_data_update_notify(app_math_TimerCounter);
  206. Process_Start(500,"app_gyro_Fix_process",app_gyro_Fix_process);
  207. Process_Start(1000,"app_math_Hour",app_math_Hour_process);
  208. }