detect_zero_vel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. void setLongTimeUpTrend(int max_index, int min_index, float max_val, float min_val, float *longTime_maxVal, float *longTime_minVal)
  413. {
  414. if(max_index > min_index && max_val > min_val + 800.f)
  415. {
  416. *longTime_maxVal = *longTime_maxVal > max_val ? *longTime_maxVal : max_val;
  417. *longTime_minVal = *longTime_minVal < min_val ? *longTime_minVal : min_val;
  418. }
  419. else if(max_index < min_index && max_val > min_val + 800.f)
  420. {
  421. *longTime_maxVal = 0.0f;
  422. *longTime_minVal = 50000.0f;
  423. }
  424. }
  425. void setLongTimeDownTrend(int max_index, int min_index, float max_val, float min_val, float *longTime_maxVal, float *longTime_minVal)
  426. {
  427. if(max_index > min_index && max_val > min_val + 800.f)
  428. {
  429. *longTime_maxVal = 0.0f;
  430. *longTime_minVal = 50000.0f;
  431. }
  432. else if(max_index < min_index && max_val > min_val + 800.f)
  433. {
  434. *longTime_maxVal = *longTime_maxVal > max_val ? *longTime_maxVal : max_val;
  435. *longTime_minVal = *longTime_minVal < min_val ? *longTime_minVal : min_val;
  436. }
  437. }
  438. int press_down_front(float *press_buff, int length)
  439. {
  440. static float max_val = 0.0f;
  441. if(press_buff[0] - press_buff[length - 1] > 100.0f)
  442. {
  443. max_val = press_buff[0] > max_val ? press_buff[0] : max_val;
  444. }
  445. else
  446. {
  447. max_val = 0;
  448. }
  449. if(max_val > press_buff[length - 1] + 2000.0f)
  450. {
  451. return 1;
  452. }
  453. return 0;
  454. }
  455. int press_down_back(float *press_buff, int length)
  456. {
  457. static float max_val = 0.0f;
  458. if(press_buff[0] - press_buff[length - 1] > 100.0f)
  459. {
  460. max_val = press_buff[0] > max_val ? press_buff[0] : max_val;
  461. }
  462. else
  463. {
  464. max_val = 0;
  465. }
  466. if(max_val > press_buff[length - 1] + 2000.0f)
  467. {
  468. return 1;
  469. }
  470. return 0;
  471. }
  472. //2020/01/02
  473. //长趋势判断压力上升
  474. int isLongTimeUpTrend(float *mag_window, int length, float up_threshhold, float *min_val)
  475. {
  476. //上下沿判断
  477. int max_index = 0; int min_index = 0;
  478. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  479. for(int i = 1; i < length; i++)
  480. {
  481. if(window_max_val < mag_window[i])
  482. {
  483. window_max_val = mag_window[i];
  484. max_index = i;
  485. }
  486. if(window_min_val > mag_window[i])
  487. {
  488. window_min_val = mag_window[i];
  489. min_index = i;
  490. }
  491. }
  492. *min_val = window_min_val;
  493. if((max_index > min_index && window_max_val - window_min_val > 1500.0f && window_max_val - mag_window[length - 1] < 10.f )
  494. || (mag_window[length - 1] - window_min_val > 2000.0f && window_max_val - mag_window[length - 1] < 10.f))
  495. {
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. int isLongTimeDownTrend(float *mag_window, int length, float up_threshhold, float *max_val)
  501. {
  502. //上下沿判断
  503. int max_index = 0; int min_index = 0;
  504. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  505. for(int i = 1; i < length; i++)
  506. {
  507. if(window_max_val < mag_window[i])
  508. {
  509. window_max_val = mag_window[i];
  510. max_index = i;
  511. }
  512. if(window_min_val > mag_window[i])
  513. {
  514. window_min_val = mag_window[i];
  515. min_index = i;
  516. }
  517. }
  518. *max_val = window_max_val;
  519. if((max_index < min_index && window_max_val - window_min_val > 2000.0f && mag_window[length - 1] - window_min_val < 1000.f)
  520. || window_max_val - mag_window[length - 1] > 1000.f)
  521. {
  522. return 1;
  523. }
  524. return 0;
  525. }
  526. void detect_zero_vel(int16_t front[3], int16_t back[3], int16_t acc[3],
  527. int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  528. {
  529. static float front_mag_window[WINDOW_SIZE];
  530. static float back_mag_window[WINDOW_SIZE];
  531. static float acc_x_window[WINDOW_SIZE];
  532. static float acc_y_window[WINDOW_SIZE];
  533. static float acc_z_window[WINDOW_SIZE];
  534. static int last_front_zupt;
  535. static int last_back_zupt;
  536. static int front_zupt_wait;
  537. static int back_zupt_wait;
  538. static int acc_zero_count;
  539. static float front_min_val;
  540. static float back_min_val;
  541. static float front_max_val;
  542. static float back_max_val;
  543. float front_val = (float) (abs(front[2]));
  544. float back_val = (float) (abs(back[2]));
  545. //滑动窗口更新数据
  546. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  547. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  548. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  549. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  550. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  551. front_mag_window[WINDOW_SIZE - 1] = front_val;
  552. back_mag_window[WINDOW_SIZE - 1] = back_val;
  553. acc_x_window[WINDOW_SIZE - 1] = acc[0] / 2048.f;
  554. acc_y_window[WINDOW_SIZE - 1] = acc[1] / 2048.f;
  555. acc_z_window[WINDOW_SIZE - 1] = acc[2] / 2048.f;
  556. /*
  557. * 计算稳定的状态
  558. */
  559. float acc_max_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  560. float acc_min_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  561. float acc_max_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  562. float acc_min_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  563. float acc_max_val_z = acc_z_window[WINDOW_SIZE - 1];
  564. float acc_min_val_z = acc_z_window[WINDOW_SIZE - 1];
  565. for(int i = WINDOW_SIZE - 10 ; i < WINDOW_SIZE; i++ )
  566. {
  567. if(acc_x_window[i] < acc_min_val_x)
  568. {
  569. acc_min_val_x = acc_x_window[i];
  570. }
  571. if(acc_x_window[i] > acc_max_val_x)
  572. {
  573. acc_max_val_x = acc_x_window[i];
  574. }
  575. if(acc_y_window[i] < acc_min_val_y)
  576. {
  577. acc_min_val_y = acc_y_window[i];
  578. }
  579. if(acc_y_window[i] > acc_max_val_y)
  580. {
  581. acc_max_val_y = acc_y_window[i];
  582. }
  583. if(acc_z_window[i] < acc_min_val_z)
  584. {
  585. acc_min_val_z = acc_z_window[i];
  586. }
  587. if(acc_z_window[i] > acc_max_val_z)
  588. {
  589. acc_max_val_z = acc_z_window[i];
  590. }
  591. }
  592. int front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_min_val);
  593. int front_down_trend = isLongTimeDownTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_max_val);
  594. int back_up_trend = isLongTimeUpTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_min_val);
  595. int back_down_trend = isLongTimeDownTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_max_val);
  596. if (back_down_trend == 1 && front_down_trend == 1)
  597. {
  598. front_up_trend = 0;
  599. back_up_trend = 0;
  600. }
  601. if(front_up_trend)
  602. {
  603. front_zero_tmp = 1;
  604. }
  605. else if(front_down_trend)
  606. {
  607. front_zero_tmp = 0;
  608. }
  609. else if(front_zero_tmp == 1)
  610. {
  611. front_zero_tmp = 2;
  612. }
  613. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  614. if(front_zero_tmp > 0)
  615. {
  616. front_zupt_wait = 20;
  617. }
  618. /*
  619. * 判断后脚往下压
  620. */
  621. if(back_up_trend)
  622. {
  623. back_zero_tmp = 1;
  624. }
  625. else if(back_down_trend)
  626. {
  627. back_zero_tmp = 0;
  628. }
  629. else if(back_zero_tmp == 1)
  630. {
  631. back_zero_tmp = 2;
  632. }
  633. // if(front_down_trend == 1 && back_up_trend == 1)
  634. // {
  635. // back_zero_tmp = 2;
  636. // }
  637. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  638. if(back_zero_tmp > 0)
  639. {
  640. back_zupt_wait = 20;
  641. }
  642. /*过滤一下类似于穿拖鞋,后鞋垫与磁力计传感器之间的距离在振荡*/
  643. if( back_zero_tmp != 0 && front_down_trend)
  644. {
  645. back_zero_tmp = 0;
  646. }
  647. if(front_zero_tmp == 2 && back_down_trend)
  648. {
  649. front_zero_tmp = 0;
  650. }
  651. if (fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) > 0.15f ||
  652. fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) > 0.15f ||
  653. fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) > 0.15f)
  654. {
  655. if (front_zero_tmp == 2)
  656. {
  657. front_zero_tmp = 0;
  658. }
  659. if (back_zero_tmp == 2)
  660. {
  661. back_zero_tmp = 0;
  662. }
  663. }
  664. if (front_zero_tmp == 1)
  665. {
  666. *front_zero = 1;
  667. }
  668. else if(front_zero_tmp == 2 &&fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.05f
  669. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.05f
  670. && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.05f)
  671. {
  672. *front_zero = 1;
  673. }
  674. else
  675. {
  676. *front_zero = 0;
  677. }
  678. if (back_zero_tmp > 0)
  679. {
  680. *back_zero = 1;
  681. }
  682. else if (back_zero_tmp == 2 && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.05f
  683. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.05f
  684. && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.05f)
  685. {
  686. *back_zero = 1;
  687. }
  688. else
  689. {
  690. *back_zero = 0;
  691. }
  692. if((front_zupt_wait > 0 || back_zupt_wait > 0) && acc_max_val_x - acc_min_val_x < 0.15f && acc_max_val_y - acc_min_val_y < 0.15f && acc_max_val_z - acc_min_val_z < 0.15f)
  693. {
  694. if (front_zupt_wait > 0)
  695. {
  696. *front_zero = 1;
  697. if (front_zero_tmp == 0)
  698. {
  699. front_zero_tmp = 2;
  700. }
  701. }
  702. if (back_zupt_wait > 0)
  703. {
  704. *back_zero = 1;
  705. if (back_zero_tmp == 0)
  706. {
  707. back_zero_tmp = 2;
  708. }
  709. }
  710. }
  711. for(int i = 0 ; i < WINDOW_SIZE - 5; i++ )
  712. {
  713. if(acc_x_window[i] < acc_min_val_x)
  714. {
  715. acc_min_val_x = acc_x_window[i];
  716. }
  717. if(acc_x_window[i] > acc_max_val_x)
  718. {
  719. acc_max_val_x = acc_x_window[i];
  720. }
  721. if(acc_y_window[i] < acc_min_val_y)
  722. {
  723. acc_min_val_y = acc_y_window[i];
  724. }
  725. if(acc_y_window[i] > acc_max_val_y)
  726. {
  727. acc_max_val_y = acc_y_window[i];
  728. }
  729. if(acc_z_window[i] < acc_min_val_z)
  730. {
  731. acc_min_val_z = acc_z_window[i];
  732. }
  733. if(acc_z_window[i] > acc_max_val_z)
  734. {
  735. acc_max_val_z = acc_z_window[i];
  736. }
  737. }
  738. 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 )
  739. {
  740. *acc_zero = 1;
  741. acc_zero_count ++;
  742. }
  743. else
  744. {
  745. *acc_zero = 0;
  746. acc_zero_count = 0;
  747. }
  748. 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)
  749. {
  750. *front_zero = 1;
  751. *back_zero = 1;
  752. front_zero_tmp = 2;
  753. back_zero_tmp = 2;
  754. }
  755. // /*
  756. // * 强制大于1秒的零速均视为触地
  757. // */
  758. if(acc_zero_count > 100)
  759. {
  760. *front_zero = 1;
  761. *back_zero = 1;
  762. front_zero_tmp = 1;
  763. back_zero_tmp = 1;
  764. }
  765. //利用加速度延续
  766. if((last_front_zupt == 1|| last_back_zupt == 1)
  767. && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.05f
  768. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.05f
  769. && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.05f)
  770. {
  771. if (last_front_zupt == 1)
  772. {
  773. *front_zero = 1;
  774. if (front_zero_tmp == 0)
  775. {
  776. front_zero_tmp = 2;
  777. }
  778. }
  779. if (last_back_zupt == 1)
  780. {
  781. *back_zero = 1;
  782. if (back_zero_tmp == 0)
  783. {
  784. back_zero_tmp = 2;
  785. }
  786. }
  787. }
  788. if(front_zupt_wait > 0)
  789. {
  790. front_zupt_wait --;
  791. }
  792. if(back_zupt_wait > 0)
  793. {
  794. back_zupt_wait --;
  795. }
  796. if (*front_zero)
  797. {
  798. front_zupt_wait = 20;
  799. }
  800. if(*back_zero)
  801. {
  802. back_zupt_wait = 20;
  803. }
  804. last_front_zupt = *front_zero;
  805. last_back_zupt = *back_zero;
  806. /*
  807. * 直接用这个来记步好了
  808. */
  809. //cal_step(*front_zero, *back_zero, acc_x_window[WINDOW_SIZE - 1], acc_y_window[WINDOW_SIZE - 1], acc_z_window[WINDOW_SIZE - 1]);
  810. }