press_down_detect.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, float pitch_left, float pitch_right)
  3. {
  4. static int16_t valid_count = 0;
  5. static float left_acc_buff[10];
  6. static float right_acc_buff[10];
  7. memcpy(left_acc_buff, left_acc_buff + 1, 9 * sizeof(float));
  8. memcpy(right_acc_buff, right_acc_buff + 1, 9 * sizeof(float));
  9. left_acc_buff[9] = left_acc_z;
  10. right_acc_buff[9] = right_acc_z;
  11. float max_val_left = left_acc_z;
  12. float min_val_left = left_acc_z;
  13. float max_val_right = right_acc_z;
  14. float min_val_right = right_acc_z;
  15. for(int i = 0; i < 10; i++)
  16. {
  17. if(max_val_left < left_acc_buff[i])
  18. {
  19. max_val_left = left_acc_buff[i];
  20. }
  21. if(min_val_left > left_acc_buff[i])
  22. {
  23. min_val_left = left_acc_buff[i];
  24. }
  25. if(max_val_right < right_acc_buff[i])
  26. {
  27. max_val_right = right_acc_buff[i];
  28. }
  29. if(min_val_right > right_acc_buff[i])
  30. {
  31. min_val_right = right_acc_buff[i];
  32. }
  33. }
  34. if(fabsf(left_acc_z - 1.0f) < 0.2f && fabsf(right_acc_z - 1.0f) < 0.2f && pitch_left > -0.15f && pitch_right > -0.15f)
  35. {
  36. valid_count ++;
  37. }
  38. else
  39. {
  40. valid_count = 0;
  41. }
  42. int16_t valid_laser = 0;
  43. if((left_press < 1000 && left_press > 200) || (right_press > 200 && right_press < 1000))
  44. {
  45. valid_laser = 1;
  46. }
  47. if(valid_count >= 10 && valid_laser == 1 && max_val_right - min_val_right < 0.1f && max_val_left - min_val_left < 0.1f)
  48. return 1;
  49. return 0;
  50. }
  51. //short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  52. //{
  53. // static int last_h_z;
  54. // static int last_s_z;
  55. // static int left_up;
  56. // static int right_up;
  57. //
  58. // static int left_up_count;
  59. // static int right_up_count;
  60. //
  61. // if(h_pos[2] - last_h_z > 0)
  62. // {
  63. // left_up = 1;
  64. // }
  65. // else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  66. // {
  67. // left_up = -1;
  68. // }
  69. //
  70. // if(left_up == 1)
  71. // {
  72. // left_up_count ++;
  73. // }
  74. // else
  75. // {
  76. // left_up_count = 0;
  77. // }
  78. //
  79. // if(s_pos[2] - last_s_z > 0)
  80. // {
  81. // right_up = 1;
  82. // }
  83. // else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  84. // {
  85. // right_up = -1;
  86. // }
  87. //
  88. // if(right_up == 1)
  89. // {
  90. // right_up_count ++;
  91. // }
  92. // else
  93. // {
  94. // right_up_count = 0;
  95. // }
  96. //
  97. // last_h_z = h_pos[2];
  98. // last_s_z = s_pos[2];
  99. //
  100. // if(left_up == 1 && right_up == 1 && right_up_count < 30 && left_up_count < 30
  101. // && right_up_count > 2 && left_up_count > 2 && s_pos[2] < 5 && h_pos[2] < 5 &&
  102. // s_pos[2] > 1 && h_pos[2] > 1)
  103. // {
  104. // return 1;
  105. // }
  106. //
  107. // return 0;
  108. //
  109. //
  110. //}
  111. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  112. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  113. {
  114. *max_val = data[0];
  115. *max_index = 0;
  116. *min_val = data[0];
  117. *min_index = 0;
  118. for(int i = 0; i < length; i++)
  119. {
  120. if(*max_val < data[i])
  121. {
  122. *max_val = data[i];
  123. *max_index = i;
  124. }
  125. if(*min_val > data[i])
  126. {
  127. *min_val = data[i];
  128. *min_index = i;
  129. }
  130. }
  131. }
  132. short press_jump_detect(float left_acc_z, float right_acc_z)
  133. {
  134. static float right_data_z_buff[10];
  135. static float left_data_z_buff[10];
  136. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  137. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  138. right_data_z_buff[9] = right_acc_z;
  139. left_data_z_buff[9] = left_acc_z;
  140. float max_val_right, min_val_right, max_val_left, min_val_left;
  141. int max_index_right, min_index_right, max_index_left, min_index_left;
  142. max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  143. max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  144. if(max_index_right < min_index_right && max_index_left < min_index_left &&
  145. max_val_right - min_val_right > 1.0f && max_val_left - min_val_left > 1.0f &&
  146. min_val_left < 0.2f && min_val_right < 0.2f && max_val_left > 1.5f && max_val_right > 1.5f &&
  147. fabsf(max_val_right - max_val_left) < 2.0f && fabsf(min_val_right - min_val_left) < 2.0f &&
  148. abs(max_index_right- max_index_left) < 5 && abs(min_index_right- min_index_left) < 5)
  149. {
  150. return 1;
  151. }
  152. return 0;
  153. }