app_math.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "app_math.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "math.h"
  5. #include "ble_comm.h"
  6. #include "app_flash.h"
  7. #include "detect_zero_vel.h"
  8. #include "tool.h"
  9. #include "app_switchimu.h"
  10. #include "detect_step_by_mag.h"
  11. #include "pdrStatus.h"
  12. #include "detect_step_by_mag.h"
  13. #include "app_client_step.h"
  14. #include "hal_mt.h"
  15. #include "hal_led.h"
  16. #include "bll_imu.h"
  17. #include "app_game.h"
  18. static uint32_t timeCNT = 0;
  19. static uint8_t FlagFix_process = 0;
  20. static int16_t offset_milisecond =0;
  21. static uint64_t last_newtime_ms =0;
  22. static uint8_t RealMinute =0;
  23. //叫硫珂쇌
  24. void app_math_calit_time(uint8_t appminute){
  25. static uint32_t last_device_time_ms = 0;
  26. uint64_t newtime_ms =0;
  27. uint32_t device_time_ms = 0;
  28. newtime_ms = (
  29. ((uint64_t) mFlash.mStep.startTime[0] << 56) | ((uint64_t)mFlash.mStep.startTime[1] << 48 )
  30. |((uint64_t)mFlash.mStep.startTime[2] << 40) | ((uint64_t)mFlash.mStep.startTime[3] << 32)
  31. |((uint64_t)mFlash.mStep.startTime[4] << 24) | ((uint64_t)mFlash.mStep.startTime[5] << 16)
  32. |((uint64_t)mFlash.mStep.startTime[6] << 8) | (uint64_t)mFlash.mStep.startTime[7]);
  33. device_time_ms = timeCNT;
  34. DEBUG_LOG("app_math_calit_time:realminute:%d,%lu,%lu\n",appminute,(uint32_t)(newtime_ms-last_newtime_ms),(uint32_t)((device_time_ms-last_device_time_ms)/32.768));
  35. // if(0 != last_newtime_ms){
  36. // if( newtime_ms > last_newtime_ms
  37. // && (newtime_ms - last_newtime_ms >= 30000)
  38. // && device_time_ms > last_device_time_ms){
  39. // uint32_t time_interval =0,devicetime_interval =0;
  40. // time_interval = (newtime_ms - last_newtime_ms);
  41. // devicetime_interval = (device_time_ms - last_device_time_ms)/32.768;
  42. // DEBUG_LOG("app_math_calit_time:%u,%u\n",time_interval,devicetime_interval);
  43. // }
  44. // }
  45. uint8_t mymintu =0;
  46. mymintu = (uint8_t)(newtime_ms%(60000));
  47. DEBUG_LOG("apptime : %u:%u,shoes time: %u:%u\n",appminute,mymintu,(uint32_t)((timeCNT)/32768/60),(uint8_t)((timeCNT)/32768%60));
  48. timeCNT = (appminute)*60*32768;
  49. last_newtime_ms = newtime_ms;
  50. last_device_time_ms = device_time_ms;
  51. //
  52. }
  53. //寧鬼珂셕珂
  54. static void app_math_Hour_process(void){
  55. static uint8_t Halfhour_cnt =0;
  56. uint32_t sec = 0;
  57. static uint32_t cnt_b=0;
  58. uint32_t cnt = NRF_RTC0->COUNTER;
  59. if(cnt<cnt_b) cnt += 16777216;
  60. timeCNT += cnt - cnt_b;
  61. sec = (timeCNT+offset_milisecond)/32768;
  62. if(cnt >16777216)cnt_b = cnt - 16777216;
  63. else cnt_b = cnt;
  64. if(sec >=1800){//곕鬼珂
  65. if(1 != Halfhour_cnt){
  66. Halfhour_cnt =1;
  67. FlagFix_process =1;
  68. }
  69. }
  70. if(sec >= 3600){//寧鬼珂
  71. timeCNT =0;
  72. app_client_step_SetIsScan();
  73. FlagFix_process =1;
  74. Halfhour_cnt =0;
  75. DEBUG_LOG("timeCNT(%d)(%d)\n",timeCNT,sec);
  76. }
  77. }
  78. //炬랬叫硫친駕
  79. static const bll_imu_one_way_param_t calibration_front_param={
  80. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //품신 - 속醵똑攣끽친駕
  81. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //품신 - 顧쭁老攣끽친駕
  82. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //품신 - 珂쇌늑25US쑹똑
  83. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //품신 - 珂쇌늑역폘
  84. .acc_fs = FML_IMU_ACC_FS_16G, //품신 - 속醵똑좆넋 - 16G
  85. .gry_fs = FML_IMU_GRY_FS_2000DPS, //품신 - 顧쭁老좆넋 - 2000DPS
  86. .mag_fs = FML_IMU_MAG_FS_30GS, //품신 - 뒈늚셕좆넋 - 30GS
  87. .acc_odr = FML_IMU_ACC_ODR_104HZ, //품신 - 속醵똑꽃湳틉쪽 - 104HZ
  88. .gry_odr = FML_IMU_GRY_ODR_104HZ, //품신 - 顧쭁老꽃湳틉쪽 - 104HZ
  89. .mag_odr = FML_IMU_MAG_ODR_200HZ, //품신 - 뒈늚셕꽃湳틉쪽 - 200HZ
  90. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  91. };
  92. static const bll_imu_one_way_param_t calibration_back_param={
  93. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //빈신 - 속醵똑攣끽친駕
  94. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //빈신 - 顧쭁老攣끽친駕
  95. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //빈신 - 珂쇌늑25US쑹똑
  96. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //빈신 - 珂쇌늑밑균
  97. .acc_fs = FML_IMU_ACC_FS_16G, //빈신 - 속醵똑좆넋 - 16G
  98. .gry_fs = FML_IMU_GRY_FS_2000DPS, //빈신 - 顧쭁老좆넋 - 2000DPS
  99. .mag_fs = FML_IMU_MAG_FS_30GS, //빈신 - 뒈늚셕좆넋 - 30GS
  100. .acc_odr = FML_IMU_ACC_ODR_OFF, //빈신 - 속醵똑꽃湳틉쪽 - 밑균
  101. .gry_odr = FML_IMU_GRY_ODR_OFF, //빈신 - 顧쭁老꽃湳틉쪽 - 밑균
  102. .mag_odr = FML_IMU_MAG_ODR_200HZ, //빈신 - 뒈늚셕꽃湳틉쪽 - 200HZ
  103. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  104. };
  105. static const bll_imu_param_t calibration_bll_imu_param_t={
  106. .config_param[BLL_IMU_DIR_FRONT] = &calibration_front_param,
  107. .config_param[BLL_IMU_DIR_BACK] = &calibration_back_param,
  108. };
  109. static void app_gyro_Fix_process(void){//顧쭁老쥐튤시攣
  110. static uint8_t state =0;
  111. static int16_t sample_count =0;
  112. static uint32_t tim =0;
  113. static uint8_t Setimu_config =0;
  114. bll_imu_data_t data= {0};
  115. uint8_t front_CS =0,back_CS =0;
  116. switch(state){
  117. case 0:
  118. if(!app_game_GetGameMode() && 1 == FlagFix_process){
  119. Process_SetHoldOn(app_gyro_Fix_process,1);
  120. bll_imu_Resume_config_param(&calibration_bll_imu_param_t);
  121. state =1;
  122. FlagFix_process =0;
  123. tim = TIME_GetTicks();
  124. Setimu_config =0;
  125. DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION,tim:%d s\r\n",TIME_GetTicks()/1000);
  126. }
  127. break;
  128. case 1:
  129. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&calibration_bll_imu_param_t);
  130. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&calibration_bll_imu_param_t);
  131. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  132. state =2;
  133. Process_UpdatePeroid(app_gyro_Fix_process,10);
  134. sample_count =0;
  135. tim = TIME_GetTicks();
  136. // MT_Run(500);
  137. // DEBUG_LOG("====INTO HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  138. }
  139. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  140. bll_imu_Resume_config_param(&calibration_bll_imu_param_t);
  141. if(++Setimu_config >= 20){Setimu_config =0;
  142. bll_imu_Resume_unregister_config_param(&calibration_bll_imu_param_t);
  143. state =3;
  144. Except_TxError(EXCEPT_GAME,"shoes into CALIBRATION mode fail");
  145. }
  146. }
  147. if(app_game_GetGameMode())state =3;//踏狗친駕苟藁놔
  148. break;
  149. case 2:
  150. //뗍혤ACC令6
  151. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 1){
  152. bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &data);
  153. estimate_gyr_bias_interface(data.gry,sample_count);
  154. // DEBUG_LOG("====>>>>gry:%d,%d,%d,tim:%d\r\n",data.gry[0],data.gry[1],data.gry[2],sample_count);
  155. sample_count++;
  156. }
  157. if(TIME_GetTicks()-tim>=10000){ //텝供10취藁놔
  158. state =3;
  159. // DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  160. }
  161. if(app_game_GetGameMode())state =3;//踏狗친駕苟藁놔
  162. break;
  163. case 3:
  164. Process_UpdatePeroid(app_gyro_Fix_process,1000);
  165. bll_imu_Resume_unregister_config_param(&calibration_bll_imu_param_t);
  166. Process_SetHoldOn(app_gyro_Fix_process,0);
  167. state =0;
  168. Setimu_config =0;
  169. // DEBUG_LOG("====OUT app_gyro_Fix_process\r\n");
  170. break;
  171. default:state =0;Setimu_config =0;break;
  172. }
  173. }
  174. void app_math_Init(void)
  175. {
  176. Process_Start(500,"app_gyro_Fix_process",app_gyro_Fix_process);
  177. Process_Start(1000,"app_math_Hour",app_math_Hour_process);
  178. }