detect_zero_vel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 front_zupt_wait;
  185. static int16_t press_wait;
  186. static int16_t shake_acc_wait;
  187. static int16_t continue_up_min_val;
  188. static int acc_zero_count;
  189. static int16_t back_up_wait;
  190. static int16_t press_up_wait;
  191. static int16_t special_press_up_wait;
  192. static int16_t front_min_val;
  193. static int16_t back_min_val;
  194. static int16_t front_max_val;
  195. static int16_t back_max_val;
  196. int16_t front_val = abs(front[2]);
  197. int16_t back_val = abs(back[2]);
  198. //滑动窗口更新数据
  199. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  200. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  201. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  202. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  203. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(int16_t));
  204. front_mag_window[WINDOW_SIZE - 1] = front_val;
  205. back_mag_window[WINDOW_SIZE - 1] = back_val;
  206. acc_x_window[WINDOW_SIZE - 1] = acc[0];
  207. acc_y_window[WINDOW_SIZE - 1] = acc[1];
  208. acc_z_window[WINDOW_SIZE - 1] = acc[2];
  209. /*
  210. * 计算稳定的状态
  211. */
  212. int16_t acc_max_val_x , acc_min_val_x ;
  213. int16_t acc_max_val_y , acc_min_val_y ;
  214. int16_t acc_max_val_z , acc_min_val_z ;
  215. //寻找加速度窗口的最大值、最小值
  216. 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);
  217. 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);
  218. 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);
  219. //判断前后脚的压力上升下降状态
  220. int back_up_trend = isLongTimeUpTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_min_val);
  221. //由于不能直接用后脚用压力来判断,那么就弄个倒计时好了
  222. if (back_up_trend == 1)
  223. {
  224. back_up_wait = 20 * SAMPLE_C;
  225. }
  226. int back_down_trend = isLongTimeDownTrend(back_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &back_max_val);
  227. //当还处于后脚压力上升的余热中,降低前脚的判断阈值
  228. int front_up_trend;
  229. //当back_up_wait 后脚跟压力上升的等待时间 大于0时候, 阈值调低为2000,否则为3000
  230. if (back_up_wait > 0)
  231. {
  232. front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD * 2, &front_min_val);
  233. }
  234. else
  235. {
  236. front_up_trend = isLongTimeUpTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD * 4, &front_min_val);
  237. }
  238. //缓慢踩地
  239. if(front_mag_window[WINDOW_SIZE - 1] - front_mag_window[WINDOW_SIZE - 1 - SAMPLE_C] >-50)
  240. {
  241. if(continue_up_min_val > front_mag_window[WINDOW_SIZE - 1])
  242. {
  243. continue_up_min_val = front_mag_window[WINDOW_SIZE - 1];
  244. }
  245. }
  246. else
  247. {
  248. continue_up_min_val = front_mag_window[WINDOW_SIZE - 1];
  249. }
  250. if (front_mag_window[WINDOW_SIZE - 1] - continue_up_min_val > MAG_THRESHHOLD * 2)
  251. {
  252. front_up_trend = 1;
  253. }
  254. //认为后脚跟触地也归为触地
  255. if (back_mag_window[WINDOW_SIZE - 1] - back_min_val > MAG_THRESHHOLD * 4)
  256. {
  257. front_up_trend = 1;
  258. }
  259. //front_down_trend 为 1时候, 意味着前脚掌压力下降
  260. int front_down_trend = isLongTimeDownTrend(front_mag_window, WINDOW_SIZE, MAG_THRESHHOLD, &front_max_val);
  261. // 自定义前后脚压力均上升,则重置压力等待时间,press_up_wait用于判断产生剧烈抖动时,则为触地
  262. // special_press_up_wait 用于判断延续触地逻辑,使其阈值变大(解决触地剧烈抖动的情况)
  263. if ((back_mag_window[WINDOW_SIZE - 1] - back_min_val > 500 && back_max_val - back_mag_window[WINDOW_SIZE - 1] < 200) ||
  264. (front_mag_window[WINDOW_SIZE - 1] - front_min_val > 500 && front_max_val - front_mag_window[WINDOW_SIZE - 1] < 200) ||
  265. (front_mag_window[WINDOW_SIZE - 1] - continue_up_min_val > 500))
  266. {
  267. press_up_wait = 20 * SAMPLE_C;
  268. special_press_up_wait = 20 * SAMPLE_C;
  269. }
  270. //发现前脚压力向下降的时候,置special_press_up_wait为0,避免离地时候,该倒计时还要生效
  271. if(front_down_trend == 1)
  272. {
  273. special_press_up_wait = 0;
  274. }
  275. //当前脚掌压力上升,后脚压力上升,则意味全脚掌着地,那就意味是真正的触地
  276. 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)
  277. {
  278. front_up_trend = 1;
  279. }
  280. //if (back_down_trend == 1 || front_down_trend == 1)
  281. //{
  282. // front_up_trend = 0;
  283. // back_up_trend = 0;
  284. //}
  285. //根据压力的上升或下降,设置零速估计状态
  286. setZeroStatus(front_up_trend, front_down_trend, &front_zero_tmp);
  287. //当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  288. if(front_zero_tmp > 0)
  289. {
  290. front_zupt_wait = 20 * SAMPLE_C;
  291. }
  292. //后脚作同样的处理
  293. //setZeroStatus(back_up_trend, back_down_trend, &back_zero_tmp);
  294. //
  295. //
  296. ////当触发了压力上升及平稳的时候,需要用加速度倒计时延续状态
  297. //if(back_zero_tmp > 0)
  298. //{
  299. // back_zupt_wait = 20 * SAMPLE_C;
  300. //}
  301. /*过滤一下类似于穿拖鞋,后鞋垫与磁力计传感器之间的距离在振荡*/
  302. //相当于脚尖开始用力了,开始要走的状态,发现后脚的状态还处于触地状态,那么认为是误判
  303. /*if( back_zero_tmp != 0 && front_down_trend)
  304. {
  305. back_zero_tmp = 0;
  306. }*/
  307. //同样的,后脚泄力了,但是前脚还处于稳定的触地状态,那么解除这个稳定状态
  308. /*if(front_zero_tmp == 2 && back_down_trend)
  309. {
  310. front_zero_tmp = 0;
  311. }*/
  312. //下面处理的刚触地时候,导致剧烈抖动,从而使触地状态不连续
  313. // var_acc_temp = var_acc_f(acc_z_window, 10 * SAMPLE_C, 1);
  314. // //寻找前后脚窗口的最大最小值
  315. // float front_mag_max_temp = find_val(front_mag_window, 0, 10 * SAMPLE_C , '>');
  316. // float front_mag_min_temp = find_val(front_mag_window, 0, 10 * SAMPLE_C , '<');
  317. // float back_mag_max_temp = find_val(back_mag_window, 0, 10 * SAMPLE_C , '>');
  318. // float back_mag_min_temp = find_val(back_mag_window, 0, 10 * SAMPLE_C , '<');
  319. //发现抖动得厉害,而且压力窗口的前半部分处于一种平缓的状态,那么认为这个触地抖动导致的
  320. //if (var_acc_temp > 0.05f && back_mag_max_temp - back_mag_min_temp > 1000)
  321. //{
  322. // press_wait = 10 * SAMPLE_C;
  323. //}
  324. //
  325. ////处于假定的触地抖动时间press_wait, 发现有压力上升,那么肯定是触地了
  326. //if (front_mag_window[WINDOW_SIZE - 1] - front_mag_window[0] > 2000 && press_wait > 0)
  327. //{
  328. // front_zero_tmp = 1;
  329. // shake_acc_wait = 20 * SAMPLE_C;
  330. //}
  331. //后脚同理
  332. // if (back_mag_window[WINDOW_SIZE - 1] - back_mag_window[0] > 1500 && press_wait > 0)
  333. // {
  334. // back_zero_tmp = 1;
  335. // shake_acc_wait = 20 * SAMPLE_C;
  336. // }
  337. //归纳上述的处理结果,并且利用acc来延续触地的判断
  338. setTouchFloorStatus(&front_zero_tmp, front_zero, acc_x_window, acc_y_window, acc_z_window);
  339. //setTouchFloorStatus(&back_zero_tmp, back_zero, acc_x_window, acc_y_window, acc_z_window);
  340. //有时候触地状态中断了,那么根据短时间内出现加速度平稳,那么意味着这个平稳状态是触地状态
  341. if((front_zupt_wait > 0 || press_up_wait > 0 ) &&
  342. (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))
  343. {
  344. *front_zero = 1;
  345. if (front_zero_tmp == 0)
  346. {
  347. front_zero_tmp = 2;
  348. }
  349. }
  350. 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];
  351. //利用特殊的压力上升等待时间和抖动来判断
  352. if(special_press_up_wait > 0 && abs(acc_norm - 4194304) < 300000)
  353. {
  354. //*front_zero = 1;
  355. if (front_zero_tmp == 0)
  356. {
  357. front_zero_tmp = 2;
  358. }
  359. }
  360. //累加平稳加速度的数目,如果超过一秒,则强制归位触地状态
  361. 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)
  362. {
  363. *acc_zero = 1;
  364. acc_zero_count ++;
  365. }
  366. else
  367. {
  368. *acc_zero = 0;
  369. acc_zero_count = 0;
  370. }
  371. // /*
  372. // * 强制大于1秒的零速均视为触地
  373. // */
  374. if(acc_zero_count > 100 * SAMPLE_C)
  375. {
  376. *front_zero = 1;
  377. front_zero_tmp = 1;
  378. }
  379. //利用加速度延续
  380. int16_t continue_thresh = 103;
  381. if(special_press_up_wait > 0 )
  382. {
  383. continue_thresh = 1030;
  384. }
  385. // if (shake_acc_wait > 0)
  386. // {
  387. // continue_thresh = 1030;
  388. // }else if(MT_GetState())
  389. // {
  390. // //震动,导致加速度数据产生抖动
  391. // continue_thresh = 250;
  392. // }
  393. if(last_front_zupt == 1
  394. && abs(acc_x_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_x_window[WINDOW_SIZE - 1]) < continue_thresh
  395. && abs(acc_y_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_y_window[WINDOW_SIZE - 1]) < continue_thresh
  396. && abs(acc_z_window[WINDOW_SIZE - 1 - SAMPLE_C] - acc_z_window[WINDOW_SIZE - 1]) < continue_thresh)
  397. {
  398. *front_zero = 1;
  399. if (front_zero_tmp == 0)
  400. {
  401. front_zero_tmp = 2;
  402. }
  403. }
  404. //各种倒计时
  405. if(front_zupt_wait > 0)
  406. {
  407. front_zupt_wait --;
  408. }
  409. if (press_wait > 0)
  410. {
  411. press_wait--;
  412. }
  413. if (shake_acc_wait > 0)
  414. {
  415. shake_acc_wait--;
  416. }
  417. if (back_up_wait > 0)
  418. {
  419. back_up_wait--;
  420. }
  421. if (press_up_wait > 0)
  422. {
  423. press_up_wait--;
  424. }
  425. if (*front_zero)
  426. {
  427. front_zupt_wait = 20 * SAMPLE_C;
  428. }
  429. if(special_press_up_wait > 0)
  430. {
  431. special_press_up_wait --;
  432. }
  433. last_front_zupt = *front_zero;
  434. }