app_wearshoes.c 3.0 KB

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