hal_wearshoes.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "arm_math.h"
  7. #define HAL_WEARSHOES_PROCESS_CYCLE 100 //线程周期,单位ms
  8. #define HAL_WEARSHOES_TIMEOUT 100 //检测周期,当前1分钟检测一次。
  9. static uint8_t isWearShoes = WEARSHOES_YES;
  10. /**
  11. @brief 求变异系数的绝对值
  12. @param p_array-[in] 数组地址
  13. @param len-[in] 数组成员个数
  14. @return 变异系数的绝对值
  15. */
  16. static float32_t CoefficientVariation(double *p_array, uint32_t len)
  17. {
  18. int i;
  19. float32_t sum = 0; //总和
  20. float32_t avg; //平均值
  21. float32_t spow = 0;
  22. for(i=0;i<len;i++)sum += p_array[i];//求总和
  23. avg = sum/len;//计算平均值
  24. for(i=0;i<len;i++)spow += (p_array[i]-avg)*(p_array[i]-avg);//平方累加
  25. float32_t standardDeviation=0,Coefficient_variation=0;
  26. // static char string[500];
  27. if(!arm_sqrt_f32((spow/len),&standardDeviation))
  28. {
  29. // sprintf(string,"standardDeviation:%f\r\n",standardDeviation);
  30. // SEGGER_RTT_printf(0,"%s",string);
  31. standardDeviation = (standardDeviation)/avg;
  32. arm_abs_f32(&standardDeviation,&Coefficient_variation,1);
  33. // sprintf(string,"Coefficient_variation:%f\r\n",Coefficient_variation);
  34. // SEGGER_RTT_printf(0,"%s",string);
  35. }
  36. return Coefficient_variation;
  37. }
  38. #define USED_ACC_CHECK_WEARSHOES_VALUE (float)2.0
  39. #define USED_MAG_CHECK_WEARSHOES_VALUE 10000
  40. /**
  41. @brief 计算三轴的变异系数来判断波动大小是否符合抖动
  42. @param accel - [in] 加速度三轴
  43. @return 无
  44. */
  45. static void mode_switch(int32_t x, int32_t y, int32_t z, uint16_t timeout)
  46. {
  47. static float32_t cur_shake = 0, last_shake = 100;
  48. static uint32_t counter = 0;
  49. uint8_t flag = 0;
  50. double array[3] = {x,y,z};
  51. cur_shake = CoefficientVariation(array, 3);
  52. //六轴触发条件
  53. if((cur_shake - last_shake) >= USED_ACC_CHECK_WEARSHOES_VALUE)
  54. flag = 1;
  55. // //前脚地磁触发条件
  56. // if((cur_shake - last_shake) >= USED_MAG_CHECK_WEARSHOES_VALUE)
  57. // flag = 1;
  58. if(isWearShoes && (counter >= timeout)) {
  59. isWearShoes = WEARSHOES_NO;
  60. SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
  61. }
  62. else if(!isWearShoes && flag){
  63. isWearShoes = WEARSHOES_YES;
  64. SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
  65. }
  66. if(flag)counter = 0;
  67. else counter++;
  68. last_shake = cur_shake;
  69. }
  70. static void hal_wearshoes_determine(uint16_t timeout)
  71. {
  72. int16_t Acc[3]={0};
  73. //获取最新一组前脚加速度
  74. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, Acc, NULL, NULL);
  75. mode_switch(Acc[0], Acc[1], Acc[2], timeout);
  76. }
  77. void hal_wearshoes_Process(void)
  78. {
  79. if(hal_mode_get() == HAL_MODE_NORMAL && isWearShoes == WEARSHOES_NO)
  80. {
  81. hal_mode_set(HAL_MODE_STANDBY); //切换为待机模式
  82. }
  83. else if(hal_mode_get() == HAL_MODE_STANDBY && isWearShoes == WEARSHOES_YES)
  84. {
  85. hal_mode_set(HAL_MODE_NORMAL); //切换为日常模式
  86. }
  87. else if(hal_mode_get() != HAL_MODE_GAME && hal_mode_get() != HAL_MODE_REALSTEP)
  88. {
  89. hal_wearshoes_determine(HAL_WEARSHOES_TIMEOUT); //检测是否穿鞋
  90. }
  91. }
  92. uint8_t hal_wearshoes_is_wearshoes(void)
  93. {
  94. return isWearShoes;
  95. }
  96. void hal_wearshoes_Init(void)
  97. {
  98. if(INIT_MODE == HAL_MODE_NORMAL)isWearShoes = WEARSHOES_YES;
  99. else if(INIT_MODE == HAL_MODE_STANDBY)isWearShoes = WEARSHOES_NO;
  100. Process_Start(HAL_WEARSHOES_PROCESS_CYCLE,"hal_wearshoes_Process",hal_wearshoes_Process);
  101. }