123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include "app_wearshoes.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_imu.h"
- #include "arm_math.h"
- #define APP_WEARSHOES_PROCESS_CYCLE 100 //线程周期,单位ms
- #define APP_WEARSHOES_TIMEOUT (APP_WEARSHOES_PROCESS_CYCLE * 6) //检测周期,当前1分钟检测一次。
- static uint8_t isWearShoes = WEARSHOES_NO;
- /**
- @brief 求变异系数的绝对值
- @param p_array-[in] 数组地址
- @param len-[in] 数组成员个数
- @return 变异系数的绝对值
- */
- static float32_t CoefficientVariation(double *p_array, uint32_t len)
- {
- int i;
- float32_t sum = 0; //总和
- float32_t avg; //平均值
- float32_t spow = 0;
- for(i=0;i<len;i++)sum += p_array[i];//求总和
- avg = sum/len;//计算平均值
- for(i=0;i<len;i++)spow += (p_array[i]-avg)*(p_array[i]-avg);//平方累加
-
- float32_t standardDeviation=0,Coefficient_variation=0;
- // static char string[500];
- if(!arm_sqrt_f32((spow/len),&standardDeviation))
- {
- // sprintf(string,"standardDeviation:%f\r\n",standardDeviation);
- // SEGGER_RTT_printf(0,"%s",string);
- standardDeviation = (standardDeviation)/avg;
- arm_abs_f32(&standardDeviation,&Coefficient_variation,1);
- // sprintf(string,"Coefficient_variation:%f\r\n",Coefficient_variation);
- // SEGGER_RTT_printf(0,"%s",string);
- }
- return Coefficient_variation;
- }
- #define USED_ACC_CHECK_WEARSHOES_VALUE (float)0.1
- #define USED_MAG_CHECK_WEARSHOES_VALUE 10000
- /**
- @brief 计算三轴的变异系数来判断波动大小是否符合抖动
- @param accel - [in] 加速度三轴
- @return 无
- */
- static void mode_switch(int32_t x, int32_t y, int32_t z, uint16_t timeout)
- {
- static float32_t cur_shake = 0, last_shake = 100;
- static uint32_t counter = 0;
- uint8_t flag = 0;
- double array[3] = {x,y,z};
-
- cur_shake = CoefficientVariation(array, 3);
-
- // //六轴触发条件
- // if((cur_shake - last_shake) >= USED_ACC_CHECK_WEARSHOES_VALUE)
- // flag = 1;
-
- //前脚地磁触发条件
- if((cur_shake - last_shake) >= USED_MAG_CHECK_WEARSHOES_VALUE)
- flag = 1;
-
- if(isWearShoes && (counter >= timeout)) {
- isWearShoes = WEARSHOES_NO;
- SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
- }
- else if(!isWearShoes && flag){
- isWearShoes = WEARSHOES_YES;
- SEGGER_RTT_printf(0,"isWearShoes,%d\r\n",isWearShoes);
- }
-
- if(flag)counter = 0;
- else counter++;
-
- last_shake = cur_shake;
- }
- static void app_wearshoes_determine(uint16_t timeout)
- {
- int16_t Acc[3]={0};
- int16_t MagFront[3]={0};
- int32_t front_mag_norm;
- IMU_Get_Index_Front_Low_Power_Data(Acc, MagFront, NULL, IMU_Get_Front_Update_Data_GroupNum()); //获取最新一组前脚地磁和加速度
-
- front_mag_norm = (int32_t)(sqrt((float) (MagFront[0] * MagFront[0] + MagFront[1] * MagFront[1] + MagFront[2] * MagFront[2])));
- mode_switch(front_mag_norm, ~front_mag_norm, 0, timeout);
- }
- void app_wearshoes_lowpower_Process(void)
- {
- if(IMU_GetCurrentMode() == STATE_LOW_POWER_MODE)//判断是否处于低功耗模式
- {
- app_wearshoes_determine(APP_WEARSHOES_TIMEOUT);
- }
- }
- uint8_t app_wearshoes_is_wearshoes(void)
- {
- return isWearShoes;
- }
- void app_wearshoes_Init(void)
- {
- Process_Start(APP_WEARSHOES_PROCESS_CYCLE,"app_wearshoes_lowpower_Process",app_wearshoes_lowpower_Process);
- }
|