app_overturn.c 7.8 KB

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