detect_step_by_acc_gyr.c 8.2 KB

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