app_math.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. char buf[30]={0};
  49. memset(buf,0,sizeof(buf));
  50. sprintf(buf,"appminute:%d,mcu miute:%d",appminute,timeCNT/32768/60);
  51. Except_TxError(EXCEPT_DATEStep,(const char *)buf);
  52. timeCNT = (appminute)*60*32768;
  53. last_newtime_ms = newtime_ms;
  54. last_device_time_ms = device_time_ms;
  55. //
  56. }
  57. //寧鬼珂셕珂
  58. static void app_math_Hour_process(void){
  59. static uint8_t Halfhour_cnt =0;
  60. uint32_t sec = 0;
  61. static uint32_t cnt_b=0;
  62. uint32_t cnt = NRF_RTC0->COUNTER;
  63. if(cnt<cnt_b) cnt += 16777216;
  64. timeCNT += cnt - cnt_b;
  65. sec = (timeCNT+offset_milisecond)/32768;
  66. if(cnt >16777216)cnt_b = cnt - 16777216;
  67. else cnt_b = cnt;
  68. if(sec >=1800){//곕鬼珂
  69. if(1 != Halfhour_cnt){
  70. Halfhour_cnt =1;
  71. FlagFix_process =1;
  72. }
  73. }
  74. if(sec >= 3600){//寧鬼珂
  75. timeCNT =0;
  76. app_client_step_SetIsScan();
  77. FlagFix_process =1;
  78. Halfhour_cnt =0;
  79. DEBUG_LOG("timeCNT(%d)(%d)\n",timeCNT,sec);
  80. }
  81. }
  82. //炬랬叫硫친駕
  83. static const bll_imu_one_way_param_t calibration_front_param={
  84. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //품신 - 속醵똑攣끽친駕
  85. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //품신 - 顧쭁老攣끽친駕
  86. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //품신 - 珂쇌늑25US쑹똑
  87. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //품신 - 珂쇌늑역폘
  88. .acc_fs = FML_IMU_ACC_FS_16G, //품신 - 속醵똑좆넋 - 16G
  89. .gry_fs = FML_IMU_GRY_FS_2000DPS, //품신 - 顧쭁老좆넋 - 2000DPS
  90. .mag_fs = FML_IMU_MAG_FS_30GS, //품신 - 뒈늚셕좆넋 - 30GS
  91. .acc_odr = FML_IMU_ACC_ODR_104HZ, //품신 - 속醵똑꽃湳틉쪽 - 104HZ
  92. .gry_odr = FML_IMU_GRY_ODR_104HZ, //품신 - 顧쭁老꽃湳틉쪽 - 104HZ
  93. .mag_odr = FML_IMU_MAG_ODR_200HZ, //품신 - 뒈늚셕꽃湳틉쪽 - 200HZ
  94. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  95. };
  96. static const bll_imu_one_way_param_t calibration_back_param={
  97. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //빈신 - 속醵똑攣끽친駕
  98. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //빈신 - 顧쭁老攣끽친駕
  99. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //빈신 - 珂쇌늑25US쑹똑
  100. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //빈신 - 珂쇌늑밑균
  101. .acc_fs = FML_IMU_ACC_FS_16G, //빈신 - 속醵똑좆넋 - 16G
  102. .gry_fs = FML_IMU_GRY_FS_2000DPS, //빈신 - 顧쭁老좆넋 - 2000DPS
  103. .mag_fs = FML_IMU_MAG_FS_30GS, //빈신 - 뒈늚셕좆넋 - 30GS
  104. .acc_odr = FML_IMU_ACC_ODR_OFF, //빈신 - 속醵똑꽃湳틉쪽 - 밑균
  105. .gry_odr = FML_IMU_GRY_ODR_OFF, //빈신 - 顧쭁老꽃湳틉쪽 - 밑균
  106. .mag_odr = FML_IMU_MAG_ODR_200HZ, //빈신 - 뒈늚셕꽃湳틉쪽 - 200HZ
  107. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  108. };
  109. static const bll_imu_param_t calibration_bll_imu_param_t={
  110. .config_param[BLL_IMU_DIR_FRONT] = &calibration_front_param,
  111. .config_param[BLL_IMU_DIR_BACK] = &calibration_back_param,
  112. };
  113. static void app_gyro_Fix_process(void){//顧쭁老쥐튤시攣
  114. static uint8_t state =0;
  115. static int16_t sample_count =0;
  116. static uint32_t tim =0;
  117. static uint8_t Setimu_config =0;
  118. bll_imu_data_t data= {0};
  119. uint8_t front_CS =0,back_CS =0;
  120. switch(state){
  121. case 0:
  122. if(!app_game_GetGameMode() && 1 == FlagFix_process){
  123. Process_SetHoldOn(app_gyro_Fix_process,1);
  124. bll_imu_Resume_config_param(&calibration_bll_imu_param_t);
  125. state =1;
  126. FlagFix_process =0;
  127. tim = TIME_GetTicks();
  128. Setimu_config =0;
  129. DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION,tim:%d s\r\n",TIME_GetTicks()/1000);
  130. }
  131. break;
  132. case 1:
  133. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&calibration_bll_imu_param_t);
  134. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&calibration_bll_imu_param_t);
  135. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  136. state =2;
  137. Process_UpdatePeroid(app_gyro_Fix_process,10);
  138. sample_count =0;
  139. tim = TIME_GetTicks();
  140. // MT_Run(500);
  141. // DEBUG_LOG("====INTO HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  142. }
  143. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  144. bll_imu_Resume_config_param(&calibration_bll_imu_param_t);
  145. if(++Setimu_config >= 20){Setimu_config =0;
  146. bll_imu_Resume_unregister_config_param(&calibration_bll_imu_param_t);
  147. state =3;
  148. Except_TxError(EXCEPT_GAME,"shoes into CALIBRATION mode fail");
  149. }
  150. }
  151. if(app_game_GetGameMode())state =3;//踏狗친駕苟藁놔
  152. break;
  153. case 2:
  154. //뗍혤ACC令6
  155. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 1){
  156. bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &data);
  157. estimate_gyr_bias_interface(data.gry,sample_count);
  158. // DEBUG_LOG("====>>>>gry:%d,%d,%d,tim:%d\r\n",data.gry[0],data.gry[1],data.gry[2],sample_count);
  159. sample_count++;
  160. }
  161. if(TIME_GetTicks()-tim>=10000){ //텝供10취藁놔
  162. state =3;
  163. // DEBUG_LOG("====>>>>HAL_SER_IMU_MODE_MANAGE_CALIBRATION\r\n");
  164. }
  165. if(app_game_GetGameMode())state =3;//踏狗친駕苟藁놔
  166. break;
  167. case 3:
  168. Process_UpdatePeroid(app_gyro_Fix_process,1000);
  169. bll_imu_Resume_unregister_config_param(&calibration_bll_imu_param_t);
  170. Process_SetHoldOn(app_gyro_Fix_process,0);
  171. state =0;
  172. Setimu_config =0;
  173. // DEBUG_LOG("====OUT app_gyro_Fix_process\r\n");
  174. break;
  175. default:state =0;Setimu_config =0;break;
  176. }
  177. }
  178. void app_math_Init(void)
  179. {
  180. Process_Start(500,"app_gyro_Fix_process",app_gyro_Fix_process);
  181. Process_Start(1000,"app_math_Hour",app_math_Hour_process);
  182. }