RunGame.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include "RunGame.h"
  2. #include "pub.h"
  3. //单位厘米
  4. #define LENGTH_THRESH 23
  5. #define PITCH_THRESH 0.3f
  6. int rotateGamePos(int* pos, float heading, int left_or_right);
  7. void check_vector(vector<POS_X_CELL> &data_vector, int zupt)
  8. {
  9. //1、当 当前为触地时刻, 应保证 data_vector 仅有两个zupt
  10. // vector 不可能为空,不作 空的判断
  11. if (zupt == 1)
  12. {
  13. //如果输入为触地信号,那么应该先弹出头部zupt为0的数据
  14. if (data_vector.size() == 1)
  15. {
  16. return;
  17. }
  18. while (data_vector.size() != 0)
  19. {
  20. if (data_vector[0].zupt == 0)
  21. {
  22. data_vector.erase(data_vector.begin());
  23. }
  24. else
  25. {
  26. break;
  27. }
  28. }
  29. //再检测当前数组应该保持两个ZUPT就好
  30. if (data_vector.size() == 1)
  31. {
  32. return;
  33. }
  34. //先统计当前有几个zupt吧
  35. int zupt_count = 0;
  36. for (int i = 0; i < data_vector.size(); i++)
  37. {
  38. if (data_vector[i].zupt == 1)
  39. {
  40. zupt_count++;
  41. }
  42. }
  43. if (zupt_count <= 1)
  44. {
  45. std::cout << "rungame error 1 zupt_count : " << zupt_count <<endl;
  46. }
  47. //删除多余的触地数据
  48. int delete_data_count = zupt_count - 2;
  49. while (delete_data_count > 0)
  50. {
  51. if (data_vector[0].zupt == 1)
  52. {
  53. delete_data_count--;
  54. }
  55. data_vector.erase(data_vector.begin());
  56. }
  57. //判断额外的情况,如果数据长度就只有两个,而且都是zupt,那么就需要删除头部
  58. if (data_vector.size() == 2 && data_vector[0].zupt == 1 && data_vector[1].zupt == 1)
  59. {
  60. data_vector.erase(data_vector.begin());
  61. }
  62. }
  63. else
  64. {
  65. //当前信号为触地信号
  66. //保证当前应该只有一个zupt信号
  67. if (data_vector.size() == 1)
  68. {
  69. data_vector.clear();
  70. }
  71. int zupt_count = 0;
  72. for (int i = 0; i < data_vector.size(); i++)
  73. {
  74. if (data_vector[i].zupt == 1)
  75. {
  76. zupt_count++;
  77. }
  78. }
  79. if (zupt_count == 0)
  80. {
  81. data_vector.clear();
  82. }
  83. //删除多余的触地数据
  84. int delete_data_count = zupt_count - 1;
  85. while (delete_data_count > 0)
  86. {
  87. if (data_vector[0].zupt == 1)
  88. {
  89. delete_data_count--;
  90. }
  91. data_vector.erase(data_vector.begin());
  92. }
  93. }
  94. }
  95. void findMaxVal(vector<int>& pos_x, int& max_val, int& max_index)
  96. {
  97. max_val = pos_x[0];
  98. max_index = 0;
  99. for (int i = 0; i < pos_x.size(); i++)
  100. {
  101. if (max_val < pos_x[i])
  102. {
  103. max_val = pos_x[i];
  104. max_index = i;
  105. }
  106. }
  107. }
  108. void findMinVal(vector<int>& pos_x, int& min_val, int& min_index)
  109. {
  110. min_val = pos_x[0];
  111. min_index = 0;
  112. for (int i = 0; i < pos_x.size(); i++)
  113. {
  114. if (min_val > pos_x[i])
  115. {
  116. min_val = pos_x[i];
  117. min_index = i;
  118. }
  119. }
  120. }
  121. //计算结果
  122. int calResult(vector<POS_X_CELL>& data_vector, int left_or_right)
  123. {
  124. int motion = 0;
  125. if (data_vector.size() <= 1)
  126. {
  127. return motion;
  128. }
  129. //经过检查后,保证了头部只有一个触地信号,
  130. //那么应当将触地信号设置本数据段的初始方向,计算一系列旋转后的位置
  131. vector<int> pos_x_vector;
  132. int pos_temp[3];
  133. float heading_temp = data_vector[0].heading * 0.0001f;
  134. for (int i = 0; i < data_vector.size(); i++)
  135. {
  136. pos_temp[0] = data_vector[i].pos_x;
  137. pos_temp[1] = data_vector[i].pos_y;
  138. pos_temp[2] = data_vector[i].pos_z;
  139. pos_x_vector.push_back(rotateGamePos(pos_temp, heading_temp, left_or_right));
  140. }
  141. //寻找一步内轨迹数据中的最大值以及最小值,左脚就找最大值,右脚找最小值
  142. int max_val = pos_x_vector[0];
  143. int max_val_index = 0;
  144. int min_val = pos_x_vector[0];
  145. int min_val_index = 0;
  146. if (left_or_right == LEFT_FOOT)
  147. {
  148. findMaxVal(pos_x_vector, max_val, max_val_index);
  149. }
  150. else
  151. {
  152. findMinVal(pos_x_vector, min_val, min_val_index);
  153. }
  154. //分左右鞋判断
  155. if ((max_val - pos_x_vector[pos_x_vector.size() - 1] > LENGTH_THRESH &&
  156. (data_vector[pos_x_vector.size() - 1].pitch * 0.0001f < PITCH_THRESH || data_vector[pos_x_vector.size() - 1].zupt) && left_or_right == LEFT_FOOT)
  157. ||(pos_x_vector[pos_x_vector.size() - 1] - min_val > LENGTH_THRESH &&
  158. (data_vector[pos_x_vector.size() - 1].pitch * 0.0001f < PITCH_THRESH || data_vector[pos_x_vector.size() - 1].zupt) && left_or_right == RIGHT_FOOT))
  159. {
  160. //DEBUG 一步内轨迹
  161. if (left_or_right == LEFT_FOOT)
  162. {
  163. std::cout << " LEFT_FOOT " << " , ";
  164. }
  165. else
  166. {
  167. std::cout << " RIGHT_FOOT " << " , ";
  168. }
  169. for (int i = 0; i < pos_x_vector.size(); i++)
  170. {
  171. std::cout << pos_x_vector[i] << " , " << data_vector[i].zupt << " , ";
  172. }
  173. std::cout << endl;
  174. //判断完,删去没有的轨迹数据,但是需要保留一个当前最新的触地数据,如果没有,那就全删了
  175. if (data_vector[data_vector.size() - 1].zupt == 1)
  176. {
  177. POS_X_CELL temp = data_vector[data_vector.size() - 1];
  178. data_vector.clear();
  179. data_vector.push_back(temp);
  180. }
  181. else
  182. {
  183. data_vector.clear();
  184. }
  185. motion = 1;
  186. }
  187. else
  188. {
  189. if (data_vector[data_vector.size() - 1].zupt == 1 && data_vector[0].zupt == 1)
  190. {
  191. //一步内都不判断
  192. if (left_or_right == LEFT_FOOT)
  193. {
  194. std::cout << " LEFT_FOOT " << " , ";
  195. }
  196. else
  197. {
  198. std::cout << " RIGHT_FOOT " << " , ";
  199. }
  200. std::cout << ", debug queue" << endl;
  201. for (int i = 0; i < pos_x_vector.size(); i++)
  202. {
  203. std::cout << pos_x_vector[i] << " , " << data_vector[i].zupt << " , ";
  204. }
  205. std::cout << endl;
  206. //在检测结果的时候,还能遇到两个触地信号,那么直接这段数据清空,留最新的触地信号
  207. POS_X_CELL temp = data_vector[data_vector.size() - 1];
  208. data_vector.clear();
  209. data_vector.push_back(temp);
  210. }
  211. }
  212. return motion;
  213. }
  214. void RunGame::Process(int* right_pos, int* right_att, int* right_acc, int right_zupt, int right_press,
  215. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  216. int jump, int down, int rssi)
  217. {
  218. POS_X_CELL left_pos_x_cell = { left_pos[0], left_pos[1], left_pos[2], left_att[0], left_att[1], left_zupt };
  219. POS_X_CELL right_pos_x_cell = { right_pos[0], right_pos[1], right_pos[2], right_att[0], right_att[1],right_zupt };
  220. left_q.push_back(left_pos_x_cell);
  221. right_q.push_back(right_pos_x_cell);
  222. check_vector(left_q, left_zupt);
  223. check_vector(right_q, right_zupt);
  224. if (calResult(left_q, LEFT_FOOT))
  225. {
  226. result[0] = MOTION_LEFT;
  227. std::cout << "appear LEFT_MOTION CMD" << endl;
  228. }
  229. else
  230. {
  231. result[0] = -1;
  232. }
  233. if (calResult(right_q, RIGHT_FOOT))
  234. {
  235. result[1] = MOTION_RIGHT;
  236. std::cout << "appear RIGHT_MOTION CMD" << endl;
  237. }
  238. else
  239. {
  240. result[1] = -1;
  241. }
  242. result[2] = getResultJump(jump);
  243. result[3] = getResultDown(down);
  244. last_left_zupt = left_zupt;
  245. last_right_zupt = right_zupt;
  246. }
  247. void RunGame::getResult(int* dec)
  248. {
  249. memcpy(dec, result, 4 * sizeof(int));
  250. }
  251. void RunGame::setResultConLeft(int zupt)
  252. {
  253. if (zupt == 1)
  254. {
  255. left_has_result = 1;
  256. left_pos_offset_min = 0;
  257. }
  258. }
  259. void RunGame::setResultConRight(int zupt)
  260. {
  261. if (zupt == 1)
  262. {
  263. right_has_result = 1;
  264. right_pos_offset_min = 0;
  265. }
  266. }
  267. int RunGame::getAccStatus(int zupt, int* acc, int* max_acc, int* min_acc)
  268. {
  269. /*
  270. * 在游戏测试的时候发现有左右乱飞的情况,现在以及乱跳的情况,现在用加速度过滤这一个情况
  271. * 尽量不在嵌入式上修改
  272. */
  273. if (zupt)
  274. {
  275. memcpy(max_acc, acc, 3 * sizeof(int));
  276. memcpy(min_acc, acc, 3 * sizeof(int));
  277. }
  278. else
  279. {
  280. for (int i = 0; i < 3; i++)
  281. {
  282. if (max_acc[i] < acc[i])
  283. {
  284. max_acc[i] = acc[i];
  285. }
  286. if (min_acc[i] > acc[i])
  287. {
  288. min_acc[i] = acc[i];
  289. }
  290. }
  291. }
  292. for (int i = 0; i < 3; i++)
  293. {
  294. if (max_acc[i] - min_acc[i] > 1024)
  295. {
  296. return 1;
  297. }
  298. }
  299. return 0;
  300. }
  301. int RunGame::getResultLeft(int *pos, int girl_shoes, float pitch)
  302. {
  303. int isLeft = -1;
  304. int distance_threshold = 15;
  305. if (pos[0] > left_pos_offset_min)
  306. {
  307. left_pos_offset_min = pos[0];
  308. }
  309. if (left_has_result == 1 && pos[0] - left_pos_offset_min < -distance_threshold && pitch < 0.3f )
  310. {
  311. printf("this motion is Left\n");
  312. isLeft = MOTION_LEFT;
  313. left_has_result = 0;
  314. }
  315. else if (left_has_result == 1 && pos[0] - left_pos_offset_min < -distance_threshold && left_acc_status == 0)
  316. {
  317. std::cout << "appear error command on zero_vel status (left_foot) " << endl;
  318. }
  319. return isLeft;
  320. }
  321. int RunGame::getResultRight(int *pos, int girl_shoes, float pitch)
  322. {
  323. int isRight = -1;
  324. int distance_threshold = 15;
  325. if (pos[0] < right_pos_offset_min)
  326. {
  327. right_pos_offset_min = pos[0];
  328. }
  329. if (right_has_result == 1 && pos[0] - right_pos_offset_min > distance_threshold && pitch < 0.3f )
  330. {
  331. printf("this motion is Right\n");
  332. isRight = MOTION_RIGHT;
  333. right_has_result = 0;
  334. }
  335. else if (right_has_result == 1 && pos[0] - right_pos_offset_min > distance_threshold && right_acc_status == 0)
  336. {
  337. std::cout << "appear error command on zero_vel status (right_foot) " << endl;
  338. }
  339. return isRight;
  340. }
  341. int RunGame::getResultLeftRight(int* pos, int devNum)
  342. {
  343. if (devNum == LEFT_FOOT && pos[1] < 0)
  344. {
  345. return MOTION_LEFT;
  346. }
  347. if (devNum == RIGHT_FOOT && pos[1] > 0)
  348. {
  349. return MOTION_RIGHT;
  350. }
  351. return -1;
  352. }
  353. int RunGame::getResultJump(int jump)
  354. {
  355. int isJump = -1;
  356. last_jump_time++;
  357. if (last_jump == 0 && jump == 1)
  358. {
  359. static int jump_count = 0;
  360. printf("this motion is Jump : %d\n", jump_count++);
  361. isJump = MOTION_JUMP;
  362. last_jump_time = 0;
  363. }
  364. else
  365. {
  366. if (last_jump == 0 && jump == 1 && !(left_acc_status == 1 && right_acc_status == 1))
  367. {
  368. std::cout << "appear error command on zero_vel status (jump command) " << endl;
  369. }
  370. }
  371. last_jump = jump;
  372. return isJump;
  373. }
  374. int RunGame::getResultDown(int down)
  375. {
  376. int isDown = -1;
  377. if (last_down == 0 && down == 1 && last_jump_time > 10)
  378. {
  379. printf("this motion is Down\n");
  380. isDown = MOTION_DOWN;
  381. }
  382. last_down = down;
  383. return isDown;
  384. }
  385. float RunGame::getGamePos(int left_or_right, int index)
  386. {
  387. if (index < 0 || index > 2)
  388. return -1;
  389. if (left_or_right == LEFT_FOOT)
  390. {
  391. return left_game_pos[index] * 0.01f;
  392. }
  393. else
  394. {
  395. return right_game_pos[index] * 0.01f;
  396. }
  397. }
  398. RunGame::RunGame()
  399. {
  400. last_down = 0;
  401. last_jump = 0;
  402. last_jump_time = 0;
  403. left_has_result = 0;
  404. right_has_result = 0;
  405. left_zupt = 0;
  406. right_zupt = 0;
  407. left_pos_offset_min = 0;
  408. right_pos_offset_min = 0;
  409. memset(result, 0, 4 * sizeof(int));
  410. memset(left_last_pos, 0, 3 * sizeof(int));
  411. memset(right_last_pos, 0, 3 * sizeof(int));
  412. left_reset_heading = 0.f;
  413. right_reset_heading = 0.f;
  414. has_init_heading = 0;
  415. zupt_count = 0;
  416. memset(left_game_pos, 0, 3 * sizeof(int));
  417. memset(right_game_pos, 0, 3 * sizeof(int));
  418. last_left_zupt = 1;
  419. last_right_zupt = 1;
  420. }
  421. void RunGame::setGameheading(int left_zupt, int right_zupt, float left_heading, float right_heading , int rssi)
  422. {
  423. if (left_zupt)
  424. {
  425. left_reset_heading = left_heading;
  426. }
  427. if (right_zupt)
  428. {
  429. right_reset_heading = right_heading;
  430. }
  431. }
  432. int rotateGamePos(int* pos, float heading,int left_or_right)
  433. {
  434. float pos_f[3];
  435. pos_f[2] = float(pos[2]) * 0.01f;
  436. pos_f[1] = float(pos[1]) * 0.01f;
  437. pos_f[0] = float(pos[0]) * 0.01f;
  438. float posTemp[3];
  439. posTemp[2] = pos_f[2];
  440. if (left_or_right == LEFT_FOOT)
  441. {
  442. posTemp[0] = cos(heading) * float(pos_f[0]) + sin(heading) * float(pos_f[1]);
  443. posTemp[1] = -sin(heading) * float(pos_f[0]) + cos(heading) * float(pos_f[1]);
  444. /*
  445. * 因为女生跑的时候,左鞋鞋子朝向是往左的,导致不在人体的主方向,会使估计的X方向轨迹会变长
  446. */
  447. pos_f[0] = 0.9848 * posTemp[0] - 0.1736 * posTemp[1];
  448. pos_f[1] = 0.1736 * posTemp[0] + 0.9848 * posTemp[1];
  449. }
  450. else
  451. {
  452. posTemp[0] = cos(heading) * float(pos_f[0]) + sin(heading) * float(pos_f[1]);
  453. posTemp[1] = -sin(heading) * float(pos_f[0]) + cos(heading) * float(pos_f[1]);
  454. pos_f[0] = 0.9848 * posTemp[0] + 0.1736 * posTemp[1];
  455. pos_f[1] = - 0.1736 * posTemp[0] + 0.9848 * posTemp[1];
  456. }
  457. /*
  458. pos[0] = (int)(pos_f[0] * 100.0f);
  459. pos[1] = (int)(pos_f[1] * 100.0f);
  460. pos[2] = (int)(pos_f[2] * 100.0f);
  461. */
  462. return (int)(pos_f[0] * 100.0f);
  463. }