detect_zero_vel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include "system.h"
  2. #include "detect_zero_vel.h"
  3. #include "hal_mt.h"
  4. #include "hal_mt.h"
  5. #include "app_flash.h"
  6. //#include "hal_imu.h"
  7. #define MAG_THRESHHOLD 500
  8. #define SAMPLE_C 4
  9. int16_t front_zero_tmp = 0;
  10. int16_t back_zero_tmp = 0;
  11. static float var_acc_temp;
  12. int32_t get_var_acc(void)
  13. {
  14. return var_acc_temp * 1000.f;
  15. }
  16. void start_cal_step(int16_t front[3], int16_t back[3], int16_t acc[3])
  17. {
  18. }
  19. void cal_step(int16_t front_zero, int16_t back_zero, float acc_x, float acc_y, float acc_z)
  20. {
  21. }
  22. float var_acc_f(int16_t* acc, int length, int sample_c)
  23. {
  24. if (length < 10*sample_c)
  25. {
  26. return 0.0f;
  27. }
  28. float mean_x = 0;
  29. float sum_x = 0;
  30. for (int i = length - 1; i >= length - 10*sample_c; i-=sample_c)
  31. {
  32. sum_x += acc[i];
  33. }
  34. mean_x = sum_x *0.1f;
  35. sum_x = 0.0f;
  36. for (int i = length - 1; i >= length - 10*sample_c; i-=sample_c)
  37. {
  38. sum_x += ((acc[i] - mean_x) * (acc[i] - mean_x));
  39. }
  40. return sum_x *0.1f / 2048.f /2048.f;
  41. }
  42. int16_t find_val(int16_t* data, int start_index, int end_index, char s)
  43. {
  44. int16_t val = data[end_index - 1];
  45. for (int i = end_index - 1; i >= start_index; i-= SAMPLE_C)
  46. {
  47. if (s == '<')
  48. {
  49. if (val > data[i])
  50. {
  51. val = data[i];
  52. }
  53. }
  54. else
  55. {
  56. if (val < data[i])
  57. {
  58. val = data[i];
  59. }
  60. }
  61. }
  62. return val;
  63. }
  64. //2020/01/02
  65. //长趋势判断压力上升
  66. int isLongTimeUpTrend(int16_t *mag_window, int16_t length, int16_t up_threshhold, int16_t *min_val)
  67. {
  68. //上下沿判断
  69. int16_t max_index = 0; int16_t min_index = 0;
  70. int16_t window_max_val = mag_window[length - 1];
  71. int16_t window_min_val = mag_window[length - 1];
  72. for(int i = length - 1; i >= 0; i -= SAMPLE_C)
  73. {
  74. if(window_max_val < mag_window[i])
  75. {
  76. window_max_val = mag_window[i];
  77. max_index = i;
  78. }
  79. if(window_min_val > mag_window[i])
  80. {
  81. window_min_val = mag_window[i];
  82. min_index = i;
  83. }
  84. }
  85. *min_val = window_min_val;
  86. if((max_index > min_index && window_max_val - window_min_val > up_threshhold && window_max_val - mag_window[length - 1] < 10 )
  87. || (mag_window[length - 1] - window_min_val > up_threshhold && window_max_val - mag_window[length - 1] < 10))
  88. {
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. int isLongTimeDownTrend(int16_t *mag_window, int16_t length, int16_t down_threshhold, int16_t *max_val)
  94. {
  95. //上下沿判断
  96. int16_t max_index = 0; int16_t min_index = 0;
  97. int16_t window_max_val = mag_window[length - 1];
  98. int16_t window_min_val = mag_window[length - 1];
  99. for(int i = length - 1; i >=0; i -= SAMPLE_C)
  100. {
  101. if(window_max_val < mag_window[i])
  102. {
  103. window_max_val = mag_window[i];
  104. max_index = i;
  105. }
  106. if(window_min_val > mag_window[i])
  107. {
  108. window_min_val = mag_window[i];
  109. min_index = i;
  110. }
  111. }
  112. *max_val = window_max_val;
  113. if((max_index < min_index && window_max_val - window_min_val > down_threshhold && mag_window[length - 1] - window_min_val < down_threshhold/2)
  114. || window_max_val - mag_window[length - 1] > down_threshhold / 2)
  115. {
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. //寻找加速度的最大值以及最小值
  121. void find_acc_max_and_min_val(int16_t*src, int16_t start_index, int16_t end_index, int16_t sample, int16_t* max_val, int16_t*min_val)
  122. {
  123. *max_val = src[end_index - 1];
  124. *min_val = src[end_index - 1];
  125. for(int i = end_index - 1; i >= start_index; i -= sample)
  126. {
  127. if(*max_val < src[i])
  128. {
  129. *max_val = src[i];
  130. }
  131. if(*min_val > src[i])
  132. {
  133. *min_val = src[i];
  134. }
  135. }
  136. }
  137. //根据压力的上升或下降,设置零速估计状态
  138. void setZeroStatus(int16_t up_trend, int16_t down_trend, int16_t *zero_tmp)
  139. {
  140. if(up_trend)
  141. {
  142. *zero_tmp = 1;
  143. }
  144. else if(down_trend)
  145. {
  146. *zero_tmp = 0;
  147. }
  148. else if(*zero_tmp == 1)
  149. {
  150. *zero_tmp = 2;
  151. }
  152. }
  153. //归纳整理触地信息的结果
  154. void setTouchFloorStatus(int16_t *zero_tmp, int16_t *zero,
  155. int16_t *acc_x_window, int16_t *acc_y_window, int16_t *acc_z_window)
  156. {
  157. if (*zero_tmp == 1)
  158. {
  159. *zero = 1;
  160. }
  161. else if(*zero_tmp == 2 &&abs(acc_x_window[WINDOW_SIZE - 1- SAMPLE_C] - acc_x_window[WINDOW_SIZE - 1]) < 103
  162. && abs(acc_y_window[WINDOW_SIZE - 1- SAMPLE_C] - acc_y_window[WINDOW_SIZE - 1]) < 103
  163. && abs(acc_z_window[WINDOW_SIZE - 1- SAMPLE_C] - acc_z_window[WINDOW_SIZE - 1]) < 103)
  164. {
  165. *zero = 1;
  166. }
  167. else
  168. {
  169. *zero_tmp = 0;
  170. *zero = 0;
  171. }
  172. }
  173. void detect_zero_vel(int16_t front[3], int16_t back[3], int16_t acc[3],
  174. int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  175. {
  176. //压力滑动窗口
  177. static int16_t front_mag_window[WINDOW_SIZE];
  178. static int16_t back_mag_window[WINDOW_SIZE];
  179. //加速度滑动窗口
  180. static int16_t acc_x_window[WINDOW_SIZE];
  181. static int16_t acc_y_window[WINDOW_SIZE];
  182. static int16_t acc_z_window[WINDOW_SIZE];
  183. static int16_t last_front_zupt;
  184. static int16_t last_back_zupt;
  185. static int16_t front_zupt_wait;
  186. static int16_t back_zupt_wait;
  187. static int16_t press_wait;
  188. static int16_t shake_acc_wait;
  189. static int16_t continue_up_min_val;
  190. static int acc_zero_count;
  191. static int16_t front_up_wait;
  192. static int16_t back_up_wait;
  193. static int16_t press_up_wait;
  194. static int16_t special_press_up_wait;
  195. static int16_t front_min_val;
  196. static int16_t back_min_val;
  197. static int16_t front_max_val;
  198. static int16_t back_max_val;
  199. int16_t front_val = abs(front[2]);
  200. int16_t back_val = abs(back[2]);
  201. //滑动窗口更新数据
  202. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  203. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  204. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  205. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  206. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  207. front_mag_window[WINDOW_SIZE - 1] = front_val;
  208. back_mag_window[WINDOW_SIZE - 1] = back_val;
  209. acc_x_window[WINDOW_SIZE - 1] = acc[0];
  210. acc_y_window[WINDOW_SIZE - 1] = acc[1];
  211. acc_z_window[WINDOW_SIZE - 1] = acc[2];
  212. /*
  213. * 计算稳定的状态
  214. */
  215. int16_t acc_max_val_x , acc_min_val_x ;
  216. int16_t acc_max_val_y , acc_min_val_y ;
  217. int16_t acc_max_val_z , acc_min_val_z ;
  218. //寻找加速度窗口的最大值、最小值
  219. find_acc_max_and_min_val(acc_x_window, WINDOW_SIZE - SAMPLE_C * 10, WINDOW_SIZE, SAMPLE_C , &acc_max_val_x, &acc_min_val_x);
  220. find_acc_max_and_min_val(acc_y_window, WINDOW_SIZE - SAMPLE_C * 10, WINDOW_SIZE, SAMPLE_C, &acc_max_val_y, &acc_min_val_y);
  221. find_acc_max_and_min_val(acc_z_window, WINDOW_SIZE - SAMPLE_C * 10, WINDOW_SIZE, SAMPLE_C, &acc_max_val_z, &acc_min_val_z);
  222. //判断前后脚的压力上升下降状态
  223. int back_up_trend = isLongTimeUpTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_min_val);
  224. //由于不能直接用后脚用压力来判断,那么就弄个倒计时好了
  225. if (back_up_trend == 1)
  226. {
  227. back_up_wait = 20 * SAMPLE_C;
  228. }
  229. int back_down_trend = isLongTimeDownTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_max_val);
  230. //当还处于后脚压力上升的余热中,降低前脚的判断阈值
  231. int front_up_trend;
  232. //当back_up_wait 后脚跟压力上升的等待时间 大于0时候, 阈值调低为2000,否则为3000
  233. if (back_up_wait > 0)
  234. {
  235. front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD * 2, &front_min_val);
  236. }
  237. else
  238. {
  239. front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD * 4, &front_min_val);
  240. }
  241. //缓慢踩地
  242. if(front_mag_window[WINDOW_SIZE - 1] - front_mag_window[WINDOW_SIZE - 1 - SAMPLE_C] >-50)
  243. {
  244. if(continue_up_min_val > front_mag_window[WINDOW_SIZE - 1])
  245. {
  246. continue_up_min_val = front_mag_window[WINDOW_SIZE - 1];
  247. }
  248. }
  249. else
  250. {
  251. continue_up_min_val = front_mag_window[WINDOW_SIZE - 1];
  252. }
  253. if (front_mag_window[WINDOW_SIZE - 1] - continue_up_min_val > MAG_THRESHHOLD * 2)
  254. {
  255. front_up_trend = 1;
  256. }
  257. //认为后脚跟触地也归为触地
  258. if (back_mag_window[WINDOW_SIZE - 1] - back_min_val > MAG_THRESHHOLD * 4)
  259. {
  260. front_up_trend = 1;
  261. }
  262. //front_down_trend 为 1时候, 意味着前脚掌压力下降
  263. int front_down_trend = isLongTimeDownTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_max_val);
  264. // 自定义前后脚压力均上升,则重置压力等待时间,press_up_wait用于判断产生剧烈抖动时,则为触地
  265. // special_press_up_wait 用于判断延续触地逻辑,使其阈值变大(解决触地剧烈抖动的情况)
  266. if ((back_mag_window[WINDOW_SIZE - 1] - back_min_val > 500 && back_max_val - back_mag_window[WINDOW_SIZE - 1] < 200) ||
  267. (front_mag_window[WINDOW_SIZE - 1] - front_min_val > 500 && front_max_val - front_mag_window[WINDOW_SIZE - 1] < 200) ||
  268. (front_mag_window[WINDOW_SIZE - 1] - continue_up_min_val > 500))
  269. {
  270. press_up_wait = 20 * SAMPLE_C;
  271. special_press_up_wait = 20 * SAMPLE_C;
  272. }
  273. //发现前脚压力向下降的时候,置special_press_up_wait为0,避免离地时候,该倒计时还要生效
  274. if(front_down_trend == 1)
  275. {
  276. special_press_up_wait = 0;
  277. }
  278. //当前脚掌压力上升,后脚压力上升,则意味全脚掌着地,那就意味是真正的触地
  279. if (back_mag_window[WINDOW_SIZE - 1] - back_min_val > MAG_THRESHHOLD * 2 && front_mag_window[WINDOW_SIZE - 1] - front_min_val > MAG_THRESHHOLD * 2)
  280. {
  281. front_up_trend = 1;
  282. }
  283. //if (back_down_trend == 1 || front_down_trend == 1)
  284. //{
  285. // front_up_trend = 0;
  286. // back_up_trend = 0;
  287. //}
  288. //根据压力的上升或下降,设置零速估计状态
  289. setZeroStatus(front_up_trend, front_down_trend, &front_zero_tmp);
  290. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  291. if(front_zero_tmp > 0)
  292. {
  293. front_zupt_wait = 20 * SAMPLE_C;
  294. }
  295. //后脚作同样的处理
  296. //setZeroStatus(back_up_trend, back_down_trend, &back_zero_tmp);
  297. //
  298. //
  299. ////当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  300. //if(back_zero_tmp > 0)
  301. //{
  302. // back_zupt_wait = 20 * SAMPLE_C;
  303. //}
  304. /*过滤一下类似于穿拖鞋,后鞋垫与磁力计传感器之间的距离在振荡*/
  305. //相当于脚尖开始用力了,开始要走的状态,发现后脚的状态还处于触地状态,那么认为是误判
  306. /*if( back_zero_tmp != 0 && front_down_trend)
  307. {
  308. back_zero_tmp = 0;
  309. }*/
  310. //同样的,后脚泄力了,但是前脚还处于稳定的触地状态,那么解除这个稳定状态
  311. /*if(front_zero_tmp == 2 && back_down_trend)
  312. {
  313. front_zero_tmp = 0;
  314. }*/
  315. //下面处理的刚触地时候,导致剧烈抖动,从而使触地状态不连续
  316. // var_acc_temp = var_acc_f(acc_z_window, 10 * SAMPLE_C, 1);
  317. // //寻找前后脚窗口的最大最小值
  318. // float front_mag_max_temp = find_val(front_mag_window, 0, 10 * SAMPLE_C , '>');
  319. // float front_mag_min_temp = find_val(front_mag_window, 0, 10 * SAMPLE_C , '<');
  320. // float back_mag_max_temp = find_val(back_mag_window, 0, 10 * SAMPLE_C , '>');
  321. // float back_mag_min_temp = find_val(back_mag_window, 0, 10 * SAMPLE_C , '<');
  322. //发现抖动得厉害,而且压力窗口的前半部分处于一种平缓的状态,那么认为这个触地抖动导致的
  323. //if (var_acc_temp > 0.05f && back_mag_max_temp - back_mag_min_temp > 1000)
  324. //{
  325. // press_wait = 10 * SAMPLE_C;
  326. //}
  327. //
  328. ////处于假定的触地抖动时间press_wait, 发现有压力上升,那么肯定是触地了
  329. //if (front_mag_window[WINDOW_SIZE - 1] - front_mag_window[0] > 2000 && press_wait > 0)
  330. //{
  331. // front_zero_tmp = 1;
  332. // shake_acc_wait = 20 * SAMPLE_C;
  333. //}
  334. //后脚同理
  335. // if (back_mag_window[WINDOW_SIZE - 1] - back_mag_window[0] > 1500 && press_wait > 0)
  336. // {
  337. // back_zero_tmp = 1;
  338. // shake_acc_wait = 20 * SAMPLE_C;
  339. // }
  340. //归纳上述的处理结果,并且利用acc来延续触地的判断
  341. setTouchFloorStatus(&front_zero_tmp, front_zero, acc_x_window, acc_y_window, acc_z_window);
  342. //setTouchFloorStatus(&back_zero_tmp, back_zero, acc_x_window, acc_y_window, acc_z_window);
  343. //有时候触地状态中断了,那么根据短时间内出现加速度平稳,那么意味着这个平稳状态是触地状态
  344. if((front_zupt_wait > 0 || press_up_wait > 0 ) &&
  345. (acc_max_val_x - acc_min_val_x < 130 && acc_max_val_y - acc_min_val_y < 130 && acc_max_val_z - acc_min_val_z < 130))
  346. {
  347. *front_zero = 1;
  348. if (front_zero_tmp == 0)
  349. {
  350. front_zero_tmp = 2;
  351. }
  352. }
  353. int32_t acc_norm = (int32_t)acc[0] * (int32_t)acc[0] + (int32_t)acc[1] *(int32_t) acc[1] + (int32_t)acc[2] * (int32_t)acc[2];
  354. //利用特殊的压力上升等待时间和抖动来判断
  355. if(special_press_up_wait > 0 && abs(acc_norm - 4194304) < 300000)
  356. {
  357. //*front_zero = 1;
  358. if (front_zero_tmp == 0)
  359. {
  360. front_zero_tmp = 2;
  361. }
  362. }
  363. //累加平稳加速度的数目,如果超过一秒,则强制归位触地状态
  364. if(acc_max_val_x - acc_min_val_x < 103 && acc_max_val_y - acc_min_val_y < 103 && acc_max_val_z - acc_min_val_z < 103)
  365. {
  366. *acc_zero = 1;
  367. acc_zero_count ++;
  368. }
  369. else
  370. {
  371. *acc_zero = 0;
  372. acc_zero_count = 0;
  373. }
  374. // /*
  375. // * 强制大于1秒的零速均视为触地
  376. // */
  377. if(acc_zero_count > 100 * SAMPLE_C)
  378. {
  379. *front_zero = 1;
  380. front_zero_tmp = 1;
  381. }
  382. //利用加速度延续
  383. int16_t continue_thresh = 103;
  384. if(special_press_up_wait > 0 )
  385. {
  386. continue_thresh = 1030;
  387. }
  388. // if (shake_acc_wait > 0)
  389. // {
  390. // continue_thresh = 1030;
  391. // }else if(MT_GetState())
  392. // {
  393. // //震动,导致加速度数据产生抖动
  394. // continue_thresh = 250;
  395. // }
  396. if(last_front_zupt == 1
  397. && abs(acc_x_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_x_window[WINDOW_SIZE - 1]) < continue_thresh
  398. && abs(acc_y_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_y_window[WINDOW_SIZE - 1]) < continue_thresh
  399. && abs(acc_z_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_z_window[WINDOW_SIZE - 1]) < continue_thresh)
  400. {
  401. *front_zero = 1;
  402. if (front_zero_tmp == 0)
  403. {
  404. front_zero_tmp = 2;
  405. }
  406. }
  407. //各种倒计时
  408. if(front_zupt_wait > 0)
  409. {
  410. front_zupt_wait --;
  411. }
  412. if (press_wait > 0)
  413. {
  414. press_wait--;
  415. }
  416. if (shake_acc_wait > 0)
  417. {
  418. shake_acc_wait--;
  419. }
  420. if (back_up_wait > 0)
  421. {
  422. back_up_wait--;
  423. }
  424. if (press_up_wait > 0)
  425. {
  426. press_up_wait--;
  427. }
  428. if (*front_zero)
  429. {
  430. front_zupt_wait = 20 * SAMPLE_C;
  431. }
  432. if(special_press_up_wait > 0)
  433. {
  434. special_press_up_wait --;
  435. }
  436. last_front_zupt = *front_zero;
  437. last_back_zupt = *back_zero;
  438. }