app_wearshoes.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "app_wearshoes.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_imu.h"
  5. #include "arm_math.h"
  6. #define APP_WEARSHOES_PROCESS_CYCLE 100 //线程周期,单位ms
  7. #define APP_WEARSHOES_TIMEOUT (APP_WEARSHOES_PROCESS_CYCLE * 6) //检测周期,当前1分钟检测一次。
  8. static uint8_t isWearShoes = WEARSHOES_NO;
  9. /**
  10. @brief 求变异系数的绝对值
  11. @param p_array-[in] 数组地址
  12. @param len-[in] 数组成员个数
  13. @return 变异系数的绝对值
  14. */
  15. static float32_t CoefficientVariation(double *p_array, uint32_t len)
  16. {
  17. int i;
  18. float32_t sum = 0; //总和
  19. float32_t avg; //平均值
  20. float32_t spow = 0;
  21. for(i=0;i<len;i++)sum += p_array[i];//求总和
  22. avg = sum/len;//计算平均值
  23. for(i=0;i<len;i++)spow += (p_array[i]-avg)*(p_array[i]-avg);//平方累加
  24. float32_t standardDeviation=0,Coefficient_variation=0;
  25. // static char string[500];
  26. if(!arm_sqrt_f32((spow/len),&standardDeviation))
  27. {
  28. // sprintf(string,"standardDeviation:%f\r\n",standardDeviation);
  29. // SEGGER_RTT_printf(0,"%s",string);
  30. standardDeviation = (standardDeviation)/avg;
  31. arm_abs_f32(&standardDeviation,&Coefficient_variation,1);
  32. // sprintf(string,"Coefficient_variation:%f\r\n",Coefficient_variation);
  33. // SEGGER_RTT_printf(0,"%s",string);
  34. }
  35. return Coefficient_variation;
  36. }
  37. #define USED_ACC_CHECK_WEARSHOES_VALUE (float)0.1
  38. #define USED_MAG_CHECK_WEARSHOES_VALUE 10000
  39. /**
  40. @brief 计算三轴的变异系数来判断波动大小是否符合抖动
  41. @param accel - [in] 加速度三轴
  42. @return 无
  43. */
  44. static void mode_switch(int32_t x, int32_t y, int32_t z, uint16_t timeout)
  45. {
  46. static float32_t cur_shake = 0, last_shake = 100;
  47. static uint32_t counter = 0;
  48. uint8_t flag = 0;
  49. double array[3] = {x,y,z};
  50. cur_shake = CoefficientVariation(array, 3);
  51. // //六轴触发条件
  52. // if((cur_shake - last_shake) >= USED_ACC_CHECK_WEARSHOES_VALUE)
  53. // flag = 1;
  54. //前脚地磁触发条件
  55. if((cur_shake - last_shake) >= USED_MAG_CHECK_WEARSHOES_VALUE)
  56. flag = 1;
  57. if(isWearShoes && (counter >= timeout)) {
  58. isWearShoes = WEARSHOES_NO;
  59. SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
  60. }
  61. else if(!isWearShoes && flag){
  62. isWearShoes = WEARSHOES_YES;
  63. SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
  64. }
  65. if(flag)counter = 0;
  66. else counter++;
  67. last_shake = cur_shake;
  68. }
  69. static void app_wearshoes_determine(uint16_t timeout)
  70. {
  71. int16_t Acc[3]={0};
  72. int16_t MagFront[3]={0};
  73. int32_t front_mag_norm;
  74. IMU_Get_Index_Front_Low_Power_Data(Acc, MagFront, NULL, IMU_Get_Front_Update_Data_GroupNum()); //获取最新一组前脚地磁和加速度
  75. front_mag_norm = (int32_t)(sqrt((float) (MagFront[0] * MagFront[0] + MagFront[1] * MagFront[1] + MagFront[2] * MagFront[2])));
  76. mode_switch(front_mag_norm, ~front_mag_norm, 0, timeout);
  77. }
  78. void app_wearshoes_lowpower_Process(void)
  79. {
  80. if(IMU_GetCurrentMode() == STATE_LOW_POWER_MODE)//判断是否处于低功耗模式
  81. {
  82. app_wearshoes_determine(APP_WEARSHOES_TIMEOUT);
  83. }
  84. }
  85. uint8_t app_wearshoes_is_wearshoes(void)
  86. {
  87. return isWearShoes;
  88. }
  89. void app_wearshoes_Init(void)
  90. {
  91. Process_Start(APP_WEARSHOES_PROCESS_CYCLE,"app_wearshoes_lowpower_Process",app_wearshoes_lowpower_Process);
  92. }