hal_wearshoes.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "hal_wearshoes.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_mode_manage.h"
  5. #include "hal_imu.h"
  6. #include "tool.h"
  7. #define HAL_WEARSHOES_PROCESS_CYCLE StandByPower_Interval //线程周期,单位ms
  8. #define HAL_WEARSHOES_TIMEOUT (60000/StandByPower_Interval) //检测周期,当前10S检测一次。
  9. static uint8_t isWearShoes = WEARSHOES_YES;
  10. /**
  11. @brief 计算三轴的变异系数来判断波动大小是否符合抖动
  12. @param accel - [in] 加速度三轴
  13. @return 无
  14. */
  15. static void mode_switch(int32_t mag_x, int32_t mag_y, int32_t mag_z, uint16_t timeout)
  16. {
  17. static float32_t mag_cur_shake = 0;
  18. static uint32_t counter = 0;
  19. uint8_t flag = 0;
  20. double array[3];
  21. array[0] = mag_x;
  22. array[1] = mag_y;
  23. array[2] = mag_z;
  24. mag_cur_shake = CoefficientVariation(array, 3);
  25. // static char string[200];
  26. // sprintf(string,"mag_cur_shake:%f \r\n",mag_cur_shake);
  27. // SEGGER_RTT_printf(0,"%s",string);
  28. if((mag_cur_shake) >= USED_MAG_CHECK_WEARSHOES_VALUE){
  29. flag = 1;
  30. }
  31. if(isWearShoes && (counter >= timeout)) {
  32. isWearShoes = WEARSHOES_NO;
  33. SEGGER_RTT_printf(0,"I am not WearShoes\r\n");
  34. }
  35. else if(!isWearShoes && flag){
  36. isWearShoes = WEARSHOES_YES;
  37. SEGGER_RTT_printf(0,"I am WearShoes\r\n");
  38. }
  39. if(flag)counter = 0;
  40. else counter++;
  41. }
  42. static void hal_wearshoes_determine(uint16_t timeout)
  43. {
  44. int16_t Mag[3]={0,0,0};
  45. int32_t mag_norm;
  46. if(hal_mode_get() == HAL_MODE_STANDBY){
  47. // //获取最新一组前脚加速度
  48. // if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, Acc, NULL, NULL);
  49. IMU_Get_Back_Data(Mag);
  50. // SEGGER_RTT_printf(0,"hal_wearshoes_determine mag[0]:%d mag[1]:%d mag[2]:%d\r\n",Mag[0],Mag[1],Mag[2]);
  51. }
  52. else if(hal_mode_get() == HAL_MODE_NORMAL){
  53. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, NULL, Mag, NULL);
  54. }
  55. mag_norm = (int32_t)(sqrt((float) (Mag[0] * Mag[0] + Mag[1] * Mag[1] + Mag[2] * Mag[2])));
  56. mode_switch(mag_norm,~mag_norm,0, timeout);
  57. }
  58. void hal_wearshoes_Process(void)
  59. {
  60. if(hal_mode_get() == HAL_MODE_NORMAL && isWearShoes == WEARSHOES_NO)
  61. {
  62. hal_mode_set(HAL_MODE_STANDBY); //切换为待机模式
  63. }
  64. else if(hal_mode_get() == HAL_MODE_STANDBY && isWearShoes == WEARSHOES_YES)
  65. {
  66. hal_mode_set(HAL_MODE_NORMAL); //切换为日常模式
  67. }
  68. else if(hal_mode_get() != HAL_MODE_GAME && hal_mode_get() != HAL_MODE_REALSTEP)
  69. {
  70. hal_wearshoes_determine(HAL_WEARSHOES_TIMEOUT); //检测是否穿鞋
  71. }
  72. }
  73. uint8_t hal_wearshoes_is_wearshoes(void)
  74. {
  75. return isWearShoes;
  76. }
  77. void hal_wearshoes_Init(void)
  78. {
  79. if(INIT_MODE == HAL_MODE_NORMAL)isWearShoes = WEARSHOES_YES;
  80. else if(INIT_MODE == HAL_MODE_STANDBY)isWearShoes = WEARSHOES_NO;
  81. Process_Start(HAL_WEARSHOES_PROCESS_CYCLE,"hal_wearshoes_Process",hal_wearshoes_Process);
  82. }