publicMotion.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "publicMotion.h"
  2. publicMotion::publicMotion()
  3. {
  4. motion_time = 0;
  5. memset(left_front_mag_window, 0, 5 * sizeof(int));
  6. memset(right_front_mag_window, 0, 5 * sizeof(int));
  7. memset(left_back_mag_window, 0, 5 * sizeof(int));
  8. memset(right_back_mag_window, 0, 5 * sizeof(int));
  9. memset(left_front_mag_mean_window, 0, 15 * sizeof(int));
  10. memset(right_front_mag_mean_window, 0, 15 * sizeof(int));
  11. memset(left_back_mag_mean_window, 0, 15 * sizeof(int));
  12. memset(right_back_mag_mean_window, 0, 15 * sizeof(int));
  13. left_front_up_time = 0;
  14. right_front_up_time = 0;
  15. left_back_up_time = 0;
  16. right_back_up_time = 0;
  17. }
  18. void publicMotion::meanFiltData(int time_count, int mag_data, int* mag_data_window, int* mean_mag_data)
  19. {
  20. if (time_count < 7)
  21. {
  22. mean_mag_data[time_count % 5] = mag_data;
  23. }
  24. else
  25. {
  26. float sum = 0;
  27. for (int i = 0; i < 5; i++)
  28. {
  29. sum += mag_data_window[i];
  30. }
  31. mean_mag_data[time_count % 15] = sum / 5.;
  32. }
  33. }
  34. /*
  35. * 均值滤波的窗口大小为10, 当检测到前三个的和 < 后三个数据的和,则视为上升的趋势
  36. * 上升的时候,还要检测上升趋势的最大值,防止两个凸曲线有重叠误判为蹲
  37. */
  38. void publicMotion::setSpecialPoint_part(int mag_data, int* mean_data, int* max_val, int* down_point, int* up_time)
  39. {
  40. int prev_sum = (mean_data[(motion_time - 8) % 15] + mean_data[(motion_time - 7) % 15] + mean_data[(motion_time - 6) % 15]);
  41. int prev_sum1 = (mean_data[(motion_time - 11) % 15] + mean_data[(motion_time - 10) % 15] + mean_data[(motion_time - 9) % 15]);
  42. int prev_sum2 = (mean_data[(motion_time - 14) % 15] + mean_data[(motion_time - 13) % 15] + mean_data[(motion_time - 12) % 15]);
  43. int back_sum = (mean_data[(motion_time - 2) % 15] + mean_data[(motion_time - 1) % 15] + mean_data[(motion_time) % 15]);
  44. if (prev_sum < back_sum - 100 || prev_sum1 < back_sum - 10 || prev_sum2 < back_sum - 10)
  45. {
  46. if (*max_val < mean_data[motion_time % 15])
  47. {
  48. *max_val = mean_data[motion_time % 15];
  49. }
  50. (*up_time)++;
  51. }
  52. else
  53. {
  54. *max_val = mean_data[motion_time % 15];
  55. *down_point = mean_data[motion_time % 15];
  56. *up_time = 0;
  57. }
  58. }
  59. void publicMotion::setSpecialPoint(int left_front_data, int right_front_data, int left_back_data, int right_back_data)
  60. {
  61. if (motion_time < 10)
  62. {
  63. left_back_mag_point_val = left_back_data;
  64. right_back_mag_point_val = right_back_data;
  65. left_front_mag_point_val = left_front_data;
  66. right_front_mag_point_val = right_front_data;
  67. left_back_mag_up_max = left_back_data;
  68. right_back_mag_up_max = right_back_data;
  69. left_front_mag_up_max = left_front_data;
  70. right_front_mag_up_max = right_front_data;
  71. }
  72. else
  73. {
  74. setSpecialPoint_part(left_front_data, left_front_mag_mean_window, &left_front_mag_up_max, &left_front_mag_point_val, &left_front_up_time);
  75. setSpecialPoint_part(right_front_data, right_front_mag_mean_window, &right_front_mag_up_max, &right_front_mag_point_val, &right_front_up_time);
  76. setSpecialPoint_part(left_back_data, left_back_mag_mean_window, &left_back_mag_up_max, &left_back_mag_point_val, &left_back_up_time);
  77. setSpecialPoint_part(right_back_data, right_back_mag_mean_window, &right_back_mag_up_max, &right_back_mag_point_val, &right_back_up_time);
  78. }
  79. }
  80. void publicMotion::setMagData(int left_front_data, int right_front_data, int left_back_data, int right_back_data)
  81. {
  82. left_front_mag_window[motion_time % 5] = left_front_data;
  83. right_front_mag_window[motion_time % 5] = right_front_data;
  84. left_back_mag_window[motion_time % 5] = left_back_data;
  85. right_back_mag_window[motion_time % 5] = right_back_data;
  86. //均值滤波
  87. meanFiltData(motion_time, left_front_data, left_front_mag_window, left_front_mag_mean_window);
  88. meanFiltData(motion_time, right_front_data, right_front_mag_window, right_front_mag_mean_window);
  89. meanFiltData(motion_time, left_back_data, left_back_mag_window, left_back_mag_mean_window);
  90. meanFiltData(motion_time, right_back_data, right_back_mag_window, right_back_mag_mean_window);
  91. setSpecialPoint(left_front_data, right_front_data, left_back_data, right_back_data);
  92. motion_time++;
  93. }
  94. int publicMotion::getDownData(int left_front_data, int right_front_data, int left_back_data, int right_back_data)
  95. {
  96. setMagData(left_front_data, right_front_data, left_back_data, right_back_data);
  97. int left_front_down_tag = 0;
  98. int right_front_down_tag = 0;
  99. int left_back_down_tag = 0;
  100. int right_back_down_tag = 0;
  101. if (left_back_data - left_back_mag_point_val > 2000 || (left_back_data - left_back_mag_point_val > 1000 && left_back_mag_point_val > 15000))
  102. {
  103. left_back_down_tag = 1;
  104. }
  105. if (right_back_data - right_back_mag_point_val > 2000 || (right_back_data - right_back_mag_point_val > 1000 && right_back_mag_point_val > 15000))
  106. {
  107. right_back_down_tag = 1;
  108. }
  109. if (left_back_down_tag && right_back_down_tag
  110. && left_back_data - left_back_mag_up_max > -500 && right_back_data - right_back_mag_up_max > -500 &&
  111. abs(left_back_up_time - right_back_up_time) < 10 && left_back_up_time > 3 && right_back_up_time > 3)
  112. {
  113. return 1;
  114. }
  115. if (left_front_data - left_front_mag_point_val > 2000 || (left_front_data - left_front_mag_point_val > 1000 && left_front_mag_point_val > 15000))
  116. {
  117. left_front_down_tag = 1;
  118. }
  119. if (right_front_data - right_front_mag_point_val > 2000 || (right_front_data - right_front_mag_point_val > 1000 && right_front_mag_point_val > 15000))
  120. {
  121. right_front_down_tag = 1;
  122. }
  123. if (left_front_down_tag && right_front_down_tag
  124. && left_front_data - left_front_mag_up_max > -500 && right_front_data - right_front_mag_up_max > -500
  125. && abs(left_front_up_time - right_front_up_time) < 10 && left_front_up_time > 3 && right_front_up_time > 3)
  126. {
  127. return 1;
  128. }
  129. return 0;
  130. }