press_down_detect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #include "press_down_detect.h"
  2. #define down_thresh 2000.0f
  3. int back_jump_stage = 0;
  4. int cancel_down = 0;
  5. int virus_flag = 0;
  6. float real_front_min_left = 50000.f;
  7. float real_front_min_right = 50000.f;
  8. void dual_foot_detect_up_trend(uint16_t* left_mag_up_min, uint16_t*left_up_count, uint16_t* left_mag_window,
  9. uint16_t* right_mag_up_min, uint16_t*right_up_count , uint16_t* right_mag_window, int window_size, int RESET_SIGNAL)
  10. {
  11. uint16_t left_max_val = left_mag_window[window_size - 1];
  12. uint16_t left_min_val = left_mag_window[window_size - 1];
  13. int16_t left_max_index = window_size - 1;
  14. int16_t left_min_index = window_size - 1;
  15. uint16_t right_max_val = right_mag_window[window_size - 1];
  16. uint16_t right_min_val = right_mag_window[window_size - 1];
  17. int16_t right_max_index = window_size - 1;
  18. int16_t right_min_index = window_size - 1;
  19. for (int i = window_size -1; i > 1; i--)
  20. {
  21. if (left_mag_window[i] >= left_mag_window[i - 1] && left_mag_window[i] < left_mag_window[i - 1] + 1000
  22. && right_mag_window[i] >= right_mag_window[i - 1] && right_mag_window[i] < right_mag_window[i - 1] + 1000)
  23. {
  24. if (left_max_val < left_mag_window[i])
  25. {
  26. left_max_val = left_mag_window[i];
  27. left_max_index = i;
  28. }
  29. if (left_min_val > left_mag_window[i])
  30. {
  31. left_min_val = left_mag_window[i];
  32. left_min_index = i;
  33. }
  34. if (right_max_val < right_mag_window[i])
  35. {
  36. right_max_val = right_mag_window[i];
  37. right_max_index = i;
  38. }
  39. if (right_min_val > right_mag_window[i])
  40. {
  41. right_min_val = right_mag_window[i];
  42. right_min_index = i;
  43. }
  44. }
  45. else
  46. {
  47. break;
  48. }
  49. }
  50. //if (left_max_val - left_min_val <= 100 || right_max_val - right_min_val <= 100)
  51. //{
  52. // memset(right_mag_window, right_mag_window[window_size - 1], window_size * sizeof(uint16_t));
  53. // memset(left_mag_window, left_mag_window[window_size - 1], window_size * sizeof(uint16_t));
  54. //}
  55. if ((left_max_val - left_min_val > 100 && left_max_index > left_min_index && left_max_val - left_mag_window[window_size - 1] < 100) &&
  56. (right_max_val - right_min_val > 100 && right_max_index > right_min_index && right_max_val - right_mag_window[window_size - 1] < 100)
  57. &&((RESET_SIGNAL && right_mag_window[window_size - 1] < right_mag_window[window_size - 2] + 1000
  58. && left_mag_window[window_size - 1] < left_mag_window[window_size - 2] + 1000)
  59. || !RESET_SIGNAL))
  60. {
  61. if (*left_up_count > 0)
  62. {
  63. *left_up_count = *left_up_count + 1;
  64. if (left_min_val < *left_mag_up_min)
  65. {
  66. *left_mag_up_min = left_min_val;
  67. }
  68. }
  69. else
  70. {
  71. int index = left_min_index > right_min_index ? left_min_index : right_min_index;
  72. *left_mag_up_min = left_mag_window[index];
  73. for (int i = 0; i <= index; i++)
  74. {
  75. left_mag_window[i] = left_mag_window[index];
  76. }
  77. *left_up_count = window_size - index;
  78. }
  79. if (*right_up_count > 0)
  80. {
  81. *right_up_count = *right_up_count + 1;
  82. if (right_min_val < *right_mag_up_min)
  83. {
  84. *right_mag_up_min = right_min_val;
  85. }
  86. }
  87. else
  88. {
  89. int index = left_min_index > right_min_index ? left_min_index : right_min_index;
  90. *right_mag_up_min = right_mag_window[index];
  91. for (int i = 0; i <= index; i++)
  92. {
  93. right_mag_window[i] = right_mag_window[index];
  94. }
  95. *right_up_count = window_size - index;
  96. }
  97. }
  98. else
  99. {
  100. *left_mag_up_min = 40000;
  101. *right_mag_up_min = 40000;
  102. *left_up_count = 0;
  103. *right_up_count = 0;
  104. //重置窗口数据
  105. //if (RESET_SIGNAL &&((right_mag_window[window_size - 1] > right_mag_window[window_size - 2] + 1000)
  106. // || (left_mag_window[window_size - 1] > left_mag_window[window_size - 2] + 1000)))
  107. ///*if (RESET_SIGNAL && ((right_mag_window[window_size - 1] > right_mag_window[window_size - 2] + 1000)
  108. // || (left_mag_window[window_size - 1] > left_mag_window[window_size - 2] + 1000)))*/
  109. //{
  110. // memset(right_mag_window, right_mag_window[window_size - 1], window_size * sizeof(uint16_t));
  111. // memset(left_mag_window, left_mag_window[window_size - 1], window_size * sizeof(uint16_t));
  112. //}
  113. }
  114. }
  115. int avoid_down_during_change_road(float* left_mag_window, float* right_mag_window, int window_size)
  116. {
  117. float left_max_val = left_mag_window[0];
  118. float left_min_val = left_mag_window[0];
  119. int left_max_index = 0;
  120. int left_min_index = 0;
  121. float right_max_val = right_mag_window[0];
  122. float right_min_val = right_mag_window[0];
  123. int right_max_index = 0;
  124. int right_min_index = 0;
  125. for (int i = 1; i < window_size; i++)
  126. {
  127. if (left_max_val < left_mag_window[i])
  128. {
  129. left_max_val = left_mag_window[i];
  130. left_max_index = i;
  131. }
  132. if (left_min_val > left_mag_window[i])
  133. {
  134. left_min_val = left_mag_window[i];
  135. left_min_index = i;
  136. }
  137. if (right_max_val < right_mag_window[i])
  138. {
  139. right_max_val = right_mag_window[i];
  140. right_max_index = i;
  141. }
  142. if (right_min_val > right_mag_window[i])
  143. {
  144. right_min_val = right_mag_window[i];
  145. right_min_index = i;
  146. }
  147. }
  148. if (left_max_index > left_min_index && right_max_index < right_min_index
  149. && left_max_val - left_min_val > 2000.f && right_max_val - right_min_val > 2000.f)
  150. {
  151. return 1;
  152. }
  153. if (left_max_index < left_min_index && right_max_index > right_min_index
  154. && left_max_val - left_min_val > 2000.f && right_max_val - right_min_val > 2000.f)
  155. {
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. int avoid_down_during_change_road_by_acc(int16_t* left_acc_window, int16_t* right_acc_window, int window_size)
  161. {
  162. int16_t left_max_val = left_acc_window[0];
  163. int16_t left_min_val = left_acc_window[0];
  164. int16_t right_max_val = right_acc_window[0];
  165. int16_t right_min_val = right_acc_window[0];
  166. for (int i = 1; i < window_size; i++)
  167. {
  168. if (left_max_val < left_acc_window[i])
  169. {
  170. left_max_val = left_acc_window[i];
  171. }
  172. if (left_min_val > left_acc_window[i])
  173. {
  174. left_min_val = left_acc_window[i];
  175. }
  176. if (right_max_val < right_acc_window[i])
  177. {
  178. right_max_val = right_acc_window[i];
  179. }
  180. if (right_min_val > right_acc_window[i])
  181. {
  182. right_min_val = right_acc_window[i];
  183. }
  184. }
  185. //if (left_max_val > left_min_val + 0.3f || right_max_val > right_min_val + 0.3f)
  186. if (left_max_val > left_min_val + 500 || right_max_val > right_min_val + 500)
  187. {
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. #define BIG_WINDOW_SIZE 20
  193. int press_down_detect_new(int index, uint16_t front_mag_left, uint16_t back_mag_left,
  194. uint16_t front_mag_right, uint16_t back_mag_right, int16_t left_zupt, int16_t right_zupt,
  195. int16_t left_acc_x, int16_t left_acc_y, int16_t left_acc_z,
  196. int16_t right_acc_x, int16_t right_acc_y, int16_t right_acc_z,
  197. int16_t* front_down, int16_t* back_down)
  198. {
  199. static uint16_t front_mag_left_big_buff[BIG_WINDOW_SIZE];
  200. static uint16_t front_mag_right_big_buff[BIG_WINDOW_SIZE];
  201. static uint16_t back_mag_left_big_buff[BIG_WINDOW_SIZE];
  202. static uint16_t back_mag_right_big_buff[BIG_WINDOW_SIZE];
  203. static int16_t left_acc_z_big_buff[BIG_WINDOW_SIZE/4];
  204. static int16_t right_acc_z_big_buff[BIG_WINDOW_SIZE/4];
  205. static uint16_t left_front_up_min = 40000;
  206. static uint16_t left_back_up_min = 40000;
  207. static uint16_t right_front_up_min = 40000;
  208. static uint16_t right_back_up_min = 40000;
  209. static uint16_t left_front_up_count = 0;
  210. static uint16_t right_front_up_count = 0;
  211. static uint16_t left_back_up_count = 0;
  212. static uint16_t right_back_up_count = 0;
  213. memcpy(front_mag_left_big_buff, front_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(uint16_t));
  214. memcpy(front_mag_right_big_buff, front_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(uint16_t));
  215. memcpy(back_mag_left_big_buff, back_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(uint16_t));
  216. memcpy(back_mag_right_big_buff, back_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(uint16_t));
  217. memcpy(left_acc_z_big_buff, left_acc_z_big_buff + 1, (BIG_WINDOW_SIZE/4 - 1) * sizeof(int16_t));
  218. memcpy(right_acc_z_big_buff, right_acc_z_big_buff + 1, (BIG_WINDOW_SIZE/4 - 1) * sizeof(int16_t));
  219. front_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_left;
  220. front_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_right;
  221. back_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_left;
  222. back_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_right;
  223. left_acc_z_big_buff[(BIG_WINDOW_SIZE/4 - 1)] = left_acc_z;
  224. right_acc_z_big_buff[(BIG_WINDOW_SIZE/4 - 1)] = right_acc_z;
  225. dual_foot_detect_up_trend(&left_front_up_min, &left_front_up_count, front_mag_left_big_buff,
  226. &right_front_up_min, &right_front_up_count, front_mag_right_big_buff, BIG_WINDOW_SIZE, 1);
  227. dual_foot_detect_up_trend(&left_back_up_min, &left_back_up_count, back_mag_left_big_buff,
  228. &right_back_up_min, &right_back_up_count, back_mag_right_big_buff, BIG_WINDOW_SIZE, 0);
  229. if (front_mag_left - left_front_up_min >2000 && front_mag_right - right_front_up_min > 2000)
  230. {
  231. *front_down = 1;
  232. //printf( "left_front_up_count: %d , right_front_up_count : %d \n" , left_front_up_count , right_front_up_count);
  233. }
  234. if (back_mag_left - left_back_up_min > 3000 && back_mag_right - right_back_up_min > 3000)
  235. {
  236. *back_down = 1;
  237. //printf("left_front_up_count: %d , right_front_up_count : %d \n", left_front_up_count , right_front_up_count);
  238. }
  239. if (avoid_down_during_change_road_by_acc(left_acc_z_big_buff, right_acc_z_big_buff, BIG_WINDOW_SIZE/4))
  240. {
  241. if (*front_down == 1)
  242. {
  243. //printf("delete front_down when change road \n");
  244. }
  245. if (*back_down == 1)
  246. {
  247. //printf("delete back_down when change road \n");
  248. }
  249. *front_down = 0;
  250. *back_down = 0;
  251. }
  252. return 0;
  253. }
  254. short pos_jump_detect(int* h_pos, int* s_pos, int left_zupt, int right_zupt)
  255. {
  256. static int last_h_z;
  257. static int last_s_z;
  258. static int left_up;
  259. static int right_up;
  260. static int left_up_count;
  261. static int right_up_count;
  262. static int left_zupt_count;
  263. static int right_zupt_count;
  264. if (left_zupt)
  265. {
  266. left_zupt_count = 10;
  267. }
  268. else
  269. {
  270. left_zupt_count--;
  271. }
  272. if (right_zupt)
  273. {
  274. right_zupt_count = 10;
  275. }
  276. else
  277. {
  278. right_zupt_count--;
  279. }
  280. if (h_pos[2] - last_h_z > 0 && h_pos[2] > 0)
  281. {
  282. left_up = 1;
  283. }
  284. else if ((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  285. {
  286. left_up = -1;
  287. }
  288. if (left_up == 1)
  289. {
  290. left_up_count++;
  291. }
  292. else
  293. {
  294. left_up_count = 0;
  295. }
  296. if (s_pos[2] - last_s_z > 0 && s_pos[2] > 0)
  297. {
  298. right_up = 1;
  299. }
  300. else if ((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  301. {
  302. right_up = -1;
  303. }
  304. if (right_up == 1)
  305. {
  306. right_up_count++;
  307. }
  308. else
  309. {
  310. right_up_count = 0;
  311. }
  312. last_h_z = h_pos[2];
  313. last_s_z = s_pos[2];
  314. if (left_up == 1 && right_up == 1 && right_up_count < 15 && left_up_count < 15
  315. && right_zupt_count > 0 && left_zupt_count > 0 &&
  316. ((right_up_count > 2 && left_up_count > 2) ||
  317. (right_up_count > 0 && left_up_count > 0 && h_pos[2] > 15 && s_pos[2] > 15)))
  318. {
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  324. void max_min_window(float* data, int length, float* max_val, int* max_index, float* min_val, int* min_index)
  325. {
  326. *max_val = data[0];
  327. *max_index = 0;
  328. *min_val = data[0];
  329. *min_index = 0;
  330. for (int i = 0; i < length; i++)
  331. {
  332. if (*max_val < data[i])
  333. {
  334. *max_val = data[i];
  335. *max_index = i;
  336. }
  337. if (*min_val > data[i])
  338. {
  339. *min_val = data[i];
  340. *min_index = i;
  341. }
  342. }
  343. }
  344. int avoid_error_jump_by_press(float* mag_window, int window_size)
  345. {
  346. float min_val = mag_window[0];
  347. float max_val = mag_window[0];
  348. int max_index = 0;
  349. for (int i = 0; i < window_size; i++)
  350. {
  351. if (min_val > mag_window[0])
  352. {
  353. min_val = mag_window[0];
  354. }
  355. if (max_val < mag_window[0])
  356. {
  357. max_val = mag_window[0];
  358. max_index = i;
  359. }
  360. }
  361. if (max_val - min_val > 2000.f && max_index == window_size - 1)
  362. {
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int left_front_press, int right_front_press)
  368. {
  369. static float right_data_z_buff[10];
  370. static float left_data_z_buff[10];
  371. static float left_front_mag_buff[10];
  372. static float right_front_mag_buff[10];
  373. static int press_wait;
  374. static int acc_wait;
  375. /*
  376. * 存储数据
  377. */
  378. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  379. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  380. right_data_z_buff[9] = right_acc_z;
  381. left_data_z_buff[9] = left_acc_z;
  382. memcpy(left_front_mag_buff, left_front_mag_buff + 1, 9 * sizeof(float));
  383. memcpy(right_front_mag_buff, right_front_mag_buff + 1, 9 * sizeof(float));
  384. left_front_mag_buff[9] = left_front_press;
  385. right_front_mag_buff[9] = right_front_press;
  386. float max_val_right_mag, min_val_right_mag, max_val_left_mag, min_val_left_mag;
  387. int max_index_right_mag, min_index_right_mag, max_index_left_mag, min_index_left_mag;
  388. max_min_window(right_front_mag_buff, 10, &max_val_right_mag, &max_index_right_mag, &min_val_right_mag, &min_index_right_mag);
  389. max_min_window(left_front_mag_buff, 10, &max_val_left_mag, &max_index_left_mag, &min_val_left_mag, &min_index_left_mag);
  390. if (max_val_right_mag > right_front_press + 3000.f && max_val_left_mag > left_front_press + 3000.f)
  391. {
  392. int start_index = max_index_left_mag > max_index_right_mag ? max_index_left_mag : max_index_right_mag;
  393. int down_flag = 1;
  394. // 遍历双脚持续下降的所用
  395. for (int i = start_index + 1; i < 10; i++)
  396. {
  397. if (right_front_mag_buff[i - 1] - right_front_mag_buff[i] < 100.f)
  398. {
  399. down_flag = 0;
  400. break;
  401. }
  402. if (left_front_mag_buff[i - 1] - left_front_mag_buff[i] < 100.f)
  403. {
  404. down_flag = 0;
  405. break;
  406. }
  407. }
  408. if (down_flag == 1)
  409. {
  410. press_wait = 10;
  411. }
  412. }
  413. /*
  414. * 加速变化估计
  415. */
  416. float max_val_right, min_val_right, max_val_left, min_val_left;
  417. int max_index_right, min_index_right, max_index_left, min_index_left;
  418. max_min_window(right_data_z_buff, 10, &max_val_right, &max_index_right, &min_val_right, &min_index_right);
  419. max_min_window(left_data_z_buff, 10, &max_val_left, &max_index_left, &min_val_left, &min_index_left);
  420. /*
  421. * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  422. */
  423. int jump = 0;
  424. if (max_index_right < min_index_right && max_index_left < min_index_left
  425. && max_val_right - min_val_right > 0.8f && max_val_left - min_val_left > 0.8f
  426. && (left_zupt == 0 && right_zupt == 0))
  427. {
  428. acc_wait = 10;
  429. }
  430. if ( acc_wait > 0 && press_wait > 0)
  431. {
  432. jump = 1;
  433. }
  434. if (acc_wait > 0)
  435. {
  436. acc_wait--;
  437. }
  438. if (press_wait > 0)
  439. {
  440. press_wait--;
  441. }
  442. //if (avoid_error_jump_by_press(left_front_mag_buff, 10))
  443. //{
  444. // jump = 0;
  445. //}
  446. //if (avoid_error_jump_by_press(right_front_mag_buff, 10))
  447. //{
  448. // jump = 0;
  449. //}
  450. return jump;
  451. }
  452. int att_jump_detect(float left_pitch, float right_pitch, int left_zupt, int right_zupt)
  453. {
  454. static int num;
  455. static float left_pitch_window[8];
  456. static float right_pitch_window[8];
  457. //用俯仰角判断好了, 现在的IMU方向 下标1为俯仰角
  458. memcpy(left_pitch_window, left_pitch_window + 1, 7 * sizeof(float));
  459. memcpy(right_pitch_window, right_pitch_window + 1, 7 * sizeof(float));
  460. left_pitch_window[7] = left_pitch;
  461. right_pitch_window[7] = right_pitch;
  462. num++;
  463. if (num < 8)
  464. {
  465. return 0;
  466. }
  467. else
  468. {
  469. num = 8;
  470. }
  471. //要求起跳要同步
  472. static int zupt_count = 0;
  473. if (left_zupt && right_zupt)
  474. {
  475. zupt_count = 21;
  476. }
  477. if (zupt_count > 0)
  478. {
  479. zupt_count--;
  480. }
  481. for (int i = 7; i > 1; i--)
  482. {
  483. if ((left_pitch_window[i] > left_pitch_window[i - 1] || left_pitch_window[i] > left_pitch_window[i - 2])
  484. && (right_pitch_window[i] > right_pitch_window[i - 1] || right_pitch_window[i] > right_pitch_window[i - 2]))
  485. {
  486. if ((left_pitch > left_pitch_window[i - 1] + 0.1f || left_pitch > left_pitch_window[i - 2] + 0.1f)
  487. && (right_pitch > right_pitch_window[i - 1] + 0.1f || right_pitch > right_pitch_window[i - 2] + 0.1f)
  488. && zupt_count > 0)
  489. {
  490. return 1;
  491. }
  492. }
  493. else
  494. {
  495. return 0;
  496. }
  497. }
  498. return 0;
  499. }