app_overturn.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "usr_config.h"
  2. #include "hal_battery.h"
  3. #include "nrf_drv_saadc.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_led.h"
  7. #include "hal_ser_imu_mode_manage.h"
  8. #include "app_overturn.h"
  9. #include "hal_wearshoes.h"
  10. #include "ble_comm.h"
  11. #define SLIDE_WINDOW_LEN 5
  12. typedef struct
  13. {
  14. int16_t max_temp;
  15. uint16_t time_count;
  16. }ACC_TIME_TYPE;
  17. typedef struct
  18. {
  19. ACC_TIME_TYPE slide_window[SLIDE_WINDOW_LEN];
  20. int window_length;
  21. }SLIDE_WINDOW;
  22. static uint8_t flag_move =0;
  23. void sort_silde_window(SLIDE_WINDOW *max_slide_window, SLIDE_WINDOW *min_slide_window, int16_t acc_val, uint16_t time_count)
  24. {
  25. ACC_TIME_TYPE acc_time_type_temp = {acc_val, time_count};
  26. uint8_t i=0;
  27. if(time_count == 0 && 0 != max_slide_window->window_length ){//时间搓归零,重置
  28. DEBUG_LOG("sort_silde_window\n");
  29. for(i=0;i<SLIDE_WINDOW_LEN;i++){
  30. max_slide_window->slide_window[i].time_count =i;
  31. min_slide_window->slide_window[i].time_count = i;
  32. }
  33. time_count = SLIDE_WINDOW_LEN;
  34. }
  35. //检测当前的值是是否是最大值
  36. if(max_slide_window->window_length == 0)
  37. {
  38. max_slide_window->slide_window[0] = acc_time_type_temp;
  39. max_slide_window->window_length = 1;
  40. }
  41. else
  42. {
  43. //先判断长度是否溢出来了
  44. while(time_count - max_slide_window->slide_window[0].time_count >= SLIDE_WINDOW_LEN)
  45. {
  46. memcpy(max_slide_window->slide_window, max_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  47. max_slide_window->window_length --;
  48. }
  49. //再检测新的元素需要前面的元素小
  50. while(max_slide_window->window_length > 0)
  51. {
  52. if(max_slide_window->slide_window[max_slide_window->window_length - 1].max_temp < acc_val)
  53. {
  54. max_slide_window->window_length --;
  55. }
  56. else
  57. {
  58. break;
  59. }
  60. }
  61. max_slide_window->slide_window[max_slide_window->window_length] = acc_time_type_temp;
  62. max_slide_window->window_length ++;
  63. }
  64. //检测当前的值是是否是最大值
  65. if(min_slide_window->window_length == 0)
  66. {
  67. min_slide_window->slide_window[0] = acc_time_type_temp;
  68. min_slide_window->window_length = 1;
  69. }
  70. else
  71. {
  72. //先判断长度是否溢出来了
  73. while(time_count - min_slide_window->slide_window[0].time_count >= SLIDE_WINDOW_LEN)
  74. {
  75. memcpy(min_slide_window->slide_window, min_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  76. min_slide_window->window_length --;
  77. }
  78. //再检测新的元素需要前面的元素小
  79. while(min_slide_window->window_length > 0)
  80. {
  81. if(min_slide_window->slide_window[min_slide_window->window_length - 1].max_temp > acc_val)
  82. {
  83. min_slide_window->window_length --;
  84. }
  85. else
  86. {
  87. break;
  88. }
  89. }
  90. min_slide_window->slide_window[min_slide_window->window_length] = acc_time_type_temp;
  91. min_slide_window->window_length ++;
  92. }
  93. }
  94. /************************ 踮脚显示电量 ***********************************/
  95. //没穿鞋的情况下
  96. void app_BatDispaly_Process_N(void)
  97. {
  98. ser_imu_data_t data= {0};
  99. static uint8_t lED_LIFE_SIGNAL = 0;
  100. static uint8_t lED_LIFE_TIME = 0;
  101. // DEBUG_LOG("app_BatDispaly_Process_N;\n");
  102. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY))
  103. {
  104. return;//停止状态
  105. }
  106. //获取最新一组前脚加速度
  107. int16_t data_size = 0;
  108. data_size = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  109. if(data_size > 0){
  110. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, data_size -1 , &data);
  111. int32_t acc_norm = (data.acc[0] * data.acc[0] +data.acc[1] * data.acc[1] + data.acc[2] * data.acc[2]);
  112. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536)
  113. {
  114. lED_LIFE_SIGNAL = 3;
  115. }
  116. else if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  117. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] < -1024)
  118. {
  119. lED_LIFE_SIGNAL = 0;
  120. }
  121. // DEBUG_LOG("acc_norm:%d,lED_LIFE_SIGNAL:%d,data.acc2:%d,lED_LIFE_TIME:%d\n",abs(acc_norm - 4194304),lED_LIFE_SIGNAL,data.acc[2],lED_LIFE_TIME);
  122. if(lED_LIFE_SIGNAL > 0)
  123. {
  124. // DEBUG_LOG("unwearshoes LED_Start(LED_OVERTURN,COLOR_GREEN);;\n");
  125. if(lED_LIFE_TIME == 0){
  126. lED_LIFE_TIME = (10000/StandByPower_Interval);
  127. if(GetBatteryPersent()>20){
  128. LED_Start(LED_OVERTURN,COLOR_GREEN);
  129. }else{
  130. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  131. }
  132. Process_SetHoldOn(app_BatDispaly_Process_N,1);
  133. }
  134. else if (2 == lED_LIFE_TIME){
  135. LED_Stop(LED_OVERTURN);
  136. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  137. }
  138. if (lED_LIFE_TIME > 1)lED_LIFE_TIME--;
  139. }
  140. else
  141. {
  142. // DEBUG_LOG("unwearshoes LED_Stop(LED_OVERTURN);\n");
  143. if(0 != lED_LIFE_TIME ){lED_LIFE_TIME =0;
  144. LED_Stop(LED_OVERTURN);
  145. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  146. }
  147. if(1 == flag_move){flag_move =0;
  148. LED_Stop(LED_OVERTURN);
  149. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  150. }
  151. }
  152. }
  153. }
  154. //穿鞋的情况下
  155. void app_BatDispaly_Process(void)
  156. {
  157. ser_imu_data_t data={0};
  158. static uint16_t time_count = 0;
  159. static uint16_t station_count =0;
  160. static uint16_t overturn_180_count = 0;
  161. static uint8_t lED_LIFE_SIGNAL = 0;
  162. static uint8_t lED_LIFE_TIME = 0;
  163. static SLIDE_WINDOW max_slide_window = { {-3096,0},0 };
  164. static SLIDE_WINDOW min_slide_window = { {3096,0},0 };
  165. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL))
  166. {
  167. return;//走路状态
  168. }
  169. // DEBUG_LOG("app_BatDispaly_Process;\n");
  170. //获取最新一组前脚加速度, 有fifo
  171. int16_t data_size = 0;
  172. data_size = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  173. if(data_size > 0){
  174. // DEBUG_LOG("data_size : %d, hal_wearshoes_is_wearshoes: %d\n", data_size,hal_wearshoes_is_wearshoes());
  175. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, data_size-1 , &data);
  176. //耗时较少的计算滑动窗口的最大值、最小值
  177. sort_silde_window(&max_slide_window, &min_slide_window, data.acc[0], time_count);
  178. if(time_count > SLIDE_WINDOW_LEN)
  179. {
  180. if(data.acc[0] - min_slide_window.slide_window[0].max_temp < 256 && max_slide_window.slide_window[0].max_temp - data.acc[0] < 256
  181. && max_slide_window.slide_window[0].max_temp - min_slide_window.slide_window[0].max_temp < 256 && data.acc[0] < -1400)
  182. {
  183. station_count++;
  184. }
  185. else station_count =0;
  186. }
  187. time_count++;
  188. //在穿鞋状态下,被翻转了
  189. int32_t acc_norm = (data.acc[0] * data.acc[0] +data.acc[1] * data.acc[1] + data.acc[2] * data.acc[2]);
  190. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536)
  191. {
  192. //DEBUG_LOG(" wear shoes if(abs(acc_norm - 4194304) < 262144 && data.acc[2] > 1536);\n");
  193. overturn_180_count ++;
  194. }
  195. else
  196. {
  197. overturn_180_count = 0;
  198. }
  199. if(overturn_180_count >= 5 || station_count > 4)
  200. {
  201. // DEBUG_LOG(" overturn_180_count > 5;\n");
  202. lED_LIFE_SIGNAL = 6;
  203. }
  204. if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  205. if(lED_LIFE_SIGNAL > 0)
  206. {
  207. // DEBUG_LOG("wearshoes LED_Start(LED_OVERTURN,COLOR_GREEN);;\n");
  208. if(lED_LIFE_TIME == 0){
  209. lED_LIFE_TIME = (10000/LowPower_Interval);
  210. if(GetBatteryPersent()>20){
  211. LED_Start(LED_OVERTURN,COLOR_GREEN);
  212. }else{
  213. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  214. }
  215. Process_SetHoldOn(app_BatDispaly_Process,1);
  216. }
  217. else if (2 == lED_LIFE_TIME){
  218. LED_Stop(LED_OVERTURN);
  219. Process_SetHoldOn(app_BatDispaly_Process,0);
  220. }
  221. if (lED_LIFE_TIME > 1)lED_LIFE_TIME--;
  222. }
  223. else
  224. {
  225. // DEBUG_LOG("wearshoes LED_Stop(LED_OVERTURN);\n");
  226. if(0 != lED_LIFE_TIME ){lED_LIFE_TIME =0;
  227. LED_Stop(LED_OVERTURN);
  228. Process_SetHoldOn(app_BatDispaly_Process,0);
  229. }
  230. if(0 == flag_move){flag_move =1;
  231. LED_Stop(LED_OVERTURN);
  232. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  233. }
  234. }
  235. }
  236. }
  237. void app_overturn_Init(void)
  238. {
  239. Process_Start(LowPower_Interval,"BatDispaly",app_BatDispaly_Process);
  240. Process_Start(StandByPower_Interval,"BatDispaly_N",app_BatDispaly_Process_N);
  241. }