press_down_detect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include "press_down_detect.h"
  2. int back_jump_stage = 0;
  3. int cancel_down = 0;
  4. int virus_flag = 0;
  5. float real_front_min_left = 50000.f;
  6. float real_front_min_right = 50000.f;
  7. void detect_up_trend(float *mag_up_min, float *mag_window, int window_size)
  8. {
  9. //1、寻找最大值
  10. float max_val = mag_window[0];
  11. for(int i = 1; i < window_size; i++)
  12. {
  13. if(max_val < mag_window[i])
  14. {
  15. max_val = mag_window[i];
  16. }
  17. }
  18. if(max_val - mag_window[0] > 100.0f)
  19. {
  20. if(mag_window[0] < *mag_up_min)
  21. {
  22. *mag_up_min = mag_window[0];
  23. }
  24. }
  25. else
  26. {
  27. *mag_up_min = 40000.f;
  28. }
  29. }
  30. void avoid_down_apeear_dual_foot_run(float *mag_window,float *another_mag_window, int window_size, float *trend_min, float *trend_max, float *down_max, float *down_min)
  31. {
  32. float max_val = mag_window[0];
  33. for(int i = 1; i < window_size; i++)
  34. {
  35. if(max_val < mag_window[i])
  36. {
  37. max_val = mag_window[i];
  38. }
  39. }
  40. if(max_val - mag_window[0] > 200.0f)
  41. {
  42. if(*trend_min > mag_window[0])
  43. {
  44. *trend_min = mag_window[0];
  45. }
  46. if( *trend_max < max_val)
  47. {
  48. *trend_max = max_val;
  49. }
  50. *down_max = another_mag_window[window_size - 1];
  51. *down_min = another_mag_window[window_size - 1];
  52. }
  53. else if(mag_window[window_size - 1] - max_val < -200.0f)
  54. {
  55. if(mag_window[window_size - 1] < *trend_max)
  56. {
  57. *trend_max = mag_window[window_size - 1];
  58. }
  59. if(max_val > *trend_min)
  60. {
  61. *trend_min = max_val;
  62. }
  63. if(*down_max < another_mag_window[window_size - 1])
  64. {
  65. *down_max = another_mag_window[window_size - 1];
  66. }
  67. if(*down_min > another_mag_window[window_size - 1])
  68. {
  69. *down_min = another_mag_window[window_size - 1];
  70. }
  71. }
  72. }
  73. int station_acc(float* acc_buff, int buff_size)
  74. {
  75. float max_val = acc_buff[0];
  76. float min_val = acc_buff[0];
  77. for(int i = 1; i < buff_size; i++)
  78. {
  79. if(max_val < acc_buff[i] )
  80. {
  81. max_val = acc_buff[i];
  82. }
  83. if(min_val > acc_buff[i] )
  84. {
  85. min_val = acc_buff[i];
  86. }
  87. }
  88. if(max_val - min_val < 0.3f)
  89. {
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. int press_down_detect_new(int index, float front_mag_left, float back_mag_left,
  95. float front_mag_right, float back_mag_right,
  96. float left_acc_x,float left_acc_y, float left_acc_z, float right_acc_x, float right_acc_y, float right_acc_z,
  97. int *front_down, int *back_down)
  98. {
  99. static float front_mag_norm_left[PRESS_COUNT_MAX];
  100. static float front_mag_norm_right[PRESS_COUNT_MAX];
  101. static float back_mag_norm_left[PRESS_COUNT_MAX];
  102. static float back_mag_norm_right[PRESS_COUNT_MAX];
  103. static float acc_norm_left[10];
  104. static float acc_norm_right[10];
  105. static float left_front_up_min = 40000.0f;
  106. static float left_back_up_min = 40000.0f;
  107. static float right_front_up_min = 40000.0f;
  108. static float right_back_up_min = 40000.0f;
  109. static float right_down_left_max = 0.0f;
  110. static float right_down_left_min = 40000.f;
  111. static float left_down_right_max = 0.f;
  112. static float left_down_right_min = 40000.f;
  113. static float left_back_trend_max = 0;
  114. static float left_back_trend_min = 40000;
  115. static float right_back_trend_max = 0;
  116. static float right_back_trend_min = 40000;
  117. memcpy(front_mag_norm_left, front_mag_norm_left + 1, 4 * sizeof(float));
  118. memcpy(front_mag_norm_right, front_mag_norm_right + 1, 4 * sizeof(float));
  119. memcpy(back_mag_norm_left, back_mag_norm_left + 1, 4 * sizeof(float));
  120. memcpy(back_mag_norm_right, back_mag_norm_right + 1, 4 * sizeof(float));
  121. memcpy(acc_norm_left, acc_norm_left + 1, 9 * sizeof(float));
  122. memcpy(acc_norm_right, acc_norm_right + 1, 9 * sizeof(float));
  123. front_mag_norm_left[PRESS_COUNT_MAX - 1] = front_mag_left;
  124. front_mag_norm_right[PRESS_COUNT_MAX - 1] = front_mag_right;
  125. back_mag_norm_left[PRESS_COUNT_MAX - 1] = back_mag_left;
  126. back_mag_norm_right[PRESS_COUNT_MAX - 1] = back_mag_right;
  127. acc_norm_left[9] = sqrt(left_acc_x * left_acc_x + left_acc_y * left_acc_y + left_acc_z * left_acc_z);
  128. acc_norm_right[9] = sqrt(right_acc_x * right_acc_x + right_acc_y * right_acc_y + right_acc_z * right_acc_z);
  129. detect_up_trend(&left_front_up_min, front_mag_norm_left, PRESS_COUNT_MAX);
  130. detect_up_trend(&right_front_up_min, front_mag_norm_right, PRESS_COUNT_MAX);
  131. detect_up_trend(&left_back_up_min, back_mag_norm_left, PRESS_COUNT_MAX);
  132. detect_up_trend(&right_back_up_min, back_mag_norm_right, PRESS_COUNT_MAX);
  133. if(front_mag_left - left_front_up_min > 1000.0f && front_mag_right - right_front_up_min > 1000.0f )
  134. {
  135. *front_down = 1;
  136. }
  137. if(back_mag_left - left_back_up_min > 2000.0f && back_mag_right - right_back_up_min > 2000.0f )
  138. {
  139. *back_down = 1;
  140. }
  141. avoid_down_apeear_dual_foot_run(back_mag_norm_left, back_mag_norm_right, PRESS_COUNT_MAX, &left_back_trend_min, &left_back_trend_max, &left_down_right_max ,&left_down_right_min);
  142. avoid_down_apeear_dual_foot_run(back_mag_norm_right, back_mag_norm_left, PRESS_COUNT_MAX, &right_back_trend_min, &right_back_trend_max, &right_down_left_max ,&right_down_left_min);
  143. if((right_back_trend_max - right_back_trend_min > 500.f && left_back_trend_max - left_back_trend_min < -500.f)||
  144. (right_back_trend_max - right_back_trend_min < -500.f && left_back_trend_max - left_back_trend_min> 500.f) )
  145. {
  146. *front_down = 0;
  147. *back_down = 0;
  148. }
  149. if(left_back_trend_max - left_back_trend_min < -2000.f && right_down_left_max - right_down_left_min < 1000.f)
  150. {
  151. *front_down = 0;
  152. *back_down = 0;
  153. }
  154. if(right_back_trend_max - right_back_trend_min < -2000.f && left_down_right_max - left_down_right_min < 1000.f)
  155. {
  156. *front_down = 0;
  157. *back_down = 0;
  158. }
  159. if(!station_acc(acc_norm_left, 10) || !station_acc(acc_norm_right, 10))
  160. {
  161. *front_down = 0;
  162. }
  163. // if(*back_down || *front_down)
  164. // {
  165. // left_front_up_min = 40000.0f;
  166. // left_back_up_min = 40000.0f;
  167. //
  168. // right_front_up_min = 40000.0f;
  169. // right_back_up_min = 40000.0f;
  170. //
  171. // }
  172. return 0;
  173. }
  174. //short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  175. //{
  176. // static int last_h_z;
  177. // static int last_s_z;
  178. // static int left_up;
  179. // static int right_up;
  180. //
  181. // static int left_up_count;
  182. // static int right_up_count;
  183. //
  184. // if(h_pos[2] - last_h_z > 0 && h_pos[2]> 0)
  185. // {
  186. // left_up = 1;
  187. // }
  188. // else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  189. // {
  190. // left_up = -1;
  191. // }
  192. //
  193. // if(left_up == 1)
  194. // {
  195. // left_up_count ++;
  196. // }
  197. // else
  198. // {
  199. // left_up_count = 0;
  200. // }
  201. //
  202. // if(s_pos[2] - last_s_z > 0 && s_pos[2] > 0)
  203. // {
  204. // right_up = 1;
  205. // }
  206. // else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  207. // {
  208. // right_up = -1;
  209. // }
  210. //
  211. // if(right_up == 1)
  212. // {
  213. // right_up_count ++;
  214. // }
  215. // else
  216. // {
  217. // right_up_count = 0;
  218. // }
  219. //
  220. // last_h_z = h_pos[2];
  221. // last_s_z = s_pos[2];
  222. //
  223. // if(left_up == 1 && right_up == 1 && right_up_count < 15 && left_up_count < 20
  224. // && right_up_count > 2 && left_up_count > 2)
  225. // {
  226. // return 1;
  227. // }
  228. //
  229. // return 0;
  230. //}
  231. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  232. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  233. {
  234. *max_val = data[0];
  235. *max_index = 0;
  236. *min_val = data[0];
  237. *min_index = 0;
  238. for(int i = 0; i < length; i++)
  239. {
  240. if(*max_val < data[i])
  241. {
  242. *max_val = data[i];
  243. *max_index = i;
  244. }
  245. if(*min_val > data[i])
  246. {
  247. *min_val = data[i];
  248. *min_index = i;
  249. }
  250. }
  251. }
  252. //short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int left_front_press, int right_front_press)
  253. //{
  254. //
  255. // static float right_data_z_buff[10];
  256. // static float left_data_z_buff[10];
  257. //
  258. // static int last_right_press;
  259. // static int last_left_press;
  260. // static int left_max_press;
  261. // static int right_max_press;
  262. //
  263. // static int wait;
  264. //
  265. // /*
  266. // * 存储数据
  267. // */
  268. // memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  269. //
  270. // memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  271. //
  272. // right_data_z_buff[9] = right_acc_z;
  273. // left_data_z_buff[9] = left_acc_z;
  274. //
  275. // if(left_zupt && right_zupt)
  276. // {
  277. // wait = 30;
  278. // }
  279. //
  280. // /*
  281. // * 检测压力下降用的逻辑
  282. // */
  283. // if(last_left_press - left_front_press > 200)
  284. // {
  285. // if(last_left_press > left_max_press)
  286. // {
  287. // left_max_press = last_left_press;
  288. // }
  289. // }
  290. // else if(last_left_press - left_front_press < -200)
  291. // {
  292. // left_max_press = 0;
  293. // }
  294. //
  295. // last_left_press = left_front_press;
  296. //
  297. // if(last_right_press - right_front_press > 200)
  298. // {
  299. // if(last_right_press > right_max_press)
  300. // {
  301. // right_max_press = last_right_press;
  302. // }
  303. // }
  304. // else if(last_right_press - right_front_press < -200)
  305. // {
  306. // right_max_press = 0;
  307. // }
  308. //
  309. // last_right_press = right_front_press;
  310. //
  311. // /*
  312. // * 加速变化估计
  313. // */
  314. //
  315. // float max_val_right, min_val_right, max_val_left, min_val_left;
  316. //
  317. // int max_index_right, min_index_right, max_index_left, min_index_left;
  318. //
  319. //
  320. // max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  321. //
  322. // max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  323. //
  324. // if(wait > 0)
  325. // {
  326. // wait --;
  327. // }
  328. //
  329. // /*
  330. // * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  331. // */
  332. // if( max_index_right < min_index_right && max_index_left < min_index_left &&
  333. //
  334. // max_val_right - min_val_right > 1.3f && max_val_left - min_val_left > 1.3f && wait > 0 && left_zupt == 0 && right_zupt == 0
  335. //
  336. // && left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000 )
  337. // //if(left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000)
  338. // {
  339. // SEGGER_RTT_printf(0, "test\n");
  340. // SEGGER_RTT_printf(0," left_max_press - left_front_press= %d , right_max_press - right_front_press = %d\n", left_max_press - left_front_press, right_max_press - right_front_press);
  341. // return 1;
  342. // }
  343. //
  344. // return 0;
  345. //
  346. //}
  347. short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int left_front_press, int right_front_press)
  348. {
  349. static float right_data_z_buff[10];
  350. static float left_data_z_buff[10];
  351. static float right_data_press[10];
  352. static float left_data_press[10];
  353. static int wait;
  354. /*
  355. * 存储数据
  356. */
  357. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  358. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  359. memcpy(right_data_press, right_data_press + 1, 9 * sizeof(float));
  360. memcpy(left_data_press, left_data_press + 1, 9 * sizeof(float));
  361. right_data_z_buff[9] = right_acc_z; left_data_z_buff[9] = left_acc_z;
  362. right_data_press[9] = right_front_press; left_data_press[9] = left_front_press;
  363. if(left_zupt && right_zupt)
  364. {
  365. wait = 30;
  366. }
  367. /*
  368. * 检测压力下降用的逻辑
  369. */
  370. float max_val_right_press, min_val_right_press, max_val_left_press, min_val_left_press;
  371. int max_index_right_press, min_index_right_press, max_index_left_press, min_index_left_press;
  372. max_min_window(right_data_press, 10, &max_val_right_press , &max_index_right_press, &min_val_right_press, &min_index_right_press);
  373. max_min_window(left_data_press, 10, &max_val_left_press , &max_index_left_press, &min_val_left_press, &min_index_left_press);
  374. /*
  375. * 加速变化估计
  376. */
  377. float max_val_right, min_val_right, max_val_left, min_val_left;
  378. int max_index_right, min_index_right, max_index_left, min_index_left;
  379. max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  380. max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  381. if(wait > 0)
  382. {
  383. wait --;
  384. }
  385. /*
  386. * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  387. */
  388. if( max_index_right < min_index_right && max_index_left < min_index_left &&
  389. max_val_right - min_val_right > 0.8f && max_val_left - min_val_left > 0.8f && wait > 0 && left_zupt == 0 && right_zupt == 0
  390. && max_index_right_press < min_index_right_press && max_index_left_press < min_index_left_press &&
  391. max_val_right_press - min_val_right_press > 3000.f && max_val_left_press - min_val_left_press > 3000.f)
  392. //if(left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000)
  393. {
  394. SEGGER_RTT_printf(0, "test\n");
  395. return 1;
  396. }
  397. return 0;
  398. }