detect_zero_vel.c 20 KB

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