detect_step_by_acc_gyr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "detect_step_by_acc_gyr.h"
  2. static int ACC_WINDOW_SIZE = 15;
  3. static float acc_y_buff[15];
  4. static float acc_z_buff[15];
  5. static float acc_x_buff[15];
  6. static float gyr_y_buff[15];
  7. //static int acc_y_wait;
  8. //static int acc_z_wait;
  9. //static int acc_x_wait;
  10. static int acc_y_status = 1;
  11. static int acc_z_status = 1;
  12. static int acc_x_status = 1;
  13. static int last_zupt;
  14. static int last_acc_x_status;
  15. static int last_acc_y_status;
  16. static int last_acc_z_status;
  17. //static int can_detect_zupt = 1;
  18. //static int stop_out_zupt = 0;
  19. static int acc_diff_wait_count = 0;
  20. static int zupt_wait_cout = 0;
  21. static int special_acc_wait_time = 0;
  22. int detect_turning_point(float *data_buff, int length, int last_status)
  23. {
  24. //1、寻找窗口的最大值的下标
  25. int max_index = 0;
  26. float max_val = data_buff[0];
  27. for(int i = 0; i < length; i++)
  28. {
  29. if(max_val < data_buff[i])
  30. {
  31. max_index = i;
  32. max_val = data_buff[i];
  33. }
  34. }
  35. //2、以最大值下标把窗口两部分,右边部分判断出骤减及减缓趋势的时候,则判断为触地
  36. int right_length = length - max_index - 1;
  37. if(right_length > 3)
  38. {
  39. //当窗口长度大于5的时候,则进行趋势判断, 再对半分窗口
  40. int right_index = max_index;
  41. //寻找最小值,确定中间点
  42. int mid_index = max_index;
  43. float min_val = data_buff[right_index];
  44. for(int i = max_index; i < length; i++)
  45. {
  46. if(min_val > data_buff[i])
  47. {
  48. min_val = data_buff[i];
  49. mid_index = i;
  50. }
  51. }
  52. float right_diff_sum = data_buff[right_index] - data_buff[mid_index];
  53. float max_right_diff = 0.0f;
  54. for(int i = right_index + 1; i <= mid_index ; i++)
  55. {
  56. if(data_buff[i-1] - data_buff[i] > max_right_diff)
  57. {
  58. max_right_diff = data_buff[i-1] - data_buff[i];
  59. }
  60. }
  61. float left_diff_sum = 0.0f;
  62. for(int i = mid_index + 1; i < length; i++)
  63. {
  64. left_diff_sum += fabsf(data_buff[i] - data_buff[i-1]);
  65. }
  66. //if(right_diff_sum > 3 * left_diff_sum && max_val > data_buff[length - 1] + 0.5f)
  67. SEGGER_RTT_printf(0, "right_diff_sum : %d, left_diff_sum : %d!\n", (int)(right_diff_sum * 1000), (int)(left_diff_sum * 1000));
  68. //衰减过于激烈,肯定为触发条件
  69. if(right_index + 2 < length && data_buff[right_index] - data_buff[right_index + 2] > 1.5f && data_buff[length-1] - min_val < 0.2f)
  70. {
  71. return 1;
  72. }
  73. if(right_diff_sum > 5 * left_diff_sum && max_val > data_buff[length - 1] + 0.8f && mid_index < length - 2 &&
  74. max_right_diff > 0.3f)
  75. {
  76. return 1;
  77. }
  78. }
  79. if(last_status == 1 && max_val > data_buff[length - 1] + 0.5f)
  80. {
  81. return 1;
  82. }
  83. float max_val_right = data_buff[length - 1];
  84. float min_val_right = data_buff[length - 1];
  85. for(int i = length - 6; i < length - 1; i++)
  86. {
  87. if(max_val_right < data_buff[i])
  88. {
  89. max_val_right = data_buff[i];
  90. }
  91. if(min_val_right > data_buff[i])
  92. {
  93. min_val_right = data_buff[i];
  94. }
  95. }
  96. if(last_status == 1 && max_val_right - min_val_right < 0.15f)
  97. {
  98. return 1;
  99. }
  100. for(int i = 0; i < length - 6; i++)
  101. {
  102. if(max_val_right < data_buff[i])
  103. {
  104. max_val_right = data_buff[i];
  105. }
  106. if(min_val_right > data_buff[i])
  107. {
  108. min_val_right = data_buff[i];
  109. }
  110. }
  111. if( max_val_right - min_val_right < 0.1f)
  112. {
  113. return 2;
  114. }
  115. return 0;
  116. }
  117. float var_acc_2(float *acc, int length)
  118. {
  119. if(length < 10)
  120. {
  121. return 0.0f;
  122. }
  123. float mean_x = 0.0f;
  124. float sum_x = 0.0f;
  125. for(int i = length - 10; i < length; i++)
  126. {
  127. sum_x += acc[i];
  128. }
  129. mean_x = sum_x * 0.1f;
  130. sum_x = 0.0f;
  131. for(int i = length - 10; i < length; i++)
  132. {
  133. sum_x += ((acc[i]- mean_x) * (acc[i]- mean_x));
  134. }
  135. return sum_x * 0.1f;
  136. }
  137. int detect_turn_point_by_gyr()
  138. {
  139. int min_index = 0;
  140. int max_index = 0;
  141. float max_val = gyr_y_buff[0];
  142. float min_val = gyr_y_buff[0];
  143. for(int i = 0; i < ACC_WINDOW_SIZE; i++)
  144. {
  145. if(min_val > gyr_y_buff[i])
  146. {
  147. min_val = gyr_y_buff[i];
  148. min_index = i;
  149. }
  150. if(max_val < gyr_y_buff[i])
  151. {
  152. max_val = gyr_y_buff[i];
  153. max_index = i;
  154. }
  155. }
  156. if(max_index < min_index && max_val > min_val + 2.0f && gyr_y_buff[10] - min_val < 0.5f &&
  157. gyr_y_buff[14] - min_val < 0.5f && max_val > 2.0f && min_val > -0.5f)
  158. {
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. int special_detect_acc_down()
  164. {
  165. int min_index = ACC_WINDOW_SIZE-10;
  166. int max_index = ACC_WINDOW_SIZE-10;
  167. float max_val = acc_z_buff[min_index];
  168. float min_val = acc_z_buff[min_index];
  169. for(int i = ACC_WINDOW_SIZE-10; i < ACC_WINDOW_SIZE; i++)
  170. {
  171. if(min_val > acc_z_buff[i])
  172. {
  173. min_val = acc_z_buff[i];
  174. min_index = i;
  175. }
  176. if(max_val < acc_z_buff[i])
  177. {
  178. max_val = acc_z_buff[i];
  179. max_index = i;
  180. }
  181. }
  182. if(max_index > min_index && max_val > min_val + 1.0f && min_val < 0.5f &&max_val > 1.2f)
  183. {
  184. special_acc_wait_time = 20;
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. void step_detect_by_acc_gyr(float *acc, float *gyr, int *x_zero, int *y_zero, int *z_zero, int *zupt)
  190. {
  191. //缓存加速度的滑动窗口
  192. memcpy(acc_x_buff, acc_x_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  193. acc_x_buff[(ACC_WINDOW_SIZE - 1)] = fabsf(acc[0]);
  194. memcpy(acc_y_buff, acc_y_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  195. acc_y_buff[(ACC_WINDOW_SIZE - 1)] = fabsf(acc[1]);
  196. memcpy(acc_z_buff, acc_z_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  197. acc_z_buff[(ACC_WINDOW_SIZE - 1)] = acc[2];
  198. memcpy(gyr_y_buff, gyr_y_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  199. gyr_y_buff[(ACC_WINDOW_SIZE - 1)] = gyr[1] * 0.0175f;
  200. //检测拐点
  201. // int acc_y_zero, acc_z_zero , acc_x_zero;
  202. acc_y_status = detect_turning_point(acc_y_buff, ACC_WINDOW_SIZE, acc_y_status);
  203. acc_z_status = detect_turning_point(acc_z_buff, ACC_WINDOW_SIZE, acc_z_status);
  204. acc_x_status = detect_turning_point(acc_x_buff, ACC_WINDOW_SIZE, acc_x_status);
  205. if(acc_x_status == 2 || acc_y_status == 2 || acc_z_status == 2)
  206. {
  207. if(acc_x_status == 1 || acc_y_status == 1 || acc_z_status == 1)
  208. {
  209. if(acc_x_status == 2)
  210. {
  211. acc_x_status = 1;
  212. }
  213. if(acc_y_status == 2)
  214. {
  215. acc_y_status = 1;
  216. }
  217. if(acc_z_status == 2)
  218. {
  219. acc_z_status = 1;
  220. }
  221. }
  222. }
  223. // if(sqrt(gyr[0] * gyr[0] + gyr[1] * gyr[1] + gyr[2] * gyr[2]) > 1.5f* 57.2958f)
  224. //// if(fabs(sqrt(acc[0] * acc[0] + acc[1] * acc[1] + acc[2] * acc[2]) - 1.0f) > 0.5f )
  225. //触底室,一般都不会X Y Z轴不会出现低概率的值
  226. // if(acc[2] < 0.7f || acc[1] < -0.8f || acc[0] < -0.8f)
  227. // {
  228. // acc_x_status = 0;
  229. // acc_y_status = 0;
  230. // acc_z_status = 0;
  231. // }
  232. //
  233. //经验值过滤, 但是发现触地的时候出现很大抖动,需要知道ACC的最大值
  234. float max_acc = acc_z_buff[ACC_WINDOW_SIZE - 10];
  235. int max_index = ACC_WINDOW_SIZE - 10;
  236. float min_acc = acc_z_buff[ACC_WINDOW_SIZE - 10];
  237. int min_index = ACC_WINDOW_SIZE - 10;
  238. for(int i = ACC_WINDOW_SIZE - 10; i < ACC_WINDOW_SIZE; i++)
  239. {
  240. if(acc_z_buff[i] > max_acc)
  241. {
  242. max_acc = acc_z_buff[i];
  243. max_index = i;
  244. }
  245. if(acc_z_buff[i] < min_acc)
  246. {
  247. min_acc = acc_z_buff[i];
  248. min_index = i;
  249. }
  250. }
  251. if(max_index == ACC_WINDOW_SIZE - 1 && max_acc - min_acc > 3.0f)
  252. {
  253. acc_diff_wait_count = 15;
  254. }
  255. //标志垫着脚跑的变量
  256. int special_zupt = 0;
  257. if((var_acc_2(acc_z_buff, ACC_WINDOW_SIZE) > 0.5f && acc[2] > 0.5f) || detect_turn_point_by_gyr())
  258. {
  259. acc_x_status = 1;
  260. acc_y_status = 1;
  261. acc_z_status = 1;
  262. special_zupt = 1;
  263. }
  264. //判断普通跑步的变量
  265. if(special_zupt == 0 &&((acc[2] < 0.7f && acc_diff_wait_count == 0) || acc[2] < 0 || sqrt(gyr[0]*gyr[0] + gyr[1]*gyr[1] + gyr[2]*gyr[2])* 0.0175f > 1.2f))
  266. {
  267. acc_x_status = 0;
  268. acc_y_status = 0;
  269. acc_z_status = 0;
  270. }
  271. //一般都是在剧烈抖动踩触发的踩,但是会出现断开的现象,添加Z轴加速度下降及平缓来避免这种断开的现象
  272. special_detect_acc_down();
  273. if(acc[2] > 1.0f && special_acc_wait_time > 0)
  274. {
  275. acc_x_status = 1;
  276. acc_y_status = 1;
  277. acc_z_status = 1;
  278. }
  279. if(special_acc_wait_time > 0)
  280. {
  281. special_acc_wait_time --;
  282. }
  283. if(acc_diff_wait_count > 0 )
  284. {
  285. acc_diff_wait_count --;
  286. }
  287. if((last_acc_x_status == 1 && acc_x_status == 0) || (last_acc_y_status == 1 && acc_y_status == 0))
  288. {
  289. *zupt = 0;
  290. }
  291. if((last_acc_x_status == 0 && acc_x_status == 1) || (last_acc_y_status == 0 && acc_y_status == 1))
  292. {
  293. *zupt = 1;
  294. zupt_wait_cout = ACC_WINDOW_SIZE;
  295. }
  296. if((acc_x_status == 1 && acc_y_status == 1) ||((last_zupt == 1 && (acc_x_status == 1 || acc_y_status == 1))))
  297. {
  298. *zupt = 1;
  299. zupt_wait_cout = ACC_WINDOW_SIZE;
  300. }
  301. if(zupt_wait_cout > 0)
  302. {
  303. zupt_wait_cout --;
  304. }
  305. last_acc_x_status = acc_x_status;
  306. last_acc_y_status = acc_y_status;
  307. last_acc_z_status = acc_z_status;
  308. *x_zero = acc_x_status;
  309. *y_zero = acc_y_status;
  310. *z_zero = acc_z_status;
  311. }