detect_zero_vel.c 11 KB

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