Tricycle.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "tricycle.h"
  2. #define PI 3.141592653f
  3. Tricycle::Tricycle()
  4. {
  5. memset(result, -1, 4 * sizeof(int));
  6. result[0] = 0;
  7. left_zupt_count = 0;
  8. right_zupt_count = 0;
  9. left_foot_init_heading = 0.0f;
  10. right_foot_init_heading = 0.0f;
  11. left_trigger_time = 0;
  12. right_trigger_time = 0;
  13. last_left_zupt = 1;
  14. last_right_zupt = 1;
  15. memset(last_left_pos, 0, 3 * sizeof(int));
  16. memset(last_right_pos, 0, 3 * sizeof(int));
  17. left_jump_status = 0;
  18. right_jump_status = 0;
  19. has_init = 0;
  20. last_jump = 0;
  21. prop_status = 0;
  22. }
  23. void Tricycle::Process(int* right_pos, int* right_att, int* right_acc, int right_zupt, int right_press,
  24. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  25. int jump, int down, int rssi)
  26. {
  27. //1、确定初始的方向
  28. if (left_zupt)
  29. {
  30. left_zupt_count++;
  31. }
  32. else
  33. {
  34. left_zupt_count = 0;
  35. }
  36. if (right_zupt)
  37. {
  38. right_zupt_count++;
  39. }
  40. else
  41. {
  42. right_zupt_count = 0;
  43. }
  44. float cur_left_heading = left_att[0] * 0.0001f;
  45. float cur_right_heading = right_att[0] * 0.0001f;
  46. //计算初始方向
  47. if (left_zupt_count > 100 && right_zupt_count > 100 && rssi < 32)
  48. {
  49. left_foot_init_heading = cur_left_heading;
  50. right_foot_init_heading = cur_right_heading;
  51. if (has_init == 0)
  52. {
  53. std::cout << "Cycle has inited !!!" << endl;
  54. }
  55. has_init = 1;
  56. }
  57. float left_heading_offset = calHeadingOffset(left_foot_init_heading, cur_left_heading);
  58. float right_heading_offset = calHeadingOffset(right_foot_init_heading, cur_right_heading);
  59. //std::cout << "left_heading_offset : " << left_heading_offset << " right_heading_offset : " << right_heading_offset << endl;
  60. if (fabsf(calHeadingOffset(left_heading_offset, right_heading_offset)) > PI/3.0f)
  61. {
  62. //std::cout<< "dual_foot_heading appear max angle" <<endl;
  63. }
  64. float game_heading_offset = (left_heading_offset + right_heading_offset) * 0.5f;
  65. //std::cout << "前进度数 :" << game_heading_offset / PI * 180.0f<<endl;
  66. //2、控制释放道具
  67. //先设置一个触发命令的时间点,即怎么判定双脚同时触地,现在设置0.5S左右的大小来等待触地信号的同时到达
  68. int down_prop = -1;
  69. int up_prop = -1;
  70. if (last_jump == 0 && jump)
  71. {
  72. left_jump_status = 1;
  73. right_jump_status = 1;
  74. std::cout << "过程中出现跳" << endl;
  75. }
  76. if ((left_zupt && last_left_zupt == 0) || (right_zupt && last_right_zupt == 0))
  77. {
  78. if (left_pos_offset < -15 && right_pos_offset > 15)
  79. {
  80. up_prop = 1;
  81. prop_status = up_prop;
  82. std::cout << "上层 发放物品" << endl;
  83. }
  84. else if (left_jump_status == 1 && right_jump_status == 1)
  85. {
  86. if (abs(left_pos_offset) < 10 && abs(right_pos_offset) < 10 && prop_status == 1)
  87. {
  88. up_prop = 1;
  89. std::cout << "因为上个状态是 上层发放物品,所以 这次也是 上层发放物品" << endl;
  90. }
  91. else if (abs(left_pos_offset) < 8 && abs(right_pos_offset) < 8)
  92. {
  93. down_prop = 1;
  94. prop_status = 0;
  95. std::cout << "下层 发放物品" << endl;
  96. }
  97. else
  98. {
  99. prop_status = 0;
  100. }
  101. std::cout << "left_pos_offset : " << left_pos_offset << " right_pos_offset : " << right_pos_offset << endl;
  102. }
  103. if (left_pos_offset > 8 && right_pos_offset < -8)
  104. {
  105. prop_status = 0;
  106. }
  107. left_jump_status = 0;
  108. right_jump_status = 0;
  109. }
  110. if (left_zupt)
  111. {
  112. for (int i = 0; i < 3; i++)
  113. {
  114. last_left_pos[i] = left_pos[i];
  115. }
  116. }
  117. left_pos_offset = left_pos[0] - last_left_pos[0];
  118. if (right_zupt)
  119. {
  120. for (int i = 0; i < 3; i++)
  121. {
  122. last_right_pos[i] = right_pos[i];
  123. }
  124. }
  125. right_pos_offset = right_pos[0] - last_right_pos[0];
  126. last_left_zupt = left_zupt;
  127. last_right_zupt = right_zupt;
  128. last_jump = jump;
  129. result[0] = (int)(game_heading_offset * 10000.0f);
  130. result[1] = result[0];
  131. result[2] = (down_prop == 1 ? MOTION_JUMP_OC : -1);
  132. result[3] = (up_prop == 1 ? MOTION_JUMP_VERTICAL : -1);
  133. if (has_init == 0)
  134. {
  135. result[0] = 0;
  136. result[1] = 0;
  137. }
  138. }
  139. float Tricycle::calHeadingOffset(float init_heading, float cur_heading)
  140. {
  141. float heading_offset = cur_heading - init_heading;
  142. if (heading_offset > PI)
  143. {
  144. heading_offset = -(2 * PI - heading_offset);
  145. }
  146. else if (heading_offset < -PI)
  147. {
  148. heading_offset = (2 * PI + heading_offset);
  149. }
  150. return heading_offset;
  151. }
  152. void Tricycle::getResult(int* dec)
  153. {
  154. memcpy(dec, result, 4 * sizeof(int));
  155. }