press_down_detect.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "press_down_detect.h"
  2. int left_press_data[10];
  3. int right_press_data[10];
  4. int left_up_count;
  5. int right_up_count;
  6. int mean_press_data[PRESS_LEGTH];
  7. int mean_min_val = 20000000;
  8. int mean_max_val = 0;
  9. int mean_up = 0;
  10. int mean_up_true = 0;
  11. int last_mean_min_val = 0;
  12. int right_min_val = 20000000;
  13. int right_max_val = 0;
  14. int left_min_val = 20000000;
  15. int left_max_val = 0;
  16. int maybe_down = 0;
  17. int tail_count = 0;
  18. int last_down_tag = 0;
  19. int left_press_min_val = 20000000;
  20. int right_press_min_val = 20000000;
  21. int left_press_max_val = 0;
  22. int right_press_max_val = 0;
  23. int left_press_sum = 0;
  24. int right_press_sum = 0;
  25. int press_count = 0;
  26. int left_press_low = LEFT_PRESS_LOW;
  27. int right_press_low = RIGHT_PRESS_LOW;
  28. uint32_t jump_count;
  29. uint32_t down_count;
  30. int last_jump_stage;
  31. uint32_t get_jump_count(void)
  32. {
  33. return jump_count;
  34. }
  35. uint32_t get_down_count(void)
  36. {
  37. return down_count;
  38. }
  39. int mid_data(int* press_data, int length)
  40. {
  41. //ðÅÝÅÅÐò
  42. int temp = press_data[0];
  43. for (int i = 0; i < length; i++)
  44. for (int j = i; j < length; j++)
  45. {
  46. if (press_data[j] < press_data[i])
  47. {
  48. temp = press_data[i];
  49. press_data[i] = press_data[j];
  50. press_data[j] = temp;
  51. }
  52. }
  53. return press_data[length / 2 - 1];
  54. }
  55. int min_press_window(int *window, int length)
  56. {
  57. int min_window_val = window[0];
  58. for (int i = 0; i < length; i++)
  59. {
  60. if (min_window_val > window[i])
  61. {
  62. min_window_val = window[i];
  63. }
  64. }
  65. return min_window_val;
  66. }
  67. int max_press_window(int *window, int length)
  68. {
  69. int max_window_val = window[0];
  70. for (int i = 0; i < length; i++)
  71. {
  72. if (max_window_val < window[i])
  73. {
  74. max_window_val = window[i];
  75. }
  76. }
  77. return max_window_val;
  78. }
  79. int poloar_trend(int index, int *window, int window_size, int *stage, int *last_top,
  80. int *last_down, int *now_top, int *now_down,int *count, int *min_count)
  81. {
  82. int max_val = window[(index - 1) % window_size];
  83. int max_index = index;
  84. int min_val = max_val;
  85. int min_index = index;
  86. for (int i = 0; i < window_size; i++)
  87. {
  88. int temp = window[(index - i - 1) % window_size];
  89. if (max_val < temp)
  90. {
  91. max_val = temp;
  92. max_index = index - i;
  93. }
  94. if (min_val > temp)
  95. {
  96. min_val = temp;
  97. min_index = index - i;
  98. }
  99. }
  100. if (max_index < min_index)
  101. {
  102. if (*stage == 2)
  103. {
  104. *last_down = 200000000;
  105. *last_top = 0;
  106. }
  107. if (*last_down > min_val)
  108. {
  109. *last_down = min_val;
  110. }
  111. if (*last_top < max_val)
  112. {
  113. *last_top = max_val;
  114. }
  115. *stage = 1;
  116. *count = 0;
  117. *min_count = 0;
  118. }
  119. else if (max_index > min_index)
  120. {
  121. if (*stage == 1)
  122. {
  123. *now_down = 200000000;
  124. *now_top = 0;
  125. }
  126. if (*now_down > min_val)
  127. {
  128. *now_down = min_val;
  129. }
  130. if (*now_top < max_val)
  131. {
  132. *now_top = max_val;
  133. }
  134. *stage = 2;
  135. *count = *count + 1;
  136. if (window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] < 80000 &&
  137. window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] >0)
  138. {
  139. *min_count = *min_count + 1;
  140. }
  141. }
  142. return max_index == index;
  143. }
  144. int press_down_detect_new(int index, int left_press, int right_press, int left_high, int right_high, float acc)
  145. {
  146. static int left_data_press[PRESS_COUNT_MAX];
  147. static int right_data_press[PRESS_COUNT_MAX];
  148. static int press_count = 0;
  149. // if((left_high > 1 || right_high > 1) || fabsf(acc-9.8f) > 8.0f )
  150. if(left_high > 1 || right_high > 1)
  151. {
  152. press_count = 0;
  153. return 0;
  154. }
  155. left_data_press[press_count % PRESS_COUNT_MAX] = left_press;
  156. right_data_press[press_count % PRESS_COUNT_MAX] = right_press;
  157. if(press_count >= PRESS_COUNT_MAX)
  158. {
  159. int left_max_val = left_press;
  160. int left_min_val = left_press;
  161. int left_min_index = press_count;
  162. int left_max_index = press_count;
  163. int right_max_val = right_press;
  164. int right_min_val = right_press;
  165. int right_min_index = press_count;
  166. int right_max_index = press_count;
  167. for(int i = press_count; i > press_count - PRESS_COUNT_MAX; i--)
  168. {
  169. if(left_data_press[i % PRESS_COUNT_MAX] > left_max_val)
  170. {
  171. left_max_val = left_data_press[i % PRESS_COUNT_MAX];
  172. left_max_index = i;
  173. }
  174. if(left_data_press[i % PRESS_COUNT_MAX] < left_min_val)
  175. {
  176. left_min_val = left_data_press[i % PRESS_COUNT_MAX];
  177. left_min_index = i;
  178. }
  179. if(right_data_press[i % PRESS_COUNT_MAX] > right_max_val)
  180. {
  181. right_max_val = right_data_press[i % PRESS_COUNT_MAX];
  182. right_max_index = i;
  183. }
  184. if(right_data_press[i % PRESS_COUNT_MAX] < right_min_val)
  185. {
  186. right_min_val = right_data_press[i % PRESS_COUNT_MAX];
  187. right_min_index = i;
  188. }
  189. }
  190. if(left_max_index == press_count && left_max_index - left_min_index > TIME_INTERVAL && left_max_val - left_min_val > PRESS_INTER
  191. && right_max_index == press_count && right_max_index - right_min_index > TIME_INTERVAL && right_max_val - right_min_val > PRESS_INTER)
  192. {
  193. press_count = press_count + 1;
  194. return 1;
  195. }
  196. }
  197. press_count = press_count + 1;
  198. return 0;
  199. }
  200. short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  201. {
  202. if(h_pos[2] > 2)
  203. {
  204. left_up_count = left_up_count + 1;
  205. }
  206. else
  207. {
  208. left_up_count = 0;
  209. }
  210. if(s_pos[2] > 2)
  211. {
  212. right_up_count = right_up_count + 1;
  213. }
  214. else
  215. {
  216. right_up_count = 0;
  217. }
  218. if(right_up_count > 0 && left_up_count > 0)
  219. {
  220. if(last_jump_stage == 0 && right_up_count < 15 && left_up_count < 15)
  221. {
  222. jump_count = jump_count + 1;
  223. last_jump_stage = 1;
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. last_jump_stage = 0;
  229. return 0;
  230. }
  231. void press_minimum_eval(int left_press, int right_press, short h_pos_z, short s_pos_z)
  232. {
  233. if(right_press > right_press_max_val)
  234. {
  235. right_press_max_val = right_press;
  236. }
  237. if(right_press < right_press_min_val)
  238. {
  239. right_press_min_val = right_press;
  240. }
  241. if(left_press > left_press_max_val)
  242. {
  243. left_press_max_val = left_press;
  244. }
  245. if(left_press < left_press_min_val)
  246. {
  247. left_press_min_val = left_press;
  248. }
  249. if(left_press_max_val - left_press_min_val < 10 && right_press_max_val - right_press_min_val < 10 )
  250. {
  251. press_count = press_count + 1;
  252. right_press_sum = right_press_sum + right_press;
  253. left_press_sum = left_press_sum + left_press;
  254. }
  255. else
  256. {
  257. press_count = 0;
  258. right_press_sum = 0;
  259. left_press_sum = 0;
  260. left_press_min_val = 20000000;
  261. right_press_min_val = 20000000;
  262. left_press_max_val = 0;
  263. right_press_max_val = 0;
  264. }
  265. if(press_count == 1000)
  266. {
  267. left_press_low = left_press_sum / press_count * 256;
  268. right_press_low = right_press_sum/ press_count * 256;
  269. press_count = 0;
  270. right_press_sum = 0;
  271. left_press_sum = 0;
  272. left_press_min_val = 20000000;
  273. right_press_min_val = 20000000;
  274. left_press_max_val = 0;
  275. right_press_max_val = 0;
  276. // gpio_mt_run(500);
  277. }
  278. }