DanceGame.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "DanceGame.h"
  2. /*
  3. * 初始化DanceGame对象,分左右脚来判断
  4. */
  5. DanceGame::DanceGame()
  6. {
  7. leftFoot = DanceFoot(LEFT_FOOT);
  8. rightFoot = DanceFoot(RIGHT_FOOT);
  9. memset(result, -1, 4 * sizeof(int));
  10. left_result = -1;
  11. right_result = -1;
  12. for (int i = 0; i < 3; i++)
  13. for(int j = 0; j < 15; j++)
  14. {
  15. left_acc_buff[i][j] = 0;
  16. right_acc_buff[i][j] = 0;
  17. }
  18. }
  19. /*
  20. * DanceGame主接口,承接Game类的主要调用
  21. */
  22. void DanceGame::Process(int* right_pos, int* right_att, int *right_acc,int right_zupt, int right_press,
  23. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  24. int jump, int down, int rssi)
  25. {
  26. static int left_zupt_count = 0;
  27. static int right_zupt_count = 0;
  28. static float leftRotate[4] = { 1.0, 0.0, 0.0, 1.0 };
  29. static float rightRotate[4] = { 1.0, 0.0, 0.0, 1.0 };
  30. float left_acc_f[3], right_acc_f[3];
  31. for (int i = 0; i < 3; i++)
  32. {
  33. left_acc_f[i] = left_acc[i] / 2048.f;
  34. right_acc_f[i] = right_acc[i] / 2048.f;
  35. }
  36. for (int i = 0; i < 3; i++)
  37. {
  38. for (int j = 0; j < 14; j++)
  39. {
  40. left_acc_buff[i][j] = left_acc_buff[i][j + 1];
  41. right_acc_buff[i][j] = right_acc_buff[i][j + 1];
  42. }
  43. }
  44. for (int i = 0; i < 3; i++)
  45. {
  46. left_acc_buff[i][14] = left_acc_f[i];
  47. right_acc_buff[i][14] = right_acc_f[i];
  48. }
  49. int left_zupt_acc = isBalance(left_acc_buff);
  50. int right_zupt_acc = isBalance(right_acc_buff);
  51. if (left_zupt_acc && rssi < DANCEGAME_MIN_RSSI)
  52. {
  53. left_zupt = 1;
  54. }
  55. if (right_zupt_acc && rssi < DANCEGAME_MIN_RSSI)
  56. {
  57. right_zupt = 1;
  58. }
  59. if ((left_zupt || left_zupt_acc) && rssi < DANCEGAME_MIN_RSSI)
  60. {
  61. left_zupt_count++;
  62. }
  63. else
  64. {
  65. left_zupt_count = 0;
  66. }
  67. if ((right_zupt || right_zupt_acc) && rssi < DANCEGAME_MIN_RSSI)
  68. {
  69. //rotateTrajRight.ResetHeading(right_att[0]);
  70. right_zupt_count++;
  71. }
  72. else
  73. {
  74. right_zupt_count = 0;
  75. }
  76. if (rssi > DANCEGAME_MIN_RSSI)
  77. {
  78. left_zupt_count = 0;
  79. right_zupt_count = 0;
  80. }
  81. if (left_zupt_count > 100 && right_zupt_count > 100)
  82. {
  83. float leftTheta = float(left_att[0]) * 0.0001f;
  84. float rightTheta = float(right_att[0]) * 0.0001f;
  85. leftRotate[0] = cos(leftTheta); leftRotate[1] = sin(leftTheta);
  86. leftRotate[2] = -sin(leftTheta); leftRotate[3] = cos(leftTheta);
  87. rightRotate[0] = cos(rightTheta); rightRotate[1] = sin(rightTheta);
  88. rightRotate[2] = -sin(rightTheta); rightRotate[3] = cos(rightTheta);
  89. std::cout << "matrix has inited!" << std::endl;
  90. left_zupt_count = 0;
  91. right_zupt_count = 0;
  92. }
  93. float left_pos_g[3];
  94. float pos_tmp[2];
  95. float right_pos_g[3];
  96. pos_tmp[0] = leftRotate[0] * left_pos[0] * 0.01 + leftRotate[1] * left_pos[1] * 0.01;
  97. pos_tmp[1] = leftRotate[2] * left_pos[0] * 0.01 + leftRotate[3] * left_pos[1] * 0.01;
  98. left_pos_g[0] = 0.9848 * pos_tmp[0] - 0.1736 * pos_tmp[1];
  99. left_pos_g[1] = 0.1736 * pos_tmp[0] + 0.9848 * pos_tmp[1];
  100. left_pos_g[2] = left_pos[2] * 0.01;
  101. pos_tmp[0] = rightRotate[0] * right_pos[0] * 0.01 + rightRotate[1] * right_pos[1] * 0.01f;
  102. pos_tmp[1] = rightRotate[2] * right_pos[0] * 0.01 + rightRotate[3] * right_pos[1] * 0.01f;
  103. right_pos_g[0] = 0.9848 * pos_tmp[0] + 0.1736 * pos_tmp[1];
  104. right_pos_g[1] = -0.1736 * pos_tmp[0] + 0.9848 * pos_tmp[1];
  105. right_pos_g[2] = right_pos[2] * 0.01;
  106. int left_result_tmp, right_result_tmp;
  107. left_result_tmp = leftFoot.calGlobalPos(left_pos_g, rssi, left_zupt, left_press, left_acc);
  108. right_result_tmp = rightFoot.calGlobalPos(right_pos_g, rssi, right_zupt, right_press, right_acc);
  109. if (left_result != left_result_tmp && left_result_tmp != -1)
  110. {
  111. result[0] = left_result_tmp;
  112. }
  113. else
  114. {
  115. result[0] = -1;
  116. }
  117. if(left_result != -1 && left_result_tmp == -1)
  118. {
  119. result[0] = left_result | MOTION_CANCEL;
  120. }
  121. left_result = left_result_tmp;
  122. if (right_result != right_result_tmp && right_result_tmp != -1)
  123. {
  124. result[1] = right_result_tmp;
  125. }
  126. else
  127. {
  128. result[1] = -1;
  129. }
  130. if (right_result != -1 && right_result_tmp == -1)
  131. {
  132. result[1] = right_result | MOTION_CANCEL;
  133. }
  134. right_result = right_result_tmp;
  135. /*
  136. result[0] = leftFoot.calGlobalPos(left_pos_g, rssi, left_zupt, left_press);
  137. result[1] = rightFoot.calGlobalPos(right_pos_g, rssi, right_zupt, right_press);
  138. */
  139. }
  140. /*
  141. * 通过三轴加速度判断是否稳定
  142. */
  143. int DanceGame::isBalance(float acc_buff[3][15])
  144. {
  145. float max_val[3];
  146. float min_val[3];
  147. for (int i = 0; i < 3; i++)
  148. {
  149. max_val[i] = acc_buff[i][0];
  150. min_val[i] = acc_buff[i][0];
  151. }
  152. for (int i = 0; i < 3; i++)
  153. {
  154. for (int j = 0; j < 15; j++)
  155. {
  156. if (acc_buff[i][j] > max_val[i])
  157. {
  158. max_val[i] = acc_buff[i][j];
  159. }
  160. if (acc_buff[i][j] < min_val[i])
  161. {
  162. min_val[i] = acc_buff[i][j];
  163. }
  164. }
  165. }
  166. if (max_val[0] - min_val[0] < 0.05f && max_val[1] - min_val[1] < 0.05f && max_val[2] - min_val[2] < 0.05f)
  167. {
  168. return 1;
  169. /*
  170. if (fabsf(min_val[0]) < 0.2f && fabsf(max_val[0]) < 0.2f
  171. && fabsf(min_val[1]) < 0.2f && fabsf(max_val[1]) < 0.2f
  172. && min_val[2] > 0.94f && max_val[2] < 1.6f)
  173. {
  174. std::cout << "test" << endl;
  175. return 1;
  176. }
  177. */
  178. }
  179. return 0;
  180. }
  181. /*
  182. * 获取跳舞游戏结果
  183. */
  184. void DanceGame::getResult(int *matrix)
  185. {
  186. memcpy(matrix, result, 4 * sizeof(int));
  187. }
  188. float DanceGame::getGamePos(int left_or_right, int index)
  189. {
  190. if (index < 0 || index > 2)
  191. return -1;
  192. if (left_or_right == LEFT_FOOT)
  193. return leftFoot.getGamePos(index);
  194. else
  195. return rightFoot.getGamePos(index);
  196. }