press_down_detect.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 check_dual_front_press_down(float *left_mag_window, float *right_mag_window, int length)
  95. {
  96. float left_max_val = left_mag_window[0];
  97. float right_max_val = right_mag_window[0];
  98. float left_min_val = left_mag_window[0];
  99. float right_min_val = right_mag_window[0];
  100. int left_max_index = 0; int right_max_index = 0;
  101. int left_min_index = 0; int right_min_index = 0;
  102. for(int i = 1; i < length; i++)
  103. {
  104. if(left_max_val < left_mag_window[i])
  105. {
  106. left_max_val = left_mag_window[i];
  107. left_max_index = i;
  108. }
  109. if(right_max_val < right_mag_window[i])
  110. {
  111. right_max_val = right_mag_window[i];
  112. right_max_index = i;
  113. }
  114. if(left_min_val > left_mag_window[i])
  115. {
  116. left_min_val = left_mag_window[i];
  117. left_min_index = i;
  118. }
  119. if(right_min_val > right_mag_window[i])
  120. {
  121. right_min_val = right_mag_window[i];
  122. right_min_index = i;
  123. }
  124. }
  125. if(right_min_index > right_max_index && left_min_index > left_max_index
  126. && left_max_val > left_min_val + 2000.0f && right_max_val > right_min_val + 2000.0f)
  127. {
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. int back_mag_different_trend(float *left_back_buff, float * right_back_buff, int length)
  133. {
  134. int left_max_index = 0; int right_max_index = 0;
  135. int left_min_index = 0; int right_min_index = 0;
  136. float left_max_val = left_back_buff[0]; float right_max_val = right_back_buff[0];
  137. float left_min_val = left_back_buff[0]; float right_min_val = right_back_buff[0];
  138. for(int i = 0; i < length; i++)
  139. {
  140. if(left_max_val < left_back_buff[i])
  141. {
  142. left_max_val = left_back_buff[i];
  143. left_max_index = i;
  144. }
  145. if(left_min_val > left_back_buff[i])
  146. {
  147. left_min_val = left_back_buff[i];
  148. left_min_index = i;
  149. }
  150. if(right_max_val < right_back_buff[i])
  151. {
  152. right_max_val = right_back_buff[i];
  153. right_max_index = i;
  154. }
  155. if(right_min_val > right_back_buff[i])
  156. {
  157. right_min_val = right_back_buff[i];
  158. right_min_index = i;
  159. }
  160. }
  161. if(left_max_index > left_min_index && left_max_val > left_min_val + 500.0f
  162. && right_max_index < right_min_index && right_max_val > right_min_val + 1000.0f)
  163. {
  164. return 1;
  165. }
  166. if(left_max_index < left_min_index && left_max_val > left_min_val + 1000.0f
  167. && right_max_index > right_min_index && right_max_val > right_min_val + 500.0f)
  168. {
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. //快速排序
  174. void quick_sork(float arr[], int start, int end)
  175. {
  176. if(start < end)
  177. {
  178. return;
  179. }
  180. int i = start;
  181. int j = end;
  182. float baseval = arr[start];
  183. while(i < j)
  184. {
  185. while(i < j && arr[j] >= baseval)
  186. {
  187. j--;
  188. }
  189. if(i < j)
  190. {
  191. arr[i] = arr[j];
  192. i++;
  193. }
  194. while(i < j && arr[i] < baseval)
  195. {
  196. i++;
  197. }
  198. if(i < j)
  199. {
  200. arr[j] = arr[i];
  201. j--;
  202. }
  203. }
  204. arr[i] = baseval;
  205. quick_sork(arr, start, i-1);
  206. quick_sork(arr, i+1, end);
  207. }
  208. float mid_val(float *mag_buff)
  209. {
  210. static float src[10];
  211. memcpy(src, mag_buff, 10 * sizeof(float));
  212. //快速排序
  213. quick_sork(mag_buff, 0 , 9);
  214. return 0.5f *(mag_buff[4] + mag_buff[5]);
  215. }
  216. int special_stay_by_back_mag_down(float *left_back_mag, float *right_back_mag, int left_zupt, int right_zupt)
  217. {
  218. static float last_left_back_mag_val = 0.0f;
  219. static float last_right_back_mag_val = 0.0f;
  220. static float max_left_back;
  221. static float min_left_back;
  222. static float max_right_back;
  223. static float min_right_back;
  224. float cur_left_back_mag_val = mid_val(left_back_mag);
  225. float cur_right_back_mag_val = mid_val(right_back_mag);
  226. if(last_left_back_mag_val < cur_left_back_mag_val - 0.00001f)
  227. {
  228. max_left_back = cur_left_back_mag_val;
  229. min_left_back = cur_left_back_mag_val;
  230. max_right_back = cur_right_back_mag_val;
  231. min_right_back = cur_right_back_mag_val;
  232. }
  233. else
  234. {
  235. min_left_back = cur_left_back_mag_val;
  236. }
  237. if(last_right_back_mag_val < cur_right_back_mag_val - 0.00001f)
  238. {
  239. max_left_back = cur_left_back_mag_val;
  240. min_left_back = cur_left_back_mag_val;
  241. max_right_back = cur_right_back_mag_val;
  242. min_right_back = cur_right_back_mag_val;
  243. }
  244. else
  245. {
  246. min_right_back = cur_right_back_mag_val;
  247. }
  248. last_left_back_mag_val = cur_left_back_mag_val;
  249. last_right_back_mag_val = cur_right_back_mag_val;
  250. if(max_left_back > min_left_back +2000.f && max_right_back > min_right_back +2000.f
  251. && left_zupt && right_zupt)
  252. {
  253. return 1;
  254. }
  255. else
  256. {
  257. return 0;
  258. }
  259. }
  260. int special_stay_by_long_time_down_process(float *front_mag_left_buff, float *front_mag_right_buff, float *back_mag_left_buff, float *back_mag_right_buff)
  261. {
  262. int left_index = 9;
  263. int right_index = 9;
  264. /*
  265. * 寻找第一个递增的点
  266. */
  267. for(left_index = 9; left_index > 0 ; left_index --)
  268. {
  269. if(front_mag_left_buff[left_index] < front_mag_left_buff[left_index - 1])
  270. {
  271. break;
  272. }
  273. }
  274. for(right_index = 9; right_index > 0 ; right_index --)
  275. {
  276. if(front_mag_right_buff[right_index] < front_mag_right_buff[right_index - 1])
  277. {
  278. break;
  279. }
  280. }
  281. float right_max_front_val = front_mag_right_buff[0];
  282. float right_min_front_val = front_mag_right_buff[0];
  283. for(int i = 0; i < 10; i++)
  284. {
  285. right_max_front_val = right_max_front_val > front_mag_right_buff[i] ? right_max_front_val : front_mag_right_buff[i];
  286. right_min_front_val = right_min_front_val < front_mag_right_buff[i] ? right_min_front_val : front_mag_right_buff[i];
  287. }
  288. /*
  289. * 有些特殊的点,左脚前掌往下压,但是右脚前掌没啥变化, 只靠后脚掌数据递降来判断
  290. */
  291. if(left_index < 6 && front_mag_left_buff[9] - front_mag_left_buff[left_index] > 1000.0f
  292. && right_max_front_val - right_min_front_val < 500.f
  293. && back_mag_right_buff[left_index] - back_mag_right_buff[9] > 1000.f)
  294. {
  295. return 1;
  296. }
  297. /*
  298. * 左脚前掌往下压,右脚前掌往下压
  299. */
  300. if(left_index < 6 && front_mag_left_buff[9] - front_mag_left_buff[left_index] > 1000.0f
  301. && right_index < 6 && front_mag_right_buff[9] - front_mag_right_buff[right_index] > 1000.0f)
  302. {
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. int special_stay_by_long_time_down(float front_mag_left, float front_mag_right, float back_mag_left, float back_mag_right)
  308. {
  309. static int time_count = 0;
  310. static float front_mag_left_buff[10];
  311. static float front_mag_right_buff[10];
  312. static float back_mag_left_buff[10];
  313. static float back_mag_right_buff[10];
  314. if(time_count == 8)
  315. {
  316. time_count = 0;
  317. }
  318. /*
  319. * 更新数据,8个数据为一组
  320. */
  321. int long_time_down = 0;
  322. if(time_count == 0)
  323. {
  324. memcpy(front_mag_left_buff, front_mag_left_buff + 1, 9 * sizeof(float));
  325. memcpy(front_mag_right_buff, front_mag_right_buff + 1, 9 * sizeof(float));
  326. memcpy(back_mag_left_buff, back_mag_left_buff + 1, 9 * sizeof(float));
  327. memcpy(back_mag_right_buff, back_mag_right_buff + 1, 9 * sizeof(float));
  328. front_mag_left_buff[9] = front_mag_left;
  329. front_mag_right_buff[9] = front_mag_right;
  330. back_mag_left_buff[9] = back_mag_left;
  331. back_mag_right_buff[9] = back_mag_right;
  332. long_time_down = special_stay_by_long_time_down_process(front_mag_left_buff, front_mag_right_buff, back_mag_left_buff, back_mag_right_buff);
  333. if(long_time_down == 0)
  334. {
  335. long_time_down = special_stay_by_long_time_down_process(front_mag_right_buff, front_mag_left_buff, back_mag_right_buff, back_mag_left_buff);
  336. }
  337. }
  338. time_count ++;
  339. return long_time_down;
  340. }
  341. int press_down_detect_new(int index, float front_mag_left, float back_mag_left,
  342. float front_mag_right, float back_mag_right,int left_zupt, int right_zupt,
  343. float left_acc_x,float left_acc_y, float left_acc_z, float right_acc_x, float right_acc_y, float right_acc_z,
  344. int *front_down, int *back_down)
  345. {
  346. static float front_mag_norm_left[PRESS_COUNT_MAX];
  347. static float front_mag_norm_right[PRESS_COUNT_MAX];
  348. static float front_mag_left_big_buff[15];
  349. static float front_mag_right_big_buff[15];
  350. static float back_mag_left_big_buff[10];
  351. static float back_mag_right_big_buff[10];
  352. static float back_mag_norm_left[PRESS_COUNT_MAX];
  353. static float back_mag_norm_right[PRESS_COUNT_MAX];
  354. static float acc_norm_left[10];
  355. static float acc_norm_right[10];
  356. static float left_front_up_min = 40000.0f;
  357. static float left_back_up_min = 40000.0f;
  358. static float right_front_up_min = 40000.0f;
  359. static float right_back_up_min = 40000.0f;
  360. memcpy(front_mag_norm_left, front_mag_norm_left + 1, 4 * sizeof(float));
  361. memcpy(front_mag_norm_right, front_mag_norm_right + 1, 4 * sizeof(float));
  362. memcpy(front_mag_left_big_buff, front_mag_left_big_buff + 1, 14 * sizeof(float));
  363. memcpy(front_mag_right_big_buff, front_mag_right_big_buff + 1, 14 * sizeof(float));
  364. memcpy(back_mag_norm_left, back_mag_norm_left + 1, 4 * sizeof(float));
  365. memcpy(back_mag_norm_right, back_mag_norm_right + 1, 4 * sizeof(float));
  366. memcpy(acc_norm_left, acc_norm_left + 1, 9 * sizeof(float));
  367. memcpy(acc_norm_right, acc_norm_right + 1, 9 * sizeof(float));
  368. memcpy(back_mag_left_big_buff, back_mag_left_big_buff + 1, 9 * sizeof(float));
  369. memcpy(back_mag_right_big_buff, back_mag_right_big_buff + 1, 9 * sizeof(float));
  370. front_mag_norm_left[PRESS_COUNT_MAX - 1] = front_mag_left;
  371. front_mag_norm_right[PRESS_COUNT_MAX - 1] = front_mag_right;
  372. front_mag_left_big_buff[14] = front_mag_left;
  373. front_mag_right_big_buff[14] = front_mag_right;
  374. back_mag_norm_left[PRESS_COUNT_MAX - 1] = back_mag_left;
  375. back_mag_norm_right[PRESS_COUNT_MAX - 1] = back_mag_right;
  376. acc_norm_left[9] = sqrt(left_acc_x * left_acc_x + left_acc_y * left_acc_y + left_acc_z * left_acc_z);
  377. acc_norm_right[9] = sqrt(right_acc_x * right_acc_x + right_acc_y * right_acc_y + right_acc_z * right_acc_z);
  378. back_mag_left_big_buff[9] = back_mag_left;
  379. back_mag_right_big_buff[9] = back_mag_right;
  380. detect_up_trend(&left_front_up_min, front_mag_norm_left, PRESS_COUNT_MAX);
  381. detect_up_trend(&right_front_up_min, front_mag_norm_right, PRESS_COUNT_MAX);
  382. detect_up_trend(&left_back_up_min, back_mag_norm_left, PRESS_COUNT_MAX);
  383. detect_up_trend(&right_back_up_min, back_mag_norm_right, PRESS_COUNT_MAX);
  384. if(left_zupt == 0)
  385. {
  386. left_front_up_min = front_mag_left;
  387. left_back_up_min = back_mag_left;
  388. }
  389. if(right_zupt == 0)
  390. {
  391. right_front_up_min = front_mag_right;
  392. right_back_up_min = back_mag_right;
  393. }
  394. //当检测到前脚掌的压力下降同时较快,将不会触发后脚掌下蹲的判断
  395. if(check_dual_front_press_down(front_mag_left_big_buff, front_mag_right_big_buff, 15))
  396. {
  397. left_back_up_min = back_mag_left;
  398. right_back_up_min = back_mag_right;
  399. }
  400. if(front_mag_left - left_front_up_min > 1000.0f && front_mag_right - right_front_up_min > 1000.0f )
  401. {
  402. *front_down = 1;
  403. }
  404. if(back_mag_different_trend(back_mag_left_big_buff, back_mag_right_big_buff, 10))
  405. {
  406. *front_down = 0;
  407. }
  408. if(back_mag_left - left_back_up_min > 2000.0f && back_mag_right - right_back_up_min > 2000.0f )
  409. {
  410. *back_down = 1;
  411. }
  412. if(!station_acc(acc_norm_left, 10) || !station_acc(acc_norm_right, 10))
  413. {
  414. *front_down = 0;
  415. *back_down = 0;
  416. }
  417. if(special_stay_by_long_time_down(front_mag_left, front_mag_right, back_mag_left, back_mag_right))
  418. {
  419. *front_down = 1;
  420. }
  421. return 0;
  422. }
  423. //short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  424. //{
  425. // static int last_h_z;
  426. // static int last_s_z;
  427. // static int left_up;
  428. // static int right_up;
  429. //
  430. // static int left_up_count;
  431. // static int right_up_count;
  432. //
  433. // if(h_pos[2] - last_h_z > 0 && h_pos[2]> 0)
  434. // {
  435. // left_up = 1;
  436. // }
  437. // else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  438. // {
  439. // left_up = -1;
  440. // }
  441. //
  442. // if(left_up == 1)
  443. // {
  444. // left_up_count ++;
  445. // }
  446. // else
  447. // {
  448. // left_up_count = 0;
  449. // }
  450. //
  451. // if(s_pos[2] - last_s_z > 0 && s_pos[2] > 0)
  452. // {
  453. // right_up = 1;
  454. // }
  455. // else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  456. // {
  457. // right_up = -1;
  458. // }
  459. //
  460. // if(right_up == 1)
  461. // {
  462. // right_up_count ++;
  463. // }
  464. // else
  465. // {
  466. // right_up_count = 0;
  467. // }
  468. //
  469. // last_h_z = h_pos[2];
  470. // last_s_z = s_pos[2];
  471. //
  472. // if(left_up == 1 && right_up == 1 && right_up_count < 15 && left_up_count < 20
  473. // && right_up_count > 2 && left_up_count > 2)
  474. // {
  475. // return 1;
  476. // }
  477. //
  478. // return 0;
  479. //}
  480. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  481. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  482. {
  483. *max_val = data[0];
  484. *max_index = 0;
  485. *min_val = data[0];
  486. *min_index = 0;
  487. for(int i = 0; i < length; i++)
  488. {
  489. if(*max_val < data[i])
  490. {
  491. *max_val = data[i];
  492. *max_index = i;
  493. }
  494. if(*min_val > data[i])
  495. {
  496. *min_val = data[i];
  497. *min_index = i;
  498. }
  499. }
  500. }
  501. //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)
  502. //{
  503. //
  504. // static float right_data_z_buff[10];
  505. // static float left_data_z_buff[10];
  506. //
  507. // static int last_right_press;
  508. // static int last_left_press;
  509. // static int left_max_press;
  510. // static int right_max_press;
  511. //
  512. // static int wait;
  513. //
  514. // /*
  515. // * 存储数据
  516. // */
  517. // memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  518. //
  519. // memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  520. //
  521. // right_data_z_buff[9] = right_acc_z;
  522. // left_data_z_buff[9] = left_acc_z;
  523. //
  524. // if(left_zupt && right_zupt)
  525. // {
  526. // wait = 30;
  527. // }
  528. //
  529. // /*
  530. // * 检测压力下降用的逻辑
  531. // */
  532. // if(last_left_press - left_front_press > 200)
  533. // {
  534. // if(last_left_press > left_max_press)
  535. // {
  536. // left_max_press = last_left_press;
  537. // }
  538. // }
  539. // else if(last_left_press - left_front_press < -200)
  540. // {
  541. // left_max_press = 0;
  542. // }
  543. //
  544. // last_left_press = left_front_press;
  545. //
  546. // if(last_right_press - right_front_press > 200)
  547. // {
  548. // if(last_right_press > right_max_press)
  549. // {
  550. // right_max_press = last_right_press;
  551. // }
  552. // }
  553. // else if(last_right_press - right_front_press < -200)
  554. // {
  555. // right_max_press = 0;
  556. // }
  557. //
  558. // last_right_press = right_front_press;
  559. //
  560. // /*
  561. // * 加速变化估计
  562. // */
  563. //
  564. // float max_val_right, min_val_right, max_val_left, min_val_left;
  565. //
  566. // int max_index_right, min_index_right, max_index_left, min_index_left;
  567. //
  568. //
  569. // max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  570. //
  571. // max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  572. //
  573. // if(wait > 0)
  574. // {
  575. // wait --;
  576. // }
  577. //
  578. // /*
  579. // * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  580. // */
  581. // if( max_index_right < min_index_right && max_index_left < min_index_left &&
  582. //
  583. // max_val_right - min_val_right > 1.3f && max_val_left - min_val_left > 1.3f && wait > 0 && left_zupt == 0 && right_zupt == 0
  584. //
  585. // && left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000 )
  586. // //if(left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000)
  587. // {
  588. // SEGGER_RTT_printf(0, "test\n");
  589. // 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);
  590. // return 1;
  591. // }
  592. //
  593. // return 0;
  594. //
  595. //}
  596. int little_jump_detect_process(int press, int *press_max, int*press_min, int *last_press)
  597. {
  598. if(*last_press - press > 200)
  599. {
  600. if(*press_max < press)
  601. {
  602. *press_max = press;
  603. }
  604. if(*press_min > press)
  605. {
  606. *press_min = press;
  607. }
  608. }
  609. else if(*last_press - press < -200)
  610. {
  611. if(*press_max > press)
  612. {
  613. *press_max = press;
  614. }
  615. if(*press_min < press)
  616. {
  617. *press_min = press;
  618. }
  619. }
  620. *last_press = press;
  621. if(*press_max - press > 3000)
  622. {
  623. return 1;
  624. }
  625. if(*press_max - press < -1000)
  626. {
  627. return -1;
  628. }
  629. return 0;
  630. }
  631. int little_jump_detect( int left_front_press, int right_front_press, int left_back_press, int right_back_press, int left_zupt, int right_zupt)
  632. {
  633. static int left_front_max = 0;
  634. static int right_front_max = 0;
  635. static int left_back_max = 0;
  636. static int right_back_max = 0;
  637. static int left_front_min = 40000;
  638. static int right_front_min = 40000;
  639. static int left_back_min = 40000;
  640. static int right_back_min = 40000;
  641. static int last_left_front_press = 0;
  642. static int last_right_front_press = 0;
  643. static int last_left_back_press = 0;
  644. static int last_right_back_press = 0;
  645. int left_front_down = little_jump_detect_process(left_front_press, &left_front_max, &left_front_min, &last_left_front_press);
  646. int right_front_down = little_jump_detect_process(right_front_press, &right_front_max, &right_front_min, &last_right_front_press);
  647. int left_back_down = little_jump_detect_process(left_back_press, &left_back_max, &left_back_min, &last_left_back_press);
  648. int right_back_down = little_jump_detect_process(right_back_press, &right_back_max, &right_back_min, &last_right_back_press);
  649. // if( left_back_down == -1)
  650. // {
  651. // left_front_max = left_front_press;
  652. //
  653. // left_front_min = left_front_press;
  654. // }
  655. //
  656. // if( right_back_down == -1)
  657. // {
  658. // right_front_max = right_front_press;
  659. //
  660. // right_front_min = right_front_press;
  661. // }
  662. if(left_front_down != 1)
  663. {
  664. right_front_max = right_front_press;
  665. right_front_min = right_front_press;
  666. }
  667. if(right_front_down != 1)
  668. {
  669. left_front_max = left_front_press;
  670. left_front_min = left_front_press;
  671. }
  672. if(left_front_down==1 && right_front_down==1 && left_zupt==0 && right_zupt==0)
  673. {
  674. return 1;
  675. }
  676. if(left_back_down==1 && right_back_down==1 && left_zupt==0 && right_zupt==0)
  677. {
  678. return 1;
  679. }
  680. return 0;
  681. }
  682. int detect_L_line(int32_t *press_buff, int length)
  683. {
  684. int max_val = press_buff[0];
  685. int max_index = 0;
  686. for(int i = 1; i < length; i++)
  687. {
  688. if(press_buff[i] > max_val)
  689. {
  690. max_val = press_buff[i];
  691. max_index = i;
  692. }
  693. }
  694. /*
  695. * 拐点太接近数组末端,舍弃掉
  696. */
  697. // if(max_index >= length - 5)
  698. // {
  699. // return 0;
  700. // }
  701. int last_val = press_buff[max_index];
  702. int turn_point_index = max_index;
  703. for(int i = max_index + 1; i < length; i++)
  704. {
  705. if(last_val >= press_buff[i])
  706. {
  707. last_val = press_buff[i];
  708. turn_point_index = i;
  709. }
  710. else
  711. {
  712. break;
  713. }
  714. }
  715. /*
  716. * 检测到临时的拐点, 需要判断拐点倒数第五个之前
  717. */
  718. last_val = press_buff[length - 4];
  719. int stable_max_val = press_buff[length - 4];
  720. int stable_min_val = press_buff[length - 4];
  721. for(int i= length - 4; i< length; i++)
  722. {
  723. if(stable_max_val < press_buff[i])
  724. {
  725. stable_max_val = press_buff[i];
  726. }
  727. if(stable_min_val > press_buff[i])
  728. {
  729. stable_min_val = press_buff[i];
  730. }
  731. }
  732. if(stable_max_val - stable_min_val < 200 && press_buff[max_index] - press_buff[turn_point_index] > 4000
  733. && turn_point_index > length - 5)
  734. {
  735. return 1;
  736. }
  737. return 0;
  738. }
  739. int detect_I_line(int32_t *press_buff, int length)
  740. {
  741. int max_val = press_buff[0];
  742. int max_index = 0;
  743. for(int i = 1; i < length; i++)
  744. {
  745. if(press_buff[i] >= max_val)
  746. {
  747. max_val = press_buff[i];
  748. max_index = i;
  749. }
  750. }
  751. /*
  752. * 拐点太接近数组末端,舍弃掉
  753. */
  754. // if(max_index >= length - 5)
  755. // {
  756. // return 0;
  757. // }
  758. int last_val = press_buff[max_index];
  759. int turn_point_index = max_index;
  760. for(int i = max_index + 1; i < length; i++)
  761. {
  762. if(last_val >= press_buff[i])
  763. {
  764. last_val = press_buff[i];
  765. turn_point_index = i;
  766. }
  767. else
  768. {
  769. break;
  770. }
  771. }
  772. if(press_buff[max_index] - press_buff[turn_point_index] > 4000 && abs(press_buff[turn_point_index] - press_buff[length -1]) < 500)
  773. {
  774. return 1;
  775. }
  776. return 0;
  777. }
  778. int little_up_jump_by_four_mag(int32_t *right_back_data_press, int32_t *left_back_data_press, int32_t *right_front_data_press, int32_t *left_front_data_press, int length)
  779. {
  780. int left_front_mag_max = left_front_data_press[0];
  781. int right_front_mag_max = right_front_data_press[0];
  782. int left_front_mag_min = left_front_data_press[0];
  783. int right_front_mag_min = right_front_data_press[0];
  784. int left_back_mag_max = left_back_data_press[0];
  785. int right_back_mag_max = right_back_data_press[0];
  786. int left_back_mag_min = left_back_data_press[0];
  787. int right_back_mag_min = right_back_data_press[0];
  788. int left_front_mag_max_index = 0; int right_front_mag_max_index = 0;
  789. int left_front_mag_min_index = 0; int right_front_mag_min_index = 0;
  790. int left_back_mag_max_index = 0; int right_back_mag_max_index = 0;
  791. int left_back_mag_min_index = 0; int right_back_mag_min_index = 0;
  792. for(int i = 1; i < length; i++)
  793. {
  794. if(left_front_mag_max < left_front_data_press[i])
  795. {
  796. left_front_mag_max = left_front_data_press[i];
  797. left_front_mag_max_index = i;
  798. }
  799. if(right_front_mag_max < right_front_data_press[i])
  800. {
  801. right_front_mag_max = right_front_data_press[i];
  802. right_front_mag_max_index = i;
  803. }
  804. if(left_front_mag_min > left_front_data_press[i])
  805. {
  806. left_front_mag_min = left_front_data_press[i];
  807. left_front_mag_min_index = i;
  808. }
  809. if(right_front_mag_min > right_front_data_press[i])
  810. {
  811. right_front_mag_min = right_front_data_press[i];
  812. right_front_mag_min_index = i;
  813. }
  814. if(left_back_mag_max < left_back_data_press[i])
  815. {
  816. left_back_mag_max = left_back_data_press[i];
  817. left_back_mag_max_index = i;
  818. }
  819. if(right_back_mag_max < right_back_data_press[i])
  820. {
  821. right_back_mag_max = right_back_data_press[i];
  822. right_back_mag_max_index = i;
  823. }
  824. if(left_back_mag_min > left_back_data_press[i])
  825. {
  826. left_back_mag_min = left_back_data_press[i];
  827. left_back_mag_min_index = i;
  828. }
  829. if(right_back_mag_min > right_back_data_press[i])
  830. {
  831. right_back_mag_min = right_back_data_press[i];
  832. right_back_mag_min_index = i;
  833. }
  834. }
  835. if(right_back_mag_min_index > right_back_mag_max_index && right_front_mag_min_index > right_front_mag_max_index
  836. && left_back_mag_min_index > left_back_mag_max_index && left_front_mag_min_index > left_front_mag_max_index
  837. && right_front_mag_max > right_front_mag_min + 2000 && right_back_mag_max > right_back_mag_min + 1000
  838. && left_front_mag_max > left_front_mag_min + 2000 && left_back_mag_max > left_back_mag_min + 1000)
  839. {
  840. return 1;
  841. }
  842. return 0;
  843. }
  844. int acc_z_down_trend(float *acc_buff, int length)
  845. {
  846. int max_index = 0;
  847. int min_index = 0;
  848. float max_val = acc_buff[0];
  849. float min_val = acc_buff[0];
  850. for(int i = 0; i < length; i++)
  851. {
  852. if(max_val < acc_buff[i])
  853. {
  854. max_val = acc_buff[i];
  855. max_index = i;
  856. }
  857. if(min_val > acc_buff[i])
  858. {
  859. min_val = acc_buff[i];
  860. min_index = i;
  861. }
  862. }
  863. if(max_index < min_index && max_val > min_val + 4.0f && max_val > 4.0f && min_val < 0.f)
  864. {
  865. return 1;
  866. }
  867. return 0;
  868. }
  869. int normal_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int32_t left_front_press, int32_t right_front_press, int32_t left_back_press, int32_t right_back_press)
  870. {
  871. static int32_t right_data_press[15];
  872. static int32_t left_data_press[15];
  873. static int32_t right_front_data_press[15];
  874. static int32_t left_front_data_press[15];
  875. static float left_acc_z_buff[10];
  876. static float right_acc_z_buff[10];
  877. static int left_wait;
  878. static int right_wait;
  879. static int wait;
  880. static int last_left_zupt;
  881. static int last_right_zupt;
  882. /*
  883. * 存储数据
  884. */
  885. memcpy(right_data_press, right_data_press + 1, 14 * sizeof(int32_t));
  886. memcpy(left_data_press, left_data_press + 1, 14 * sizeof(int32_t));
  887. memcpy(right_front_data_press, right_front_data_press + 1, 14 * sizeof(int32_t));
  888. memcpy(left_front_data_press, left_front_data_press + 1, 14 * sizeof(int32_t));
  889. memcpy(left_acc_z_buff, left_acc_z_buff + 1, 9 * sizeof(float));
  890. memcpy(right_acc_z_buff, right_acc_z_buff + 1, 9 * sizeof(float));
  891. right_data_press[14] = right_back_press; left_data_press[14] = left_back_press;
  892. right_front_data_press[14] = right_front_press; left_front_data_press[14] = left_front_press;
  893. left_acc_z_buff[9] = left_acc_z; right_acc_z_buff[9] = right_acc_z;
  894. // if(left_zupt == 0 && last_left_zupt == 1)
  895. // {
  896. // left_wait = 10;
  897. // }
  898. //
  899. // if(right_zupt == 0 && last_right_zupt == 1)
  900. // {
  901. // right_wait = 10;
  902. // }
  903. //
  904. // if(left_wait > 0)
  905. // {
  906. // left_wait --;
  907. // }
  908. //
  909. // if(right_wait > 0)
  910. // {
  911. // right_wait --;
  912. // }
  913. if(left_zupt == 1 && right_zupt == 1)
  914. {
  915. wait = 13;
  916. }
  917. if(wait > 0)
  918. {
  919. wait --;
  920. }
  921. last_left_zupt = left_zupt;
  922. last_right_zupt = right_zupt;
  923. if(detect_L_line(right_data_press, 15) && detect_L_line(left_data_press, 15) && wait > 0 && wait < 12)
  924. {
  925. return 1;
  926. }
  927. if(detect_I_line(right_front_data_press, 15) && detect_I_line(left_front_data_press, 15) && wait > 0 && wait < 12)
  928. {
  929. return 1;
  930. }
  931. if(little_up_jump_by_four_mag(right_data_press+7, left_data_press+7, right_front_data_press+7, left_front_data_press+7, 8) && wait > 0 && wait < 12)
  932. {
  933. return 1;
  934. }
  935. // if(acc_z_down_trend(left_acc_z_buff, 10) && acc_z_down_trend(right_acc_z_buff, 10) )
  936. // {
  937. // return 1;
  938. // }
  939. return 0;
  940. }
  941. short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int32_t left_front_press, int32_t right_front_press, int32_t left_back_press, int32_t right_back_press)
  942. {
  943. // return little_jump_detect( left_front_press, right_front_press, left_back_press, right_back_press, left_zupt, right_zupt) ||
  944. // normal_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, left_front_press, right_front_press, left_back_press, right_back_press);
  945. return normal_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, left_front_press, right_front_press, left_back_press, right_back_press);
  946. }