detect_zero_vel.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "detect_zero_vel.h"
  2. int front_zero_tmp = 0;
  3. int back_zero_tmp = 0;
  4. void detect_zero_vel(int16_t front, int16_t back, int16_t acc[3],
  5. int16_t *front_zero, int16_t *back_zero, int16_t *acc_zero)
  6. {
  7. static float front_mag_window[WINDOW_SIZE];
  8. static float back_mag_window[WINDOW_SIZE];
  9. static float acc_x_window[WINDOW_SIZE];
  10. static float acc_y_window[WINDOW_SIZE];
  11. static float acc_z_window[WINDOW_SIZE];
  12. static int last_front_zupt;
  13. static int last_back_zupt;
  14. static int front_zupt_wait;
  15. static int back_zupt_wait;
  16. float front_val = front;
  17. float back_val = back;
  18. float acc_val = acc[2] / 2048.f;
  19. //滑动窗口更新数据
  20. memcpy(front_mag_window, front_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  21. memcpy(back_mag_window, back_mag_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  22. memcpy(acc_x_window, acc_x_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  23. memcpy(acc_y_window, acc_y_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  24. memcpy(acc_z_window, acc_z_window + 1, (WINDOW_SIZE - 1) * sizeof(float));
  25. front_mag_window[WINDOW_SIZE - 1] = front_val;
  26. back_mag_window[WINDOW_SIZE - 1] = back_val;
  27. acc_x_window[WINDOW_SIZE - 1] = acc[0] / 2048.f;
  28. acc_y_window[WINDOW_SIZE - 1] = acc[1] / 2048.f;
  29. acc_z_window[WINDOW_SIZE - 1] = acc[2] / 2048.f;
  30. float front_max_val = front_val;
  31. float front_min_val = front_val;
  32. uint16_t front_max_index = WINDOW_SIZE - 1;
  33. uint16_t front_min_index = WINDOW_SIZE - 1;
  34. float back_max_val = back_val;
  35. float back_min_val = back_val;
  36. uint16_t back_max_index = WINDOW_SIZE - 1;
  37. uint16_t back_min_index = WINDOW_SIZE - 1;
  38. for(int i = 0 ; i < WINDOW_SIZE; i++ )
  39. {
  40. if(front_mag_window[i] > front_max_val)
  41. {
  42. front_max_val = front_mag_window[i];
  43. front_max_index = i;
  44. }
  45. if(front_mag_window[i] < front_min_val)
  46. {
  47. front_min_val = front_mag_window[i];
  48. front_min_index = i;
  49. }
  50. if(back_mag_window[i] > back_max_val)
  51. {
  52. back_max_val = back_mag_window[i];
  53. back_max_index = i;
  54. }
  55. if(back_mag_window[i] < back_min_val)
  56. {
  57. back_min_val = back_mag_window[i];
  58. back_min_index = i;
  59. }
  60. }
  61. /*
  62. * 计算稳定的状态
  63. */
  64. float acc_max_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  65. float acc_min_val_x = acc_x_window[WINDOW_SIZE - 1] ;
  66. float acc_max_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  67. float acc_min_val_y = acc_y_window[WINDOW_SIZE - 1] ;
  68. float acc_max_val_z = acc_z_window[WINDOW_SIZE - 1];
  69. float acc_min_val_z = acc_z_window[WINDOW_SIZE - 1];
  70. for(int i = WINDOW_SIZE - 5 ; i < WINDOW_SIZE; i++ )
  71. {
  72. if(acc_x_window[i] < acc_min_val_x)
  73. {
  74. acc_min_val_x = acc_x_window[i];
  75. }
  76. if(acc_x_window[i] > acc_max_val_x)
  77. {
  78. acc_max_val_x = acc_x_window[i];
  79. }
  80. if(acc_y_window[i] < acc_min_val_y)
  81. {
  82. acc_min_val_y = acc_y_window[i];
  83. }
  84. if(acc_y_window[i] > acc_max_val_y)
  85. {
  86. acc_max_val_y = acc_y_window[i];
  87. }
  88. if(acc_z_window[i] < acc_min_val_z)
  89. {
  90. acc_min_val_z = acc_z_window[i];
  91. }
  92. if(acc_z_window[i] > acc_max_val_z)
  93. {
  94. acc_max_val_z = acc_z_window[i];
  95. }
  96. }
  97. // if(acc_max_val - acc_min_val < 0.08f)
  98. // {
  99. // acc_zero_tmp = 1;
  100. // }
  101. /*
  102. * 判断前脚掌往下压
  103. */
  104. if(front_max_index < front_min_index && front_max_val > front_min_val + 200.f)
  105. {
  106. front_zero_tmp = 1;
  107. front_zupt_wait = 10;
  108. }
  109. else if(front_max_index > front_min_index && front_max_val > front_min_val + 200.f)
  110. {
  111. front_zero_tmp = 0;
  112. }
  113. else if(front_zero_tmp > 0)
  114. {
  115. front_zero_tmp = 2;
  116. }
  117. /*
  118. * 判断后脚往下压
  119. */
  120. if(back_max_index < back_min_index && back_max_val > back_min_val + 200.f)
  121. {
  122. back_zero_tmp = 1;
  123. back_zupt_wait = 10;
  124. }
  125. else if(back_max_index > back_min_index && back_max_val > back_min_val + 200.f)
  126. {
  127. back_zero_tmp = 0;
  128. }
  129. else if(back_zero_tmp > 0)
  130. {
  131. back_zero_tmp = 2;
  132. }
  133. /*
  134. * 判断仅前踮 或 后垫
  135. */
  136. if(back_zero_tmp == 2 && front_zero_tmp == 0)
  137. {
  138. back_zero_tmp = 0;
  139. }
  140. if(front_zero_tmp == 2 && back_zero_tmp == 0)
  141. {
  142. front_zero_tmp = 0;
  143. }
  144. if((back_max_index > back_min_index && back_max_val > back_min_val + 500.f))
  145. {
  146. front_zero_tmp = 0;
  147. front_zupt_wait = 0;
  148. if((front_max_index > front_min_index && front_max_val > front_min_val + 500.f))
  149. {
  150. back_zero_tmp = 0;
  151. back_zupt_wait = 0;
  152. }
  153. }
  154. if(front_zero_tmp == 1)
  155. {
  156. *front_zero = 1;
  157. }
  158. else if(front_zupt_wait > 0 && acc_max_val_x - acc_min_val_x < 0.025f && acc_max_val_y - acc_min_val_y < 0.025f )
  159. {
  160. *front_zero = 1;
  161. front_zupt_wait = 10;
  162. }
  163. else
  164. {
  165. *front_zero = 0;
  166. }
  167. // if(*back_zero == 0 && back_zupt_wait > 0 && acc_max_val - acc_min_val < 0.08f)
  168. // {
  169. // *back_zero = 1;
  170. // }
  171. if(back_zero_tmp == 1)
  172. {
  173. *back_zero = 1;
  174. }
  175. else if(back_zupt_wait > 0 && acc_max_val_x - acc_min_val_x < 0.025f && acc_max_val_y - acc_min_val_y < 0.025f )
  176. {
  177. *back_zero = 1;
  178. back_zupt_wait = 10;
  179. }
  180. else
  181. {
  182. *back_zero = 0;
  183. }
  184. for(int i = 0 ; i < WINDOW_SIZE - 5; i++ )
  185. {
  186. if(acc_x_window[i] < acc_min_val_x)
  187. {
  188. acc_min_val_x = acc_x_window[i];
  189. }
  190. if(acc_x_window[i] > acc_max_val_x)
  191. {
  192. acc_max_val_x = acc_x_window[i];
  193. }
  194. if(acc_y_window[i] < acc_min_val_y)
  195. {
  196. acc_min_val_y = acc_y_window[i];
  197. }
  198. if(acc_y_window[i] > acc_max_val_y)
  199. {
  200. acc_max_val_y = acc_y_window[i];
  201. }
  202. if(acc_z_window[i] < acc_min_val_z)
  203. {
  204. acc_min_val_z = acc_z_window[i];
  205. }
  206. if(acc_z_window[i] > acc_max_val_z)
  207. {
  208. acc_max_val_z = acc_z_window[i];
  209. }
  210. }
  211. 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)
  212. {
  213. *acc_zero = 1;
  214. }
  215. else
  216. {
  217. *acc_zero = 0;
  218. }
  219. if(*front_zero == 0)
  220. {
  221. *front_zero = *back_zero;
  222. }
  223. if(front_zupt_wait > 0)
  224. {
  225. front_zupt_wait --;
  226. }
  227. if(back_zupt_wait > 0)
  228. {
  229. back_zupt_wait --;
  230. }
  231. //利用加速度延续
  232. if((last_front_zupt == 1 || last_back_zupt == 1)
  233. && fabsf(acc_x_window[WINDOW_SIZE - 2] - acc_x_window[WINDOW_SIZE - 1]) < 0.015f
  234. && fabsf(acc_y_window[WINDOW_SIZE - 2] - acc_y_window[WINDOW_SIZE - 1]) < 0.015f)
  235. {
  236. *front_zero = 1;
  237. *back_zero = 1;
  238. front_zero_tmp = 1;
  239. back_zero_tmp = 1;
  240. front_zupt_wait = 10;
  241. back_zupt_wait = 10;
  242. }
  243. last_front_zupt = front_zero_tmp;
  244. last_back_zupt = back_zero_tmp;
  245. }