hal_wearshoes - 副本.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "math.h"
  7. #include "exception.h"
  8. #include "app_flash.h"
  9. #define HAL_WEARSHOES_PROCESS_CYCLE StandByPower_Interval //线程周期,单位ms
  10. #define HAL_WEARSHOES_TIMEOUT (60000/StandByPower_Interval) //检测周期,当前10S检测一次。
  11. static uint8_t isWearShoes = WEARSHOES_YES;
  12. /**
  13. @brief 计算三轴的变异系数来判断波动大小是否符合抖动
  14. @param accel - [in] 加速度三轴
  15. @return 无
  16. */
  17. #define Bufflength 6
  18. static int32_t buff[Bufflength]={0};
  19. static uint8_t FirstFlag =0;
  20. static void mode_switch(int32_t mag_x, int32_t mag_y, int32_t mag_z, uint16_t timeout)
  21. {
  22. static uint32_t counter = 0;
  23. uint8_t flag = 0;
  24. int32_t LvBobuff[Bufflength]={0};
  25. int32_t temp =0;
  26. uint8_t i=0;
  27. if(0 == FirstFlag){
  28. FirstFlag =1;
  29. for(i=0;i< Bufflength;i++){
  30. buff[i]=mag_x;
  31. }
  32. }else{
  33. for(i=0;i<(Bufflength-1);i++){
  34. buff[i]=buff[i+1];
  35. }
  36. buff[Bufflength-1] = mag_x;
  37. }
  38. memcpy(LvBobuff,buff,sizeof(buff));
  39. //冒泡排序
  40. for(uint8_t d=0;d<(Bufflength-1);d++){
  41. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  42. if(LvBobuff[b]>LvBobuff[b+1]){
  43. temp=LvBobuff[b+1];
  44. LvBobuff[b+1]=LvBobuff[b];
  45. LvBobuff[b]=temp;
  46. }
  47. }
  48. }
  49. // for(i=0;i<(Bufflength-1);i++)
  50. // SEGGER_RTT_printf(0,"LvBobuff[5]:%d,LvBobuff[0]:%d\r\n",LvBobuff[Bufflength-1],LvBobuff[0]);
  51. if((LvBobuff[Bufflength-1] - LvBobuff[0]) >= 1000){
  52. flag = 1;
  53. }
  54. if(isWearShoes && (counter >= timeout)) {
  55. isWearShoes = WEARSHOES_NO;
  56. SEGGER_RTT_printf(0,"I am not WearShoes\r\n");
  57. }
  58. else if(!isWearShoes && flag){
  59. isWearShoes = WEARSHOES_YES;
  60. SEGGER_RTT_printf(0,"I am WearShoes\r\n");
  61. }
  62. if(flag)counter = 0;
  63. else counter++;
  64. }
  65. static void hal_wearshoes_determine(uint16_t timeout)
  66. {
  67. int16_t Mag[3]={0,0,0};
  68. int32_t mag_norm;
  69. ser_imu_data_t data;
  70. static uint8_t state =0;
  71. switch(state){
  72. case 0:
  73. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY) != -1){
  74. // //获取最新一组后脚磁力计
  75. 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);
  76. Mag[0] = data.mag[0];Mag[1] = data.mag[1];Mag[2] = data.mag[2];
  77. // SEGGER_RTT_printf(0,"hal_wearshoes_determine mag[0]:%d mag[1]:%d mag[2]:%d\r\n",Mag[0],Mag[1],Mag[2]);
  78. }else{
  79. state =1;
  80. FirstFlag =0;
  81. }
  82. break;
  83. case 1:
  84. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL) != -1){
  85. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  86. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  87. Mag[0] = data.mag[0];Mag[1] = data.mag[1];Mag[2] = data.mag[2];
  88. }
  89. }
  90. else{
  91. state =0;
  92. FirstFlag =0;
  93. }
  94. break;
  95. default:
  96. state =0;
  97. break;
  98. }
  99. // if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY) != -1){
  100. // //获取最新一组前脚加速度
  101. // if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, Acc, NULL, NULL);
  102. // 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);
  103. // Mag[0] = data.mag[0];Mag[1] = data.mag[1];Mag[2] = data.mag[2];
  104. // SEGGER_RTT_printf(0,"hal_wearshoes_determine mag[0]:%d mag[1]:%d mag[2]:%d\r\n",Mag[0],Mag[1],Mag[2]);
  105. // }
  106. // else if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL) != -1){
  107. // if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  108. // hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  109. // Mag[0] = data.mag[0];Mag[1] = data.mag[1];Mag[2] = data.mag[2];
  110. // }
  111. // }
  112. mag_norm = (int32_t)(sqrt((float) (Mag[0] * Mag[0] + Mag[1] * Mag[1] + Mag[2] * Mag[2])));
  113. if(mag_norm != 0)mode_switch(mag_norm,~mag_norm,0, timeout);
  114. }
  115. void hal_wearshoes_Process(void)
  116. {
  117. if((hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL) != -1) && isWearShoes == WEARSHOES_NO)
  118. {
  119. //切换为待机模式
  120. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_NORMAL, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  121. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_STANDBY, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  122. }
  123. else if((hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY) != -1) && isWearShoes == WEARSHOES_YES)
  124. {
  125. //切换为日常模式
  126. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_NORMAL, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  127. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_STANDBY, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  128. }
  129. else
  130. {
  131. hal_wearshoes_determine(HAL_WEARSHOES_TIMEOUT); //检测是否穿鞋
  132. }
  133. }
  134. uint8_t hal_wearshoes_is_wearshoes(void)
  135. {
  136. return isWearShoes;
  137. }
  138. void hal_wearshoes_Init(void)
  139. {
  140. if(INIT_MODE == 1){
  141. isWearShoes = WEARSHOES_NO;
  142. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_STANDBY, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  143. }else if(INIT_MODE == 2){
  144. isWearShoes = WEARSHOES_YES;
  145. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_NORMAL, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  146. }
  147. Process_Start(HAL_WEARSHOES_PROCESS_CYCLE,"hal_wearshoes_Process",hal_wearshoes_Process);
  148. }