detect_zero_vel.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. //#include "hal_imu.h"
  347. #define MAG_THRESHHOLD 2000.f
  348. int front_zero_tmp = 0;
  349. int back_zero_tmp = 0;
  350. void start_cal_step(int16_t front[3], int16_t back[3], int16_t acc[3])
  351. {
  352. int16_t front_zero = 0;
  353. int16_t back_zero = 0;
  354. int16_t acc_zero = 0;
  355. SEGGER_RTT_printf(0,"detect_zero_vel( front, back, acc, &front_zero, &back_zero, &acc_zero); \n");
  356. detect_zero_vel( front, back, acc, &front_zero, &back_zero, &acc_zero);
  357. }
  358. void cal_step(int16_t front_zero, int16_t back_zero, float acc_x, float acc_y, float acc_z)
  359. {
  360. static int step_calm_down;
  361. static float acc_min[3];
  362. static float acc_max[3];
  363. if(!(front_zero || back_zero))
  364. {
  365. acc_max[2] = 1.0f;
  366. acc_min[2] = 1.0f;
  367. if(acc_x > acc_max[0])
  368. {
  369. acc_max[0] = acc_x;
  370. }
  371. if(acc_y > acc_max[1])
  372. {
  373. acc_max[1] = acc_y;
  374. }
  375. if(acc_z > acc_max[2])
  376. {
  377. acc_max[2] = acc_z;
  378. }
  379. if(acc_x < acc_min[0])
  380. {
  381. acc_min[0] = acc_x;
  382. }
  383. if(acc_y < acc_min[1])
  384. {
  385. acc_min[1] = acc_y;
  386. }
  387. if(acc_z < acc_min[2])
  388. {
  389. acc_min[2] = acc_z;
  390. }
  391. }
  392. if(front_zero || back_zero)
  393. {
  394. 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))
  395. {
  396. mFlash.mStep.stepCur[0] ++;
  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. // float prev_window_sum = (mag_window[0] + mag_window[1] + mag_window[2]) * 0.333f;
  477. //
  478. // float back_window_sum = (mag_window[length - 1] + mag_window[length - 2] + mag_window[length - 3]) * 0.333f;
  479. //
  480. // float back_window_sum1 = (mag_window[length - 4] + mag_window[length - 5] + mag_window[length - 6]) * 0.333f;
  481. // float back_window_sum2 = (mag_window[length - 7] + mag_window[length - 8] + mag_window[length - 9]) * 0.333f;
  482. //
  483. // 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 )
  484. // {
  485. // //触发判断, 先替换掉最小值
  486. // float min_val_temp = mag_window[0];
  487. //
  488. // min_val_temp = min_val_temp < mag_window[1] ? min_val_temp : mag_window[1];
  489. //
  490. // min_val_temp = min_val_temp < mag_window[2] ? min_val_temp : mag_window[2];
  491. //
  492. // *min_val = min_val_temp < *min_val ? min_val_temp : *min_val;
  493. //
  494. // float max_val_temp = mag_window[length - 6];
  495. //
  496. // for(int i = length - 5; i < length; i++)
  497. // {
  498. // if(max_val_temp < mag_window[i])
  499. // {
  500. // max_val_temp = mag_window[i];
  501. // }
  502. // }
  503. //
  504. // if(max_val_temp > *min_val + 1500.0f)
  505. // {
  506. // return 1;
  507. // }
  508. // }
  509. // else
  510. // {
  511. // *min_val = mag_window[length - 1];
  512. // }
  513. //上下沿判断
  514. int max_index = 0; int min_index = 0;
  515. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  516. for(int i = 1; i < length; i++)
  517. {
  518. if(window_max_val < mag_window[i])
  519. {
  520. window_max_val = mag_window[i];
  521. max_index = i;
  522. }
  523. if(window_min_val > mag_window[i])
  524. {
  525. window_min_val = mag_window[i];
  526. min_index = i;
  527. }
  528. }
  529. *min_val = window_min_val;
  530. if((max_index > min_index && window_max_val - window_min_val > 1500.0f && window_max_val - mag_window[length - 1] < 1000.f )
  531. || (mag_window[length - 1] - window_min_val > 2000.0f))
  532. {
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. int isLongTimeDownTrend(float *mag_window, int length, float up_threshhold, float *max_val)
  538. {
  539. // float prev_window_sum = (mag_window[0] + mag_window[1] + mag_window[2]) * 0.333f;
  540. //
  541. // float back_window_sum = (mag_window[length - 1] + mag_window[length - 2] + mag_window[length - 3]) * 0.333f;
  542. //
  543. // float back_window_sum1 = (mag_window[length - 4] + mag_window[length - 5] + mag_window[length - 6]) * 0.333f;
  544. // float back_window_sum2 = (mag_window[length - 7] + mag_window[length - 8] + mag_window[length - 9]) * 0.333f;
  545. //
  546. // 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 )
  547. // {
  548. // float max_val_temp = mag_window[0];
  549. //
  550. // max_val_temp = max_val_temp > mag_window[1] ? max_val_temp : mag_window[1];
  551. //
  552. // max_val_temp = max_val_temp > mag_window[2] ? max_val_temp : mag_window[2];
  553. //
  554. // *max_val = *max_val > max_val_temp ? *max_val : max_val_temp;
  555. //
  556. //
  557. //
  558. // float min_val_temp = mag_window[length - 6];
  559. //
  560. // for(int i = length - 5; i < length; i++)
  561. // {
  562. // if(min_val_temp > mag_window[i])
  563. // {
  564. // min_val_temp = mag_window[i];
  565. // }
  566. // }
  567. // if(*max_val > min_val_temp + 1500.0f)
  568. // {
  569. // return 1;
  570. // }
  571. // }
  572. // else
  573. // {
  574. // *max_val = mag_window[length - 1];
  575. // }
  576. //上下沿判断
  577. int max_index = 0; int min_index = 0;
  578. float window_max_val = mag_window[0]; float window_min_val = mag_window[0];
  579. for(int i = 1; i < length; i++)
  580. {
  581. if(window_max_val < mag_window[i])
  582. {
  583. window_max_val = mag_window[i];
  584. max_index = i;
  585. }
  586. if(window_min_val > mag_window[i])
  587. {
  588. window_min_val = mag_window[i];
  589. min_index = i;
  590. }
  591. }
  592. *max_val = window_max_val;
  593. if((max_index < min_index && window_max_val - window_min_val > 2000.0f && mag_window[length - 1] - window_min_val < 1000.f)
  594. || window_max_val - mag_window[length - 1] > 1000.f)
  595. {
  596. return 1;
  597. }
  598. return 0;
  599. }
  600. void detect_zero_vel(int16_t front[3], int16_t back[3], int16_t acc[3],
  601. int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  602. {
  603. static float front_mag_window[WINDOW_SIZE];
  604. static float back_mag_window[WINDOW_SIZE];
  605. static float acc_x_window[WINDOW_SIZE];
  606. static float acc_y_window[WINDOW_SIZE];
  607. static float acc_z_window[WINDOW_SIZE];
  608. static int last_front_zupt;
  609. static int last_back_zupt;
  610. static int front_zupt_wait;
  611. static int back_zupt_wait;
  612. static int acc_zero_count;
  613. static float front_min_val;
  614. static float back_min_val;
  615. static float front_max_val;
  616. static float back_max_val;
  617. float front_val = (float) (abs(front[2]));
  618. float back_val = (float) (abs(back[2]));
  619. //滑动窗口更新数据
  620. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  621. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  622. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  623. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  624. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  625. front_mag_window[WINDOW_SIZE - 1] = front_val;
  626. back_mag_window[WINDOW_SIZE - 1] = back_val;
  627. acc_x_window[WINDOW_SIZE - 1] = acc[0] / 2048.f;
  628. acc_y_window[WINDOW_SIZE - 1] = acc[1] / 2048.f;
  629. acc_z_window[WINDOW_SIZE - 1] = acc[2] / 2048.f;
  630. /*
  631. * 计算稳定的状态
  632. */
  633. float acc_max_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  634. float acc_min_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  635. float acc_max_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  636. float acc_min_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  637. float acc_max_val_z = acc_z_window[WINDOW_SIZE - 1];
  638. float acc_min_val_z = acc_z_window[WINDOW_SIZE - 1];
  639. for(int i = WINDOW_SIZE - 10 ; i < WINDOW_SIZE; i++ )
  640. {
  641. if(acc_x_window[i] < acc_min_val_x)
  642. {
  643. acc_min_val_x = acc_x_window[i];
  644. }
  645. if(acc_x_window[i] > acc_max_val_x)
  646. {
  647. acc_max_val_x = acc_x_window[i];
  648. }
  649. if(acc_y_window[i] < acc_min_val_y)
  650. {
  651. acc_min_val_y = acc_y_window[i];
  652. }
  653. if(acc_y_window[i] > acc_max_val_y)
  654. {
  655. acc_max_val_y = acc_y_window[i];
  656. }
  657. if(acc_z_window[i] < acc_min_val_z)
  658. {
  659. acc_min_val_z = acc_z_window[i];
  660. }
  661. if(acc_z_window[i] > acc_max_val_z)
  662. {
  663. acc_max_val_z = acc_z_window[i];
  664. }
  665. }
  666. int front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_min_val);
  667. int front_down_trend = isLongTimeDownTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_max_val);
  668. int back_up_trend = isLongTimeUpTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_min_val);
  669. int back_down_trend = isLongTimeDownTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_max_val);
  670. if (back_down_trend == 1 && front_down_trend == 1)
  671. {
  672. front_up_trend = 0;
  673. back_up_trend = 0;
  674. }
  675. if(back_mag_window[WINDOW_SIZE - 1] - back_min_val > 1000.f && front_mag_window[WINDOW_SIZE-1] - front_min_val > 500.f
  676. && fabsf(front_max_val - front_mag_window[WINDOW_SIZE-1]) < 0.001f)
  677. {
  678. front_up_trend = 1;
  679. }
  680. // if (back_up_trend == 1 && acc_z_window[WINDOW_SIZE - 1] > 0.7f)
  681. // {
  682. // front_up_trend = 1;
  683. // }
  684. //if(back_down_trend == 1 && front_up_trend == 1)
  685. //{
  686. // printf("back_down_trend == 1 && front_up_trend == 1 \n");
  687. // front_up_trend = 0;
  688. //}
  689. if(front_up_trend)
  690. {
  691. front_zero_tmp = 1;
  692. }
  693. else if(front_down_trend)
  694. {
  695. front_zero_tmp = 0;
  696. }
  697. else if(front_zero_tmp == 1)
  698. {
  699. front_zero_tmp = 2;
  700. }
  701. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  702. if(front_zero_tmp > 0)
  703. {
  704. front_zupt_wait = 20;
  705. }
  706. /*
  707. * 判断后脚往下压
  708. */
  709. if(back_up_trend)
  710. {
  711. back_zero_tmp = 1;
  712. }
  713. else if(back_down_trend)
  714. {
  715. back_zero_tmp = 0;
  716. }
  717. else if(back_zero_tmp == 1)
  718. {
  719. back_zero_tmp = 2;
  720. }
  721. // if(front_down_trend == 1 && back_up_trend == 1)
  722. // {
  723. // back_zero_tmp = 2;
  724. // }
  725. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  726. if(back_zero_tmp > 0)
  727. {
  728. back_zupt_wait = 20;
  729. }
  730. /*过滤一下类似于穿拖鞋,后鞋垫与磁力计传感器之间的距离在振荡*/
  731. if( back_zero_tmp == 2 && front_down_trend)
  732. {
  733. back_zero_tmp = 0;
  734. }
  735. if(front_zero_tmp == 2 && back_down_trend)
  736. {
  737. front_zero_tmp = 0;
  738. }
  739. if((front_zupt_wait > 0 || back_zupt_wait > 0) && acc_max_val_x - acc_min_val_x < 0.2f && acc_max_val_y - acc_min_val_y < 0.2f && acc_max_val_z - acc_min_val_z < 0.2f)
  740. {
  741. *back_zero = 1;
  742. *front_zero = 1;
  743. }
  744. if(front_zero_tmp > 0)
  745. {
  746. /*
  747. * 如IMU放在前脚掌,就不用front_calm_down_wait == 0的判断了
  748. */
  749. *front_zero = 1;
  750. }
  751. else
  752. {
  753. *front_zero = 0;
  754. }
  755. if(back_zero_tmp > 0)
  756. {
  757. *back_zero = 1;
  758. }
  759. else
  760. {
  761. *back_zero = 0;
  762. }
  763. for(int i = 0 ; i < WINDOW_SIZE - 5; i++ )
  764. {
  765. if(acc_x_window[i] < acc_min_val_x)
  766. {
  767. acc_min_val_x = acc_x_window[i];
  768. }
  769. if(acc_x_window[i] > acc_max_val_x)
  770. {
  771. acc_max_val_x = acc_x_window[i];
  772. }
  773. if(acc_y_window[i] < acc_min_val_y)
  774. {
  775. acc_min_val_y = acc_y_window[i];
  776. }
  777. if(acc_y_window[i] > acc_max_val_y)
  778. {
  779. acc_max_val_y = acc_y_window[i];
  780. }
  781. if(acc_z_window[i] < acc_min_val_z)
  782. {
  783. acc_min_val_z = acc_z_window[i];
  784. }
  785. if(acc_z_window[i] > acc_max_val_z)
  786. {
  787. acc_max_val_z = acc_z_window[i];
  788. }
  789. }
  790. 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 )
  791. {
  792. *acc_zero = 1;
  793. acc_zero_count ++;
  794. }
  795. else
  796. {
  797. *acc_zero = 0;
  798. acc_zero_count = 0;
  799. }
  800. // /*
  801. // * 强制大于1秒的零速均视为触地
  802. // */
  803. if(acc_zero_count > 100)
  804. {
  805. *front_zero = 1;
  806. *back_zero = 1;
  807. front_zero_tmp = 1;
  808. back_zero_tmp = 1;
  809. }
  810. //利用加速度延续
  811. if((last_front_zupt == 1|| last_back_zupt == 1)
  812. && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.1f
  813. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.1f
  814. && fabsf(acc_z_window[WINDOW_SIZE - 2] - acc_z_window[WINDOW_SIZE - 1]) < 0.1f)
  815. {
  816. *front_zero = 1;
  817. *back_zero = 1;
  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(*front_zero || *back_zero)
  828. {
  829. front_zupt_wait = 20;
  830. back_zupt_wait = 20;
  831. }
  832. last_front_zupt = *front_zero;
  833. last_back_zupt = *back_zero;
  834. /*
  835. * 直接用这个来记步好了
  836. */
  837. cal_step(*front_zero, *back_zero, acc_x_window[WINDOW_SIZE - 1], acc_y_window[WINDOW_SIZE - 1], acc_z_window[WINDOW_SIZE - 1]);
  838. }