hal_wearshoes.c 3.9 KB

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