app_math.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "app_math.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_imu.h"
  5. #include "hal_mode_manage.h"
  6. #include "math.h"
  7. #include "ble_comm.h"
  8. #include "app_flash.h"
  9. #include "detect_zero_vel.h"
  10. #include "tool.h"
  11. #include "app_switchimu.h"
  12. #include "detect_step_by_mag.h"
  13. #include "pdrStatus.h"
  14. #include "detect_step_by_mag.h"
  15. static int16_t acc_front[IMU_BUFF_SIZE][3];
  16. static int16_t gry_front[IMU_BUFF_SIZE][3];
  17. static int16_t mag6310_front[IMU_BUFF_SIZE][3];
  18. static int16_t mag6310_back[3];
  19. static int32_t timestamp_front[IMU_BUFF_SIZE];
  20. static uint8_t rssi;
  21. static int16_t IMU_STATUS; //记录状态用来重新记录时间戳
  22. static int32_t timestamp;
  23. static int32_t last_timestamp;
  24. void process_imu_data_front(int front_index)
  25. {
  26. if(IMU_STATUS != 1)
  27. {
  28. IMU_STATUS = 1;
  29. last_timestamp = timestamp_front[0];
  30. timestamp = 0;
  31. }
  32. for(int i = 0; i < front_index; i++)
  33. {
  34. int32_t dt = timestamp_front[i] - last_timestamp;
  35. if(dt > 20000 || dt < 0)
  36. {
  37. dt = 10000;
  38. }
  39. timestamp += dt;
  40. last_timestamp = timestamp_front[i];
  41. // SEGGER_RTT_printf(0,"timestamp_front[i] : %d; i = %d\r\n", timestamp_front[i], i);
  42. IMU_Process_motion_queue(mFlash.isHost, timestamp, acc_front[i],
  43. gry_front[i],mag6310_front[i], mag6310_back, rssi);
  44. }
  45. }
  46. void app_math_TimerCounter(void)
  47. {
  48. int16_t group_num = 0;
  49. //游戏模式
  50. if(hal_mode_get() == HAL_MODE_GAME){
  51. // CHECK_TIMECONSUMING_START;
  52. rssi = 0-host_get_rssi();
  53. group_num = IMU_Get_Front_Data_Num();
  54. for(int i=0;i<group_num;i++){
  55. IMU_Get_Front_Data(i, gry_front[i], acc_front[i], mag6310_front[i], &timestamp_front[i]);
  56. // 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]);
  57. // JS_RTT_Print_06(mag6310_front[i][0],mag6310_front[i][1],mag6310_front[i][2],timestamp_front[i],0,0);
  58. }
  59. IMU_Get_Back_Data(mag6310_back);
  60. if(mFlash.isHost){
  61. process_imu_data_front(group_num);
  62. }else if(Slave_Get7_5ms_interval()){
  63. process_imu_data_front(group_num);
  64. }
  65. // CHECK_TIMECONSUMING_END;
  66. }else{
  67. //将状态重设为0
  68. IMU_STATUS = 0;
  69. set_pdr_status();
  70. }
  71. //实时计步模式
  72. if(hal_mode_get() == HAL_MODE_REALSTEP){
  73. rssi = 0-host_get_rssi();
  74. group_num = IMU_Get_Front_Data_Num();
  75. for(int i=0;i<group_num;i++)IMU_Get_Front_Data(i, gry_front[i], acc_front[i], mag6310_front[i], &timestamp_front[i]);
  76. IMU_Get_Back_Data(mag6310_back);
  77. if(app_switchimu_GetGameModeLsm() == USED_FRONT_LSM){
  78. // SEGGER_RTT_printf(0,"app_switchimu_GetGameModeLsm() == USED_FRONT_LSM\r\n");
  79. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  80. mFlash.mStep.stepCur[0]++;
  81. }
  82. }else if(app_switchimu_GetGameModeLsm() == USED_CENTER_LSM){
  83. // SEGGER_RTT_printf(0,"app_switchimu_GetGameModeLsm() == USED_CENTER_LSM\r\n");
  84. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  85. mFlash.mStep.stepCur[0]++;
  86. }
  87. }
  88. }
  89. }
  90. static void app_math_DailyStep_Process(void)
  91. {
  92. int16_t acc[3];
  93. int16_t mag6310[3];
  94. if(hal_mode_get() == HAL_MODE_NORMAL){
  95. for(int i=0; i < IMU_Get_Front_Data_Num(); i++)
  96. {
  97. IMU_Get_Front_Data(i, NULL, acc, mag6310, NULL);
  98. //SEGGER_RTT_printf(0,"f_mx=%d\r,f_my=%d\r,f_mz=%d\r\n",mag6310[0],mag6310[1],mag6310[2]);
  99. if(1 == detect_step_by_mag(mag6310,acc[2])){
  100. mFlash.mStep.stepCur[0]++;
  101. SEGGER_RTT_printf(0,"curren step:%d\r\n",mFlash.mStep.stepCur[0]);
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. //static void hal_stepSaveTest_process(void){
  108. // mFlash.mStep.stepCur[0]++;
  109. //// Flash_SaveStep();
  110. // SEGGER_RTT_printf(0,"hal step Save one minute:step %d\r\n",mFlash.mStep.stepCur[0]);
  111. //}
  112. void app_math_Init(void)
  113. {
  114. Process_Start(100,"app_math_DailyStep_Process",app_math_DailyStep_Process);
  115. Process_Start(10,"app_math_TimerCounter",app_math_TimerCounter);
  116. // if(mFlash.isHost)Process_Start(10000,"hal_stepSaveTest",hal_stepSaveTest_process);
  117. }