detect_zero_vel.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. //#include "detect_zero_vel.h"
  2. //#include "hal_mt.h"
  3. //#include "hal_flash.h"
  4. //#define MAG_THRESHHOLD 1000.f
  5. //int front_zero_tmp = 0;
  6. //int back_zero_tmp = 0;
  7. //void start_cal_step(int16_t front[3], int16_t back[3], int16_t acc[3])
  8. //{
  9. // int16_t front_zero = 0;
  10. // int16_t back_zero = 0;
  11. // int16_t acc_zero = 0;
  12. //
  13. // detect_zero_vel( front, back, acc, &front_zero, &back_zero, &acc_zero);
  14. //}
  15. //void cal_step(int16_t front_zero, int16_t back_zero)
  16. //{
  17. // static int step_calm_down;
  18. //
  19. // if(front_zero || back_zero)
  20. // {
  21. // if(step_calm_down == 0)
  22. // {
  23. // mFlash.mStep.stepCur[0] ++;
  24. // }
  25. // step_calm_down = 10;
  26. // }
  27. //
  28. // if(step_calm_down > 0)
  29. // {
  30. // step_calm_down --;
  31. // }
  32. //}
  33. // void detect_zero_vel(int16_t front[3], int16_t back[3], int16_t acc[3],
  34. // int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  35. //{
  36. // static float front_mag_window[WINDOW_SIZE];
  37. //
  38. // static float back_mag_window[WINDOW_SIZE];
  39. //
  40. // static float acc_x_window[WINDOW_SIZE];
  41. // static float acc_y_window[WINDOW_SIZE];
  42. // static float acc_z_window[WINDOW_SIZE];
  43. //
  44. // static int last_front_zupt;
  45. //
  46. // static int last_back_zupt;
  47. //
  48. // static int front_zupt_wait;
  49. //
  50. // static int back_zupt_wait;
  51. //
  52. //
  53. // float front_val = sqrt((float) (front[0] * front[0] + front[1] * front[1] + front[2] * front[2]));
  54. //
  55. // float back_val = sqrt((float) (back[0] * back[0] + back[1] * back[1] + back[2] * back[2]));
  56. //
  57. //
  58. // //滑动窗口更新数据
  59. //
  60. // memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  61. //
  62. // memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  63. //
  64. // memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  65. //
  66. // memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  67. //
  68. // memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  69. //
  70. // front_mag_window[WINDOW_SIZE - 1] = front_val;
  71. //
  72. // back_mag_window[WINDOW_SIZE - 1] = back_val;
  73. //
  74. // acc_x_window[WINDOW_SIZE - 1] = acc[0] / 2048.f;
  75. // acc_y_window[WINDOW_SIZE - 1] = acc[1] / 2048.f;
  76. // acc_z_window[WINDOW_SIZE - 1] = acc[2] / 2048.f;
  77. // float front_max_val = front_val;
  78. // float front_min_val = front_val;
  79. //
  80. // uint16_t front_max_index = WINDOW_SIZE - 1;
  81. // uint16_t front_min_index = WINDOW_SIZE - 1;
  82. //
  83. // float back_max_val = back_val;
  84. // float back_min_val = back_val;
  85. //
  86. // uint16_t back_max_index = WINDOW_SIZE - 1;
  87. // uint16_t back_min_index = WINDOW_SIZE - 1;
  88. //
  89. // for(int i = 0 ; i < WINDOW_SIZE; i++ )
  90. // {
  91. // if(front_mag_window[i] > front_max_val)
  92. // {
  93. // front_max_val = front_mag_window[i];
  94. // front_max_index = i;
  95. // }
  96. //
  97. // if(front_mag_window[i] < front_min_val)
  98. // {
  99. // front_min_val = front_mag_window[i];
  100. // front_min_index = i;
  101. // }
  102. //
  103. // if(back_mag_window[i] > back_max_val)
  104. // {
  105. // back_max_val = back_mag_window[i];
  106. // back_max_index = i;
  107. // }
  108. //
  109. // if(back_mag_window[i] < back_min_val)
  110. // {
  111. // back_min_val = back_mag_window[i];
  112. // back_min_index = i;
  113. // }
  114. //
  115. // }
  116. //
  117. // /*
  118. // * 计算稳定的状态
  119. // */
  120. //
  121. // float acc_max_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  122. // float acc_min_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  123. //
  124. // float acc_max_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  125. // float acc_min_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  126. //
  127. // float acc_max_val_z = acc_z_window[WINDOW_SIZE - 1];
  128. // float acc_min_val_z = acc_z_window[WINDOW_SIZE - 1];
  129. //
  130. // for(int i = WINDOW_SIZE - 5 ; i < WINDOW_SIZE; i++ )
  131. // {
  132. // if(acc_x_window[i] < acc_min_val_x)
  133. // {
  134. // acc_min_val_x = acc_x_window[i];
  135. // }
  136. //
  137. // if(acc_x_window[i] > acc_max_val_x)
  138. // {
  139. // acc_max_val_x = acc_x_window[i];
  140. // }
  141. //
  142. // if(acc_y_window[i] < acc_min_val_y)
  143. // {
  144. // acc_min_val_y = acc_y_window[i];
  145. // }
  146. //
  147. // if(acc_y_window[i] > acc_max_val_y)
  148. // {
  149. // acc_max_val_y = acc_y_window[i];
  150. // }
  151. //
  152. // if(acc_z_window[i] < acc_min_val_z)
  153. // {
  154. // acc_min_val_z = acc_z_window[i];
  155. // }
  156. //
  157. // if(acc_z_window[i] > acc_max_val_z)
  158. // {
  159. // acc_max_val_z = acc_z_window[i];
  160. // }
  161. //
  162. // }
  163. //
  164. // if(front_max_index > front_min_index && front_max_val > front_min_val + MAG_THRESHHOLD)
  165. // {
  166. // front_zero_tmp = 1;
  167. //
  168. // front_zupt_wait = 10;
  169. // }
  170. // else if(front_max_index < front_min_index && front_max_val > front_min_val + MAG_THRESHHOLD)
  171. // {
  172. // front_zero_tmp = 0;
  173. // }
  174. // else if(front_zero_tmp == 1)
  175. // {
  176. // front_zero_tmp = 2;
  177. // }
  178. //
  179. //
  180. // /*
  181. // * 判断后脚往下压
  182. // */
  183. //
  184. // if(back_max_index > back_min_index && back_max_val > back_min_val + MAG_THRESHHOLD)
  185. // {
  186. // back_zero_tmp = 1;
  187. //
  188. // back_zupt_wait = 10;
  189. // }
  190. //
  191. // else if(back_max_index < back_min_index && back_max_val > back_min_val + MAG_THRESHHOLD)
  192. // {
  193. // back_zero_tmp = 0;
  194. // }
  195. // else if(back_zero_tmp == 1)
  196. // {
  197. // back_zero_tmp = 2;
  198. // }
  199. //
  200. // /*
  201. // * 判断仅前踮 或 后垫
  202. // */
  203. // if(back_zero_tmp == 2 && front_zero_tmp == 0)
  204. // {
  205. // back_zero_tmp = 0;
  206. // }
  207. //
  208. // if(front_zero_tmp == 2 && back_zero_tmp == 0)
  209. // {
  210. // front_zero_tmp = 0;
  211. // }
  212. //
  213. // //延迟等待稳定状态
  214. //// if(*front_zero == 0 && front_zupt_wait > 0 && acc_max_val - acc_min_val < 0.08f)
  215. //// {
  216. //// *front_zero = 1;
  217. //// }
  218. //
  219. // if(front_zero_tmp == 1)
  220. // {
  221. // *front_zero = 1;
  222. //
  223. // }
  224. // else if(front_zupt_wait > 0 && acc_max_val_x - acc_min_val_x < 0.07f && acc_max_val_y - acc_min_val_y < 0.07f && acc_max_val_z - acc_min_val_z < 0.07f)
  225. // {
  226. // *front_zero = 1;
  227. // front_zupt_wait = 20;
  228. // }
  229. // else
  230. // {
  231. // *front_zero = 0;
  232. // }
  233. //
  234. //
  235. //// if(*back_zero == 0 && back_zupt_wait > 0 && acc_max_val - acc_min_val < 0.08f)
  236. //// {
  237. //// *back_zero = 1;
  238. //// }
  239. //
  240. // if(back_zero_tmp == 1)
  241. // {
  242. // *back_zero = 1;
  243. // }
  244. // else if(back_zupt_wait > 0 && acc_max_val_x - acc_min_val_x < 0.07f && acc_max_val_y - acc_min_val_y < 0.07f && acc_max_val_z - acc_min_val_z < 0.07f )
  245. // {
  246. // *back_zero = 1;
  247. //
  248. // back_zupt_wait = 20;
  249. // }
  250. // else
  251. // {
  252. // *back_zero = 0;
  253. // }
  254. //
  255. //
  256. // for(int i = 0 ; i < WINDOW_SIZE - 5; i++ )
  257. // {
  258. // if(acc_x_window[i] < acc_min_val_x)
  259. // {
  260. // acc_min_val_x = acc_x_window[i];
  261. // }
  262. //
  263. // if(acc_x_window[i] > acc_max_val_x)
  264. // {
  265. // acc_max_val_x = acc_x_window[i];
  266. // }
  267. //
  268. // if(acc_y_window[i] < acc_min_val_y)
  269. // {
  270. // acc_min_val_y = acc_y_window[i];
  271. // }
  272. //
  273. // if(acc_y_window[i] > acc_max_val_y)
  274. // {
  275. // acc_max_val_y = acc_y_window[i];
  276. // }
  277. //
  278. // if(acc_z_window[i] < acc_min_val_z)
  279. // {
  280. // acc_min_val_z = acc_z_window[i];
  281. // }
  282. //
  283. // if(acc_z_window[i] > acc_max_val_z)
  284. // {
  285. // acc_max_val_z = acc_z_window[i];
  286. // }
  287. //
  288. // }
  289. //
  290. //
  291. // if(acc_max_val_x - acc_min_val_x < 0.05f && acc_max_val_y - acc_min_val_y < 0.05f && acc_max_val_z - acc_min_val_z < 0.05f)
  292. // {
  293. // *acc_zero = 1;
  294. // }
  295. // else
  296. // {
  297. // *acc_zero = 0;
  298. // }
  299. //
  300. //
  301. // if(*front_zero == 0)
  302. // {
  303. // *front_zero = *back_zero;
  304. // }
  305. //
  306. // if(front_zupt_wait > 0)
  307. // {
  308. // front_zupt_wait --;
  309. // }
  310. //
  311. // if(back_zupt_wait > 0)
  312. // {
  313. // back_zupt_wait --;
  314. // }
  315. //
  316. //
  317. // //利用加速度延续
  318. // if((last_front_zupt == 1|| last_back_zupt == 1)
  319. // && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.05f
  320. // && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.05f
  321. // && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.05f)
  322. // {
  323. // *front_zero = 1;
  324. // *back_zero = 1;
  325. //
  326. // front_zero_tmp = 1;
  327. // back_zero_tmp = 1;
  328. //
  329. // front_zupt_wait = 15;
  330. // back_zupt_wait = 15;
  331. // }
  332. //
  333. //
  334. // last_front_zupt = front_zero_tmp;
  335. //
  336. // last_back_zupt = back_zero_tmp;
  337. //
  338. // /*
  339. // * 直接用这个来记步好了
  340. // */
  341. // cal_step(*front_zero, *back_zero );
  342. //}
  343. #include "detect_zero_vel.h"
  344. #include "hal_mt.h"
  345. #include "app_flash.h"
  346. #define MAG_THRESHHOLD 2000.f
  347. int front_zero_tmp = 0;
  348. int back_zero_tmp = 0;
  349. void start_cal_step(int16_t front[3], int16_t back[3], int16_t acc[3])
  350. {
  351. int16_t front_zero = 0;
  352. int16_t back_zero = 0;
  353. int16_t acc_zero = 0;
  354. SEGGER_RTT_printf(0,"detect_zero_vel( front, back, acc, &front_zero, &back_zero, &acc_zero); \n");
  355. detect_zero_vel( front, back, acc, &front_zero, &back_zero, &acc_zero);
  356. }
  357. void cal_step(int16_t front_zero, int16_t back_zero, float acc_x, float acc_y, float acc_z)
  358. {
  359. static int step_calm_down;
  360. static float acc_min[3];
  361. static float acc_max[3];
  362. if(!(front_zero || back_zero))
  363. {
  364. acc_max[2] = 1.0f;
  365. acc_min[2] = 1.0f;
  366. if(acc_x > acc_max[0])
  367. {
  368. acc_max[0] = acc_x;
  369. }
  370. if(acc_y > acc_max[1])
  371. {
  372. acc_max[1] = acc_y;
  373. }
  374. if(acc_z > acc_max[2])
  375. {
  376. acc_max[2] = acc_z;
  377. }
  378. if(acc_x < acc_min[0])
  379. {
  380. acc_min[0] = acc_x;
  381. }
  382. if(acc_y < acc_min[1])
  383. {
  384. acc_min[1] = acc_y;
  385. }
  386. if(acc_z < acc_min[2])
  387. {
  388. acc_min[2] = acc_z;
  389. }
  390. }
  391. if(front_zero || back_zero)
  392. {
  393. if(step_calm_down == 0 && (acc_max[0] - acc_min[0] > 0.8f || acc_max[1] - acc_min[1] > 0.8f || acc_max[2] - acc_min[2] > 0.8f))
  394. {
  395. mFlash.mStep.stepCur[0] ++;
  396. }
  397. //假设20ms相见为极限
  398. step_calm_down = 5;
  399. acc_max[0] = acc_x;
  400. acc_max[1] = acc_y;
  401. acc_max[2] = acc_z;
  402. acc_min[0] = acc_x;
  403. acc_min[1] = acc_y;
  404. acc_min[2] = acc_z;
  405. }
  406. if(step_calm_down > 0)
  407. {
  408. step_calm_down --;
  409. }
  410. }
  411. void setLongTimeUpTrend(int max_index, int min_index, float max_val, float min_val, float *longTime_maxVal, float *longTime_minVal)
  412. {
  413. if(max_index > min_index && max_val > min_val + 800.f)
  414. {
  415. *longTime_maxVal = *longTime_maxVal > max_val ? *longTime_maxVal : max_val;
  416. *longTime_minVal = *longTime_minVal < min_val ? *longTime_minVal : min_val;
  417. }
  418. else if(max_index < min_index && max_val > min_val + 800.f)
  419. {
  420. *longTime_maxVal = 0.0f;
  421. *longTime_minVal = 50000.0f;
  422. }
  423. }
  424. void setLongTimeDownTrend(int max_index, int min_index, float max_val, float min_val, float *longTime_maxVal, float *longTime_minVal)
  425. {
  426. if(max_index > min_index && max_val > min_val + 800.f)
  427. {
  428. *longTime_maxVal = 0.0f;
  429. *longTime_minVal = 50000.0f;
  430. }
  431. else if(max_index < min_index && max_val > min_val + 800.f)
  432. {
  433. *longTime_maxVal = *longTime_maxVal > max_val ? *longTime_maxVal : max_val;
  434. *longTime_minVal = *longTime_minVal < min_val ? *longTime_minVal : min_val;
  435. }
  436. }
  437. int press_down_front(float *press_buff)
  438. {
  439. static float max_val = 0.0f;
  440. if(press_buff[0] - press_buff[4] > 100.0f)
  441. {
  442. max_val = press_buff[0] > max_val ? press_buff[0] : max_val;
  443. }
  444. else
  445. {
  446. max_val = 0;
  447. }
  448. if(max_val > press_buff[4] + 2000.0f)
  449. {
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. int press_down_back(float *press_buff)
  455. {
  456. static float max_val = 0.0f;
  457. if(press_buff[0] - press_buff[4] > 100.0f)
  458. {
  459. max_val = press_buff[0] > max_val ? press_buff[0] : max_val;
  460. }
  461. else
  462. {
  463. max_val = 0;
  464. }
  465. if(max_val > press_buff[4] + 2000.0f)
  466. {
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. //2020/01/02
  472. //长趋势判断压力上升
  473. int isLongTimeUpTrend(float *mag_window, int length, float up_threshhold, float *min_val)
  474. {
  475. float prev_window_sum = (mag_window[0] + mag_window[1] + mag_window[2]) * 0.333f;
  476. float back_window_sum = (mag_window[length - 1] + mag_window[length - 2] + mag_window[length - 3]) * 0.333f;
  477. float back_window_sum1 = (mag_window[length - 4] + mag_window[length - 5] + mag_window[length - 6]) * 0.333f;
  478. float back_window_sum2 = (mag_window[length - 7] + mag_window[length - 8] + mag_window[length - 9]) * 0.333f;
  479. if(back_window_sum > prev_window_sum + 50.f || back_window_sum1 > prev_window_sum + 50.f || back_window_sum2 > prev_window_sum + 50.f )
  480. {
  481. //触发判断, 先替换掉最小值
  482. float min_val_temp = mag_window[0];
  483. min_val_temp = min_val_temp < mag_window[1] ? min_val_temp : mag_window[1];
  484. min_val_temp = min_val_temp < mag_window[2] ? min_val_temp : mag_window[2];
  485. *min_val = min_val_temp < *min_val ? min_val_temp : *min_val;
  486. float max_val_temp = mag_window[length - 6];
  487. for(int i = length - 5; i < length; i++)
  488. {
  489. if(max_val_temp < mag_window[i])
  490. {
  491. max_val_temp = mag_window[i];
  492. }
  493. }
  494. if(max_val_temp > *min_val + 2000.0f)
  495. {
  496. return 1;
  497. }
  498. }
  499. else
  500. {
  501. *min_val = mag_window[length - 1];
  502. }
  503. //上下沿判断
  504. int max_index = 0; int min_index = 0;
  505. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  506. for(int i = 1; i < length; i++)
  507. {
  508. if(window_max_val < mag_window[i])
  509. {
  510. window_max_val = mag_window[i];
  511. max_index = i;
  512. }
  513. if(window_min_val > mag_window[i])
  514. {
  515. window_min_val = mag_window[i];
  516. min_index = i;
  517. }
  518. }
  519. if((max_index > min_index && window_max_val - window_min_val > 2000.0f) || (mag_window[length - 1] - window_min_val > 2000.0f))
  520. {
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. int isLongTimeDownTrend(float *mag_window, int length, float up_threshhold, float *max_val)
  526. {
  527. float prev_window_sum = (mag_window[0] + mag_window[1] + mag_window[2]) * 0.333f;
  528. float back_window_sum = (mag_window[length - 1] + mag_window[length - 2] + mag_window[length - 3]) * 0.333f;
  529. float back_window_sum1 = (mag_window[length - 4] + mag_window[length - 5] + mag_window[length - 6]) * 0.333f;
  530. float back_window_sum2 = (mag_window[length - 7] + mag_window[length - 8] + mag_window[length - 9]) * 0.333f;
  531. if(prev_window_sum > back_window_sum + 50.f || prev_window_sum > back_window_sum1 + 50.f || prev_window_sum > back_window_sum2 + 50.f )
  532. {
  533. float max_val_temp = mag_window[0];
  534. max_val_temp = max_val_temp > mag_window[1] ? max_val_temp : mag_window[1];
  535. max_val_temp = max_val_temp > mag_window[2] ? max_val_temp : mag_window[2];
  536. *max_val = *max_val > max_val_temp ? *max_val : max_val_temp;
  537. float min_val_temp = mag_window[length - 6];
  538. for(int i = length - 5; i < length; i++)
  539. {
  540. if(min_val_temp > mag_window[i])
  541. {
  542. min_val_temp = mag_window[i];
  543. }
  544. }
  545. if(*max_val > min_val_temp + 2000.0f)
  546. {
  547. return 1;
  548. }
  549. }
  550. else
  551. {
  552. *max_val = mag_window[length - 1];
  553. }
  554. //上下沿判断
  555. int max_index = 0; int min_index = 0;
  556. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  557. for(int i = 1; i < length; i++)
  558. {
  559. if(window_max_val < mag_window[i])
  560. {
  561. window_max_val = mag_window[i];
  562. max_index = i;
  563. }
  564. if(window_min_val > mag_window[i])
  565. {
  566. window_min_val = mag_window[i];
  567. min_index = i;
  568. }
  569. }
  570. if(max_index < min_index && window_max_val - window_min_val > + 2000.0f)
  571. {
  572. return 1;
  573. }
  574. return 0;
  575. }
  576. void detect_zero_vel(int16_t front[3], int16_t back[3], int16_t acc[3],
  577. int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  578. {
  579. static float front_mag_window[WINDOW_SIZE];
  580. static float back_mag_window[WINDOW_SIZE];
  581. static float acc_x_window[WINDOW_SIZE];
  582. static float acc_y_window[WINDOW_SIZE];
  583. static float acc_z_window[WINDOW_SIZE];
  584. static int last_front_zupt;
  585. static int last_back_zupt;
  586. static int front_zupt_wait;
  587. static int back_zupt_wait;
  588. static int front_calm_down_wait;
  589. static int acc_zero_count;
  590. static float back_mag_max_up = 0.0f;
  591. static float back_mag_min_up = 50000.f;//磁力计均方和大小小于32000
  592. static float back_mag_max_down = 0.0f;
  593. static float back_mag_min_down = 50000.f;//磁力计均方和大小小于32000
  594. static float front_mag_max_down = 0.0f;
  595. static float front_mag_min_down = 50000.f;//磁力计均方和大小小于32000
  596. static float front_min_val;
  597. static float back_min_val;
  598. static float front_max_val;
  599. static float back_max_val;
  600. float front_val = (float) (abs(front[2]));
  601. float back_val = (float) (abs(back[2]));
  602. //滑动窗口更新数据
  603. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  604. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  605. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  606. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  607. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  608. front_mag_window[WINDOW_SIZE - 1] = front_val;
  609. back_mag_window[WINDOW_SIZE - 1] = back_val;
  610. acc_x_window[WINDOW_SIZE - 1] = acc[0] / 2048.f;
  611. acc_y_window[WINDOW_SIZE - 1] = acc[1] / 2048.f;
  612. acc_z_window[WINDOW_SIZE - 1] = acc[2] / 2048.f;
  613. // float front_max_val = front_val;
  614. // float front_min_val = front_val;
  615. //
  616. // uint16_t front_max_index = WINDOW_SIZE - 1;
  617. // uint16_t front_min_index = WINDOW_SIZE - 1;
  618. //
  619. // float back_max_val = back_val;
  620. // float back_min_val = back_val;
  621. //
  622. // uint16_t back_max_index = WINDOW_SIZE - 1;
  623. // uint16_t back_min_index = WINDOW_SIZE - 1;
  624. // for(int i = 0 ; i < WINDOW_SIZE; i++ )
  625. // {
  626. // if(front_mag_window[i] > front_max_val)
  627. // {
  628. // front_max_val = front_mag_window[i];
  629. // front_max_index = i;
  630. // }
  631. //
  632. // if(front_mag_window[i] < front_min_val)
  633. // {
  634. // front_min_val = front_mag_window[i];
  635. // front_min_index = i;
  636. // }
  637. //
  638. // if(back_mag_window[i] > back_max_val)
  639. // {
  640. // back_max_val = back_mag_window[i];
  641. // back_max_index = i;
  642. // }
  643. //
  644. // if(back_mag_window[i] < back_min_val)
  645. // {
  646. // back_min_val = back_mag_window[i];
  647. // back_min_index = i;
  648. // }
  649. //
  650. // }
  651. /*
  652. * 计算稳定的状态
  653. */
  654. float acc_max_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  655. float acc_min_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  656. float acc_max_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  657. float acc_min_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  658. float acc_max_val_z = acc_z_window[WINDOW_SIZE - 1];
  659. float acc_min_val_z = acc_z_window[WINDOW_SIZE - 1];
  660. for(int i = WINDOW_SIZE - 10 ; i < WINDOW_SIZE; i++ )
  661. {
  662. if(acc_x_window[i] < acc_min_val_x)
  663. {
  664. acc_min_val_x = acc_x_window[i];
  665. }
  666. if(acc_x_window[i] > acc_max_val_x)
  667. {
  668. acc_max_val_x = acc_x_window[i];
  669. }
  670. if(acc_y_window[i] < acc_min_val_y)
  671. {
  672. acc_min_val_y = acc_y_window[i];
  673. }
  674. if(acc_y_window[i] > acc_max_val_y)
  675. {
  676. acc_max_val_y = acc_y_window[i];
  677. }
  678. if(acc_z_window[i] < acc_min_val_z)
  679. {
  680. acc_min_val_z = acc_z_window[i];
  681. }
  682. if(acc_z_window[i] > acc_max_val_z)
  683. {
  684. acc_max_val_z = acc_z_window[i];
  685. }
  686. }
  687. int front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_min_val);
  688. int front_down_trend = isLongTimeDownTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_max_val);
  689. int back_up_trend = isLongTimeUpTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_min_val);
  690. int back_down_trend = isLongTimeDownTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_max_val);
  691. if(front_up_trend)
  692. {
  693. front_zero_tmp = 1;
  694. }
  695. else if(front_down_trend)
  696. {
  697. front_zero_tmp = 0;
  698. }
  699. else if(front_zero_tmp == 1)
  700. {
  701. front_zero_tmp = 2;
  702. }
  703. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  704. if(front_zero_tmp > 0)
  705. {
  706. front_zupt_wait = 20;
  707. }
  708. /*
  709. * 判断后脚往下压
  710. */
  711. if(back_up_trend)
  712. {
  713. back_zero_tmp = 1;
  714. }
  715. else if(back_down_trend)
  716. {
  717. back_zero_tmp = 0;
  718. }
  719. else if(back_zero_tmp == 1)
  720. {
  721. back_zero_tmp = 2;
  722. }
  723. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  724. if(back_zero_tmp > 0)
  725. {
  726. back_zupt_wait = 20;
  727. }
  728. /*过滤一下类似于穿拖鞋,后鞋垫与磁力计传感器之间的距离在振荡*/
  729. if( back_zero_tmp == 2 && front_down_trend)
  730. {
  731. back_zero_tmp = 0;
  732. }
  733. if(front_zero_tmp == 2 && back_down_trend)
  734. {
  735. front_zero_tmp = 0;
  736. }
  737. /*
  738. * 规避先脚掌下地,后脚掌在下地;接着是后脚先离地,再前脚离地的情况
  739. */
  740. // setLongTimeUpTrend(back_max_index, back_min_index, back_val, back_min_val, &back_mag_max_up, &back_mag_min_up);
  741. //
  742. // setLongTimeDownTrend(back_max_index, back_min_index, back_val, back_min_val, &back_mag_max_down, &back_mag_min_down);
  743. //
  744. // setLongTimeDownTrend(front_max_index, front_min_index, front_val, front_min_val, &front_mag_max_down, &front_mag_min_down);
  745. if(back_zero_tmp == 2 && back_mag_max_down > back_mag_min_down + 3000.f)
  746. {
  747. back_zero_tmp = 0;
  748. back_mag_max_down = 0.0f;
  749. back_mag_min_down = 50000.0f;
  750. }
  751. if(front_zero_tmp == 2 && front_mag_max_down > front_mag_min_down + 3000.f)
  752. {
  753. front_zero_tmp = 0;
  754. front_mag_max_down = 0.0f;
  755. front_mag_min_down = 50000.0f;
  756. }
  757. if((front_zupt_wait > 0 || back_zupt_wait > 0) && acc_max_val_x - acc_min_val_x < 0.1f && acc_max_val_y - acc_min_val_y < 0.1f && acc_max_val_z - acc_min_val_z < 0.1f)
  758. {
  759. *back_zero = 1;
  760. *front_zero = 1;
  761. }
  762. /*
  763. * 判断仅前踮 或 后垫, IMU往前移动,就不需要这个了
  764. */
  765. // if(back_zero_tmp == 2 && front_zero_tmp == 0)
  766. // {
  767. // back_zero_tmp = 0;
  768. // }
  769. //
  770. // if(front_zero_tmp == 2 && back_zero_tmp == 0)
  771. // {
  772. // front_zero_tmp = 0;
  773. // }
  774. //延迟等待稳定状态
  775. // if(*front_zero == 0 && front_zupt_wait > 0 && acc_max_val - acc_min_val < 0.08f)
  776. // {
  777. // *front_zero = 1;
  778. // }
  779. if(front_zero_tmp > 0)
  780. {
  781. /*
  782. * 如IMU放在前脚掌,就不用front_calm_down_wait == 0的判断了
  783. */
  784. *front_zero = 1;
  785. }
  786. else
  787. {
  788. *front_zero = 0;
  789. }
  790. // if(*back_zero == 0 && back_zupt_wait > 0 && acc_max_val - acc_min_val < 0.08f)
  791. // {
  792. // *back_zero = 1;
  793. // }
  794. if(back_zero_tmp > 0)
  795. {
  796. *back_zero = 1;
  797. }
  798. else
  799. {
  800. *back_zero = 0;
  801. }
  802. /*
  803. * 会出现缓慢离地,但是没有检测到离地,补个逻辑,后面整段检测触地逻辑需要重写
  804. */
  805. if(acc_max_val_x - acc_min_val_x > 0.25f || acc_max_val_y - acc_min_val_y > 0.25f || acc_max_val_z - acc_min_val_z > 0.25f)
  806. {
  807. if(press_down_front(front_mag_window + 10) || press_down_back(back_mag_window + 10))
  808. {
  809. *back_zero = 0;
  810. *front_zero = 0;
  811. front_zero_tmp = 0;
  812. back_zero_tmp = 0;
  813. }
  814. }
  815. for(int i = 0 ; i < WINDOW_SIZE - 5; i++ )
  816. {
  817. if(acc_x_window[i] < acc_min_val_x)
  818. {
  819. acc_min_val_x = acc_x_window[i];
  820. }
  821. if(acc_x_window[i] > acc_max_val_x)
  822. {
  823. acc_max_val_x = acc_x_window[i];
  824. }
  825. if(acc_y_window[i] < acc_min_val_y)
  826. {
  827. acc_min_val_y = acc_y_window[i];
  828. }
  829. if(acc_y_window[i] > acc_max_val_y)
  830. {
  831. acc_max_val_y = acc_y_window[i];
  832. }
  833. if(acc_z_window[i] < acc_min_val_z)
  834. {
  835. acc_min_val_z = acc_z_window[i];
  836. }
  837. if(acc_z_window[i] > acc_max_val_z)
  838. {
  839. acc_max_val_z = acc_z_window[i];
  840. }
  841. }
  842. if(acc_max_val_x - acc_min_val_x < 0.03f && acc_max_val_y - acc_min_val_y < 0.03f && acc_max_val_z - acc_min_val_z < 0.03f )
  843. {
  844. *acc_zero = 1;
  845. acc_zero_count ++;
  846. }
  847. else
  848. {
  849. *acc_zero = 0;
  850. acc_zero_count = 0;
  851. }
  852. // /*
  853. // * 强制大于1秒的零速均视为触地
  854. // */
  855. if(acc_zero_count > 100)
  856. {
  857. *front_zero = 1;
  858. *back_zero = 1;
  859. front_zero_tmp = 1;
  860. back_zero_tmp = 1;
  861. }
  862. //利用加速度延续
  863. if((last_front_zupt == 1|| last_back_zupt == 1)
  864. && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.1f
  865. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.1f
  866. && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.1f)
  867. {
  868. *front_zero = 1;
  869. *back_zero = 1;
  870. }
  871. if(front_zupt_wait > 0)
  872. {
  873. front_zupt_wait --;
  874. }
  875. if(back_zupt_wait > 0)
  876. {
  877. back_zupt_wait --;
  878. }
  879. if(*front_zero || *back_zero)
  880. {
  881. front_zupt_wait = 20;
  882. back_zupt_wait = 20;
  883. }
  884. last_front_zupt = *front_zero;
  885. last_back_zupt = *back_zero;
  886. /*
  887. * 直接用这个来记步好了
  888. */
  889. cal_step(*front_zero, *back_zero, acc_x_window[WINDOW_SIZE - 1], acc_y_window[WINDOW_SIZE - 1], acc_z_window[WINDOW_SIZE - 1]);
  890. }