press_down_detect.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "press_down_detect.h"
  2. int press_down_detect_new(int index, uint16_t left_press, uint16_t right_press, float left_acc_z, float right_acc_z,
  3. float pitch_left, float pitch_right, uint8_t _rssi)
  4. {
  5. static int16_t valid_count = 0;
  6. if(fabsf(left_acc_z - 1.0f) < 0.3f && fabsf(right_acc_z - 1.0f) < 0.3f)
  7. {
  8. valid_count ++;
  9. }
  10. else
  11. {
  12. valid_count = 0;
  13. }
  14. int16_t valid_laser = 0;
  15. if(_rssi < 30)
  16. {
  17. if((left_press < 800 && left_press > 100) || (right_press > 100 && right_press < 800))
  18. {
  19. valid_laser = 1;
  20. }
  21. }
  22. else
  23. {
  24. if((left_press < 800 && left_press > 100) && (right_press > 100 && right_press < 800))
  25. {
  26. valid_laser = 1;
  27. }
  28. }
  29. if(valid_count >= 10 && valid_laser == 1)
  30. return 1;
  31. return 0;
  32. }
  33. int cancel_down = 0;
  34. int virus_flag = 0;
  35. float real_max_val_right = 0.0f;
  36. float real_max_val_left = 0.0f;
  37. float real_max_front_left = 0.0f;
  38. float real_max_front_right = 0.0f;
  39. int press_down_detect_press(int index, float front_mag_left, float back_mag_left,
  40. float front_mag_right, float back_mag_right, int *front_down, int *back_down)
  41. {
  42. static float front_mag_norm_left[PRESS_COUNT_MAX];
  43. static float front_mag_norm_right[PRESS_COUNT_MAX];
  44. static float back_mag_norm_left[PRESS_COUNT_MAX];
  45. static float back_mag_norm_right[PRESS_COUNT_MAX];
  46. memcpy(front_mag_norm_left, front_mag_norm_left + 1, (PRESS_COUNT_MAX - 1) * sizeof(float));
  47. front_mag_norm_left[PRESS_COUNT_MAX - 1] = front_mag_left;
  48. memcpy(back_mag_norm_left, back_mag_norm_left + 1, (PRESS_COUNT_MAX - 1) * sizeof(float));
  49. back_mag_norm_left[PRESS_COUNT_MAX - 1] = back_mag_left;
  50. memcpy(front_mag_norm_right, front_mag_norm_right + 1, (PRESS_COUNT_MAX - 1) * sizeof(float));
  51. front_mag_norm_right[PRESS_COUNT_MAX - 1] = front_mag_right;
  52. memcpy(back_mag_norm_right, back_mag_norm_right + 1, (PRESS_COUNT_MAX - 1) * sizeof(float));
  53. back_mag_norm_right[PRESS_COUNT_MAX - 1] = back_mag_right;
  54. int front_down_tmp = 0;
  55. int back_down_tmp = 0;
  56. float left_max_front_val = front_mag_left;
  57. float left_min_front_val = front_mag_left;
  58. int left_min_front_index = PRESS_COUNT_MAX - 1;
  59. int left_max_front_index = PRESS_COUNT_MAX - 1;
  60. float left_max_back_val = back_mag_left;
  61. float left_min_back_val = back_mag_left;
  62. int left_min_back_index = PRESS_COUNT_MAX - 1;
  63. int left_max_back_index = PRESS_COUNT_MAX - 1;
  64. float right_max_front_val = front_mag_right;
  65. float right_min_front_val = front_mag_right;
  66. int right_min_front_index = PRESS_COUNT_MAX - 1;
  67. int right_max_front_index = PRESS_COUNT_MAX - 1;
  68. float right_max_back_val = back_mag_right;
  69. float right_min_back_val = back_mag_right;
  70. int right_min_back_index = PRESS_COUNT_MAX - 1;
  71. int right_max_back_index = PRESS_COUNT_MAX - 1;
  72. for(int i = 0; i < PRESS_COUNT_MAX; i++)
  73. {
  74. if(front_mag_norm_left[i] > left_max_front_val)
  75. {
  76. left_max_front_val = front_mag_norm_left[i];
  77. left_max_front_index = i;
  78. }
  79. if(front_mag_norm_left[i] < left_min_front_val)
  80. {
  81. left_min_front_val = front_mag_norm_left[i];
  82. left_min_front_index = i;
  83. }
  84. if(back_mag_norm_left[i] > left_max_back_val)
  85. {
  86. left_max_back_val = back_mag_norm_left[i];
  87. left_max_back_index = i;
  88. }
  89. if(back_mag_norm_left[i] < left_min_back_val)
  90. {
  91. left_min_back_val = back_mag_norm_left[i];
  92. left_min_back_index = i;
  93. }
  94. if(front_mag_norm_right[i] > right_max_front_val)
  95. {
  96. right_max_front_val = front_mag_norm_right[i];
  97. right_max_front_index = i;
  98. }
  99. if(front_mag_norm_right[i] < right_min_front_val)
  100. {
  101. right_min_front_val = front_mag_norm_right[i];
  102. right_min_front_index = i;
  103. }
  104. if(back_mag_norm_right[i] > right_max_back_val)
  105. {
  106. right_max_back_val = back_mag_norm_right[i];
  107. right_max_back_index = i;
  108. }
  109. if(back_mag_norm_right[i] < right_min_back_val)
  110. {
  111. right_min_back_val = back_mag_norm_right[i];
  112. right_min_back_index = i;
  113. }
  114. }
  115. //判断前蹲
  116. /*
  117. if(left_max_front_index < left_min_front_index && left_max_front_val > left_min_front_val + 250.0f
  118. && right_max_front_index < right_min_front_index && right_max_front_val > right_min_front_val + 250.0f
  119. && abs(right_min_front_index - left_min_front_index) < 4)
  120. {
  121. front_down_tmp = 1;
  122. }
  123. */
  124. if(left_max_front_index < left_min_front_index && left_max_front_val > left_min_front_val + 100.0f)
  125. {
  126. real_max_front_left = real_max_front_left > left_max_front_val ? real_max_front_left : left_max_front_val;
  127. }
  128. else
  129. {
  130. real_max_front_left = 0.f;
  131. }
  132. if(right_max_front_index < right_min_front_index && right_max_front_val > right_min_front_val + 100.0f)
  133. {
  134. real_max_front_right = real_max_front_right > right_max_front_val ? real_max_front_right : right_max_front_val;
  135. }
  136. else
  137. {
  138. real_max_front_right = 0.f;
  139. }
  140. if(real_max_front_left - left_min_front_val > 200.f && real_max_front_right - right_min_front_val > 200)
  141. {
  142. front_down_tmp = 1;
  143. }
  144. //判断后蹲
  145. if(left_max_back_index < left_min_back_index && left_max_back_val > left_min_back_val + 150.0f
  146. && right_max_back_index < right_min_back_index && right_max_back_val > right_min_back_val + 150.0f
  147. && left_max_back_val < 3100.f && right_max_back_val < 3100.f)
  148. {
  149. back_down_tmp = 1;
  150. }
  151. /*
  152. * 取消前蹲引发的后蹲判断
  153. */
  154. if(left_max_back_index < left_min_back_index && left_max_back_val > left_min_back_val + 100.0f)
  155. {
  156. real_max_val_left = left_max_back_val > real_max_val_left ? left_max_back_val : real_max_val_left;
  157. }
  158. else
  159. {
  160. real_max_val_left = 0.0f;
  161. }
  162. if(right_max_back_index < right_min_back_index && right_max_back_val > right_min_back_val + 100.0f)
  163. {
  164. real_max_val_right = right_max_back_val > real_max_val_right ? right_max_back_val : real_max_val_right;
  165. }
  166. else
  167. {
  168. real_max_val_right = 0.0f;
  169. }
  170. if(real_max_val_right > 3100.0f && real_max_val_left > 3100.0f)
  171. {
  172. back_down_tmp = 0;
  173. }
  174. *front_down = front_down_tmp;
  175. *back_down = back_down_tmp;
  176. return 0;
  177. }
  178. //short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  179. //{
  180. // static int last_h_z;
  181. // static int last_s_z;
  182. // static int left_up;
  183. // static int right_up;
  184. //
  185. // static int left_up_count;
  186. // static int right_up_count;
  187. //
  188. // if(h_pos[2] - last_h_z > 0)
  189. // {
  190. // left_up = 1;
  191. // }
  192. // else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  193. // {
  194. // left_up = -1;
  195. // }
  196. //
  197. // if(left_up == 1)
  198. // {
  199. // left_up_count ++;
  200. // }
  201. // else
  202. // {
  203. // left_up_count = 0;
  204. // }
  205. //
  206. // if(s_pos[2] - last_s_z > 0)
  207. // {
  208. // right_up = 1;
  209. // }
  210. // else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  211. // {
  212. // right_up = -1;
  213. // }
  214. //
  215. // if(right_up == 1)
  216. // {
  217. // right_up_count ++;
  218. // }
  219. // else
  220. // {
  221. // right_up_count = 0;
  222. // }
  223. //
  224. // last_h_z = h_pos[2];
  225. // last_s_z = s_pos[2];
  226. //
  227. // if(left_up == 1 && right_up == 1 && right_up_count < 30 && left_up_count < 30
  228. // && right_up_count > 2 && left_up_count > 2 && s_pos[2] < 5 && h_pos[2] < 5 &&
  229. // s_pos[2] > 1 && h_pos[2] > 1)
  230. // {
  231. // return 1;
  232. // }
  233. //
  234. // return 0;
  235. //
  236. //
  237. //}
  238. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  239. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  240. {
  241. *max_val = data[0];
  242. *max_index = 0;
  243. *min_val = data[0];
  244. *min_index = 0;
  245. for(int i = 0; i < length; i++)
  246. {
  247. if(*max_val < data[i])
  248. {
  249. *max_val = data[i];
  250. *max_index = i;
  251. }
  252. if(*min_val > data[i])
  253. {
  254. *min_val = data[i];
  255. *min_index = i;
  256. }
  257. }
  258. }
  259. short press_jump_detect(float left_acc_z, float right_acc_z)
  260. {
  261. static float right_data_z_buff[10];
  262. static float left_data_z_buff[10];
  263. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  264. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  265. right_data_z_buff[9] = right_acc_z;
  266. left_data_z_buff[9] = left_acc_z;
  267. float max_val_right, min_val_right, max_val_left, min_val_left;
  268. int max_index_right, min_index_right, max_index_left, min_index_left;
  269. max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  270. max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  271. if(max_index_right < min_index_right && max_index_left < min_index_left &&
  272. max_val_right - min_val_right > 1.0f && max_val_left - min_val_left > 1.0f &&
  273. min_val_left < 0.2f && min_val_right < 0.2f && max_val_left > 1.5f && max_val_right > 1.5f &&
  274. fabsf(max_val_right - max_val_left) < 2.0f && fabsf(min_val_right - min_val_left) < 2.0f &&
  275. abs(max_index_right- max_index_left) < 4 && abs(min_index_right- min_index_left) < 4
  276. )
  277. {
  278. return 1;
  279. }
  280. return 0;
  281. }