app_math.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include "hal_mt.h"
  16. #include "hal_led.h"
  17. #define IMU_MAX_GROUP_NUM 20
  18. static int16_t acc_front[IMU_MAX_GROUP_NUM][3];
  19. static int16_t gry_front[IMU_MAX_GROUP_NUM][3];
  20. static int16_t mag6310_front[IMU_MAX_GROUP_NUM][3];
  21. static int16_t mag6310_back[3];
  22. static int32_t timestamp_front[IMU_MAX_GROUP_NUM];
  23. static uint8_t rssi;
  24. static int16_t IMU_STATUS; //记录状态用来重新记录时间戳
  25. static int32_t timestamp;
  26. static int32_t last_timestamp;
  27. void process_imu_data_front(int front_index)
  28. {
  29. if(IMU_STATUS != 1)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. // DEBUG_LOG("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. // DEBUG_LOG("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. //测试使用,游戏模式下 1S亮绿灯20ms.
  50. static void app_game_led(uint8_t gamemode){
  51. static uint8_t led_state =0;
  52. static uint8_t temp =0;
  53. if(gamemode){
  54. temp++;
  55. if(temp == 100){
  56. if(led_state){led_state =0;
  57. LED_Start(LED_RUN,COLOR_GREEN);
  58. }
  59. else {led_state =1;
  60. LED_Start(LED_RUN,COLOR_BLACK);
  61. }
  62. }
  63. if(temp > 102){temp =0;
  64. LED_Stop(LED_RUN);
  65. }
  66. }else{
  67. if(2 == led_state){
  68. LED_Stop(LED_RUN);
  69. }
  70. }
  71. }
  72. void app_math_TimerCounter(int front_num, int back_num, int _rssi)
  73. {
  74. ser_imu_data_t data;
  75. int16_t group_num = 0;
  76. //游戏模式
  77. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1){
  78. rssi = _rssi;
  79. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  80. if(group_num >IMU_MAX_GROUP_NUM || front_num == 0)return;
  81. for(int i=0;i<group_num;i++){
  82. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  83. gry_front[i][0] = data.gry[0];gry_front[i][1] = data.gry[1];gry_front[i][2] = data.gry[2];
  84. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  85. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  86. timestamp_front[i] = data.fifo_timestamp;
  87. // 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]);
  88. // JS_RTT_Print_06(mag6310_front[i][0],mag6310_front[i][1],mag6310_front[i][2],timestamp_front[i],0,0);
  89. }
  90. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_BACK) >= 1){
  91. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_BACK, 0, &data);
  92. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  93. }
  94. if(mFlash.isHost){
  95. process_imu_data_front(group_num);
  96. }else if(Slave_Get7_5ms_interval()){
  97. process_imu_data_front(group_num);
  98. }
  99. app_game_led(1);
  100. }else{
  101. //将状态重设为0
  102. IMU_STATUS = 0;
  103. set_pdr_status();
  104. app_game_led(0);
  105. }
  106. //实时计步模式
  107. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != -1){
  108. rssi = _rssi;
  109. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  110. if(group_num >IMU_MAX_GROUP_NUM || front_num == 0)return;
  111. for(int i=0;i<group_num;i++){
  112. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  113. gry_front[i][0] = data.gry[0];gry_front[i][1] = data.gry[1];gry_front[i][2] = data.gry[2];
  114. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  115. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  116. timestamp_front[i] = data.fifo_timestamp;
  117. }
  118. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_BACK) >= 1){
  119. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_BACK, 0, &data);
  120. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  121. }
  122. // DEBUG_LOG("======RealTimeStep=====%d\r\n",TIME_GetTicks());
  123. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  124. mFlash.mStep.stepCur[0]++;
  125. DEBUG_LOG("======RealTimeStep=====step %d\r\n",mFlash.mStep.stepCur[0]);
  126. }
  127. }
  128. }
  129. static void app_math_DailyStep_Process(void)
  130. {
  131. int16_t acc[3];
  132. int16_t mag6310[3];
  133. int16_t group_num = 0;
  134. ser_imu_data_t data;
  135. uint8_t displaybuf[30]={0};
  136. 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){
  137. if(mFlash.isHost)sprintf((char *)displaybuf,"11111 Left DailyStep current step\r\n");
  138. group_num = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  139. for(int i=0; i < group_num; i++){
  140. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, i, &data);
  141. mag6310[0] = data.mag[0];mag6310[1] = data.mag[1];mag6310[2] = data.mag[2];
  142. acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
  143. // DEBUG_LOG("f_mx=%d\r,f_my=%d\r,f_mz=%d\r\n",mag6310[0],mag6310[1],mag6310[2]);
  144. if(1 == detect_step_by_mag(mag6310,acc[2])){
  145. mFlash.mStep.stepCur[0]++;
  146. DEBUG_LOG("DailyStep current step:%d\r\n",mFlash.mStep.stepCur[0]);
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. static uint32_t timeCNT = 0;
  153. static uint8_t FlagFix_process = 0;
  154. //一小时计时
  155. static void app_math_Hour_process(void){
  156. static uint8_t Halfhour_cnt =0;
  157. uint32_t sec = 0;
  158. static uint32_t cnt_b=0;
  159. uint32_t cnt = NRF_RTC0->COUNTER;
  160. if(cnt<cnt_b) cnt += 16777216;
  161. timeCNT += cnt - cnt_b;
  162. sec = timeCNT/32768;
  163. if(sec >= 130000 )DEBUG_LOG("!!!!!!!!!!!!!!! sec over,%d,%d,%d\n",cnt,cnt_b,sec);
  164. if(cnt >16777216)cnt_b = cnt - 16777216;
  165. else cnt_b = cnt;
  166. if(sec >= 1800){//1800半小时
  167. Halfhour_cnt++;
  168. FlagFix_process =1;
  169. timeCNT =0;
  170. DEBUG_LOG("timeCNT(%d)(%d)\n",timeCNT,sec);
  171. }
  172. if(Halfhour_cnt >=2){Halfhour_cnt =0;
  173. app_client_step_SetIsScan();
  174. }
  175. }
  176. static void app_gyro_Fix_process(void){//陀螺仪零偏矫正
  177. static uint8_t state =0;
  178. static int16_t sample_count =0;
  179. static uint32_t tim =0;
  180. ser_imu_data_t data;
  181. switch(state){
  182. case 0:
  183. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) && 1 == FlagFix_process){
  184. Process_SetHoldOn(app_gyro_Fix_process,1);
  185. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_CALIBRATION, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  186. state =1;
  187. FlagFix_process =0;
  188. tim = TIME_GetTicks();
  189. DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION,tim:%d s\r\n",TIME_GetTicks()/1000);
  190. }
  191. break;
  192. case 1:
  193. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_CALIBRATION)){
  194. state =2;
  195. Process_UpdatePeroid(app_gyro_Fix_process,10);
  196. sample_count =0;
  197. tim = TIME_GetTicks();
  198. // MT_Run(500);
  199. // DEBUG_LOG("====INTO HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  200. }else{
  201. if(TIME_GetTicks()-tim>=10000){tim = TIME_GetTicks();//进入游戏模式10秒失败
  202. // DEBUG_LOG("====cnt >= 3000\r\n");
  203. state =3;
  204. }
  205. }
  206. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME))state =3;//游戏模式下退出
  207. break;
  208. case 2:
  209. //读取ACC值6
  210. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  211. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  212. estimate_gyr_bias_interface(data.gry,sample_count);
  213. // DEBUG_LOG("====>>>>gry:%d,%d,%d,tim:%d\r\n",data.gry[0],data.gry[1],data.gry[2],sample_count);
  214. sample_count++;
  215. }
  216. if(TIME_GetTicks()-tim>=10000){ //跑完10秒退出
  217. state =3;
  218. // DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  219. }
  220. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME))state =3;//游戏模式下退出
  221. break;
  222. case 3:
  223. Process_UpdatePeroid(app_gyro_Fix_process,1000);
  224. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_CALIBRATION, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  225. Process_SetHoldOn(app_gyro_Fix_process,0);
  226. state =0;
  227. // DEBUG_LOG("====OUT app_gyro_Fix_process\r\n");
  228. break;
  229. default:state =0;break;
  230. }
  231. }
  232. void app_math_Init(void)
  233. {
  234. Process_Start(0,"app_math_DailyStep_Process",app_math_DailyStep_Process);
  235. // Process_Start(10,"app_math_TimerCounter",app_math_TimerCounter);
  236. hal_ser_imu_data_update_notify(app_math_TimerCounter);
  237. Process_Start(500,"app_gyro_Fix_process",app_gyro_Fix_process);
  238. Process_Start(1000,"app_math_Hour",app_math_Hour_process);
  239. }