detect_zero_vel.c 12 KB

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