app_overturn - 副本 (2).c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // DEBUG_LOG("max_slide_window->slide_window[0]:0%2x,0%2x\n",max_slide_window->slide_window[0],&max_slide_window->slide_window[0]);
  27. //检测当前的值是是否是最大值
  28. if(max_slide_window->window_length == 0)
  29. {
  30. max_slide_window->slide_window[0] = acc_time_type_temp;
  31. max_slide_window->window_length = 1;
  32. }
  33. else
  34. {
  35. //先判断长度是否溢出来了
  36. DEBUG_LOG("sort_silde_window>>>>>>>>:%d,%d\n",time_count,max_slide_window->slide_window[0].time_count);
  37. while(time_count - max_slide_window->slide_window[0].time_count >= SLIDE_WINDOW_LEN)
  38. {
  39. memcpy(max_slide_window->slide_window, max_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  40. max_slide_window->window_length --;
  41. DEBUG_LOG("111111 window_length:%d\n",max_slide_window->window_length);
  42. }
  43. DEBUG_LOG("22222 window_length:%d,time_count:%d\n",max_slide_window->window_length,time_count);
  44. // //再检测新的元素需要前面的元素小
  45. // while(max_slide_window->window_length > 0)
  46. // {
  47. // DEBUG_LOG("33333 window_length:%d\n",max_slide_window->window_length);
  48. // if(max_slide_window->slide_window[max_slide_window->window_length - 1].max_temp < acc_val)
  49. // {
  50. // DEBUG_LOG("44444 window_length:%d\n",max_slide_window->window_length);
  51. // max_slide_window->window_length --;
  52. // }
  53. // else
  54. // {
  55. // break;
  56. // }
  57. // }
  58. DEBUG_LOG("5555 window_length:%d\n",max_slide_window->window_length);
  59. max_slide_window->slide_window[max_slide_window->window_length] = acc_time_type_temp;
  60. max_slide_window->window_length ++;
  61. DEBUG_LOG("<<<<<<<<<<<sort_silde_window end :%d\n",max_slide_window->window_length);
  62. }
  63. //检测当前的值是是否是最小值
  64. if(min_slide_window->window_length == 0)
  65. {
  66. min_slide_window->slide_window[0] = acc_time_type_temp;
  67. min_slide_window->window_length = 1;
  68. }
  69. else
  70. {
  71. //先判断长度是否溢出来了
  72. while(time_count - min_slide_window->slide_window[0].time_count >= SLIDE_WINDOW_LEN)
  73. {
  74. memcpy(min_slide_window->slide_window, min_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  75. min_slide_window->window_length --;
  76. }
  77. //再检测新的元素需要前面的元素小
  78. while(min_slide_window->window_length > 0)
  79. {
  80. if(min_slide_window->slide_window[min_slide_window->window_length - 1].max_temp > acc_val)
  81. {
  82. min_slide_window->window_length --;
  83. }
  84. else
  85. {
  86. break;
  87. }
  88. }
  89. min_slide_window->slide_window[min_slide_window->window_length] = acc_time_type_temp;
  90. min_slide_window->window_length ++;
  91. }
  92. }
  93. /************************ 踮脚显示电量 ***********************************/
  94. //没穿鞋的情况下
  95. void app_BatDispaly_Process_N(void)
  96. {
  97. ser_imu_data_t data= {0};
  98. static uint8_t lED_LIFE_SIGNAL = 0;
  99. static uint8_t lED_LIFE_TIME = 0;
  100. // DEBUG_LOG("app_BatDispaly_Process_N;\n");
  101. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY))
  102. {
  103. return;//停止状态
  104. }
  105. //获取最新一组前脚加速度
  106. int16_t data_size = 0;
  107. data_size = hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT);
  108. if(data_size > 0){
  109. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, data_size -1 , &data);
  110. int32_t acc_norm = (data.acc[0] * data.acc[0] +data.acc[1] * data.acc[1] + data.acc[2] * data.acc[2]);
  111. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536)
  112. {
  113. lED_LIFE_SIGNAL = 3;
  114. }
  115. else if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  116. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] < -1024)
  117. {
  118. lED_LIFE_SIGNAL = 0;
  119. }
  120. DEBUG_LOG("000000 data.acc[2]:%d\n",data.acc[2]);
  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 uint8_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] < -1500)
  182. {
  183. station_count++;
  184. }
  185. else station_count =0;
  186. }
  187. time_count++;
  188. // DEBUG_LOG("111111 %d:%d:%d:%d\n",(data.acc[0] - min_slide_window.slide_window[0].max_temp),
  189. // (max_slide_window.slide_window[0].max_temp - data.acc[0]),
  190. // (max_slide_window.slide_window[0].max_temp - min_slide_window.slide_window[0].max_temp),data.acc[0]);
  191. //在穿鞋状态下,被翻转了
  192. // DEBUG_LOG("111111 data.acc[2]:%d\n",data.acc[2]);
  193. int32_t acc_norm = (data.acc[0] * data.acc[0] +data.acc[1] * data.acc[1] + data.acc[2] * data.acc[2]);
  194. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536)
  195. {
  196. //DEBUG_LOG(" wear shoes if(abs(acc_norm - 4194304) < 262144 && data.acc[2] > 1536);\n");
  197. overturn_180_count ++;
  198. }
  199. else
  200. {
  201. overturn_180_count = 0;
  202. }
  203. if(overturn_180_count >= 5 || station_count > 4)
  204. {
  205. // DEBUG_LOG(" overturn_180_count > 5;\n");
  206. lED_LIFE_SIGNAL = 6;
  207. }
  208. if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  209. if(lED_LIFE_SIGNAL > 0)
  210. {
  211. // DEBUG_LOG("wearshoes LED_Start(LED_OVERTURN,COLOR_GREEN);;\n");
  212. if(lED_LIFE_TIME == 0){
  213. lED_LIFE_TIME = (10000/LowPower_Interval);
  214. if(GetBatteryPersent()>20){
  215. LED_Start(LED_OVERTURN,COLOR_GREEN);
  216. }else{
  217. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  218. }
  219. Process_SetHoldOn(app_BatDispaly_Process,1);
  220. }
  221. else if (2 == lED_LIFE_TIME){
  222. LED_Stop(LED_OVERTURN);
  223. Process_SetHoldOn(app_BatDispaly_Process,0);
  224. }
  225. if (lED_LIFE_TIME > 1)lED_LIFE_TIME--;
  226. }
  227. else
  228. {
  229. // DEBUG_LOG("wearshoes LED_Stop(LED_OVERTURN);\n");
  230. if(0 != lED_LIFE_TIME ){lED_LIFE_TIME =0;
  231. LED_Stop(LED_OVERTURN);
  232. Process_SetHoldOn(app_BatDispaly_Process,0);
  233. }
  234. if(0 == flag_move){flag_move =1;
  235. LED_Stop(LED_OVERTURN);
  236. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  237. }
  238. }
  239. }
  240. }
  241. void app_overturn_Init(void)
  242. {
  243. Process_Start(LowPower_Interval,"BatDispaly",app_BatDispaly_Process);
  244. Process_Start(StandByPower_Interval,"BatDispaly_N",app_BatDispaly_Process_N);
  245. }