DanceFoot.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #include "DanceFoot.h"
  2. /*
  3. * 初始化脚步的数据
  4. */
  5. DanceFoot::DanceFoot(int LEFT_OR_RIGHT)
  6. {
  7. left_or_right = LEFT_OR_RIGHT;
  8. foot_rssi = 0;
  9. foot_zupt = 1;
  10. rssi_zupt = 1;
  11. max_rssi = 0;
  12. min_rssi = 100;
  13. memset(attitude, 0, 3 * sizeof(float));
  14. memset(globalPos, 0, 3 * sizeof(float));
  15. memset(offsetPos, 0, 3 * sizeof(float));
  16. memset(last_pos_g, 0, 2 * sizeof(float));
  17. memset(pos, 0, 2 * sizeof(float));
  18. last_pos[0] = 0; last_pos[1] = 0;
  19. last_zupt = 1;
  20. danceTraj[0] = 0; danceTraj[1] = 0;
  21. rotate_err = 0;
  22. rotateCor[0] = 1.0; rotateCor[1] = 0.0; rotateCor[2] = 0.0; rotateCor[3] = 1.0;
  23. zupt_count = 1;
  24. pos_index = -1;
  25. }
  26. /*
  27. * 设置脚的姿态,当前位置,鞋子上来的位置偏移量, rssi, zupt变量
  28. */
  29. int DanceFoot::setAttitude(float att[3])
  30. {
  31. memcpy(attitude, att, 3 * sizeof(float));
  32. return 0;
  33. }
  34. int DanceFoot::setPosGlobal(float pos[3])
  35. {
  36. memcpy(globalPos, pos, 3 * sizeof(float));
  37. return 0;
  38. }
  39. int DanceFoot::setPosOffset(float pos[3])
  40. {
  41. memcpy(offsetPos, pos, 3 * sizeof(float));
  42. return 0;
  43. }
  44. int DanceFoot::setFootZupt(int zupt)
  45. {
  46. foot_zupt = zupt;
  47. return 0;
  48. }
  49. int DanceFoot::setFootRssi(int rssi)
  50. {
  51. foot_rssi = rssi;
  52. return 0;
  53. }
  54. int DanceFoot::setMaxRssi(int rssi)
  55. {
  56. max_rssi = rssi;
  57. return 0;
  58. }
  59. int DanceFoot::setMinRssi(int rssi)
  60. {
  61. min_rssi = rssi;
  62. return 0;
  63. }
  64. /*
  65. * 通过判断rssi的变化范围来确定是否没有位移,
  66. * 原地踏步返回0 ,有明显变化的返回1
  67. */
  68. int DanceFoot::isMoveRssi(int rssi, int zupt)
  69. {
  70. int result = 1;
  71. if (zupt == 0)
  72. {
  73. if (rssi > max_rssi)
  74. {
  75. setMaxRssi(rssi);
  76. }
  77. if (rssi < min_rssi)
  78. {
  79. setMinRssi(rssi);
  80. }
  81. }
  82. else
  83. {
  84. if (rssi_zupt == 0)
  85. {
  86. if (max_rssi - min_rssi < 3)
  87. {
  88. result = 0;
  89. }
  90. }
  91. setMaxRssi(0);
  92. setMinRssi(100);
  93. }
  94. rssi_zupt = zupt;
  95. return result;
  96. }
  97. /*
  98. * 处理全局位置的边界问题,全局位置应为40cmX40cm区域
  99. */
  100. int DanceFoot::posBoundary(float pos[3])
  101. {
  102. float posLength = sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
  103. if (posLength > 0.25)
  104. {
  105. pos[0] = pos[0] / posLength * 0.25;
  106. pos[1] = pos[1] / posLength * 0.25;
  107. }
  108. return 0;
  109. }
  110. /*
  111. * 通过角度来判断跳舞毯的方向
  112. *
  113. */
  114. int DanceFoot::getGameDiretion(float *pos)
  115. {
  116. float x = pos[0] ;
  117. float y = pos[1] ;
  118. if (left_or_right == LEFT_FOOT)
  119. {
  120. std::cout << "left_foot ";
  121. }
  122. else
  123. {
  124. std::cout << "right_foot ";
  125. }
  126. if((left_or_right == LEFT_FOOT&& sqrt((x+0.15) * (x+0.15) + y * y) < 0.1)
  127. || (left_or_right == RIGHT_FOOT && sqrt((x - 0.15) * (x - 0.15) + y * y) < 0.1))
  128. {
  129. if (left_or_right == LEFT_FOOT)
  130. {
  131. pos[0] = -0.5f;
  132. }
  133. else
  134. {
  135. pos[0] = 0.5f;
  136. }
  137. pos[1] = 0.0f;
  138. std::cout << "danceGame result: zeros point. " << std::endl;
  139. return MOTION_STEP;
  140. }
  141. float angle = atan2(y, x);
  142. if (angle > 0.0f)
  143. {
  144. if (angle < 1.5708)
  145. {
  146. std::cout << "danceGame result: right up. " << std::endl;
  147. return MOTION_RIGHT_UP;
  148. }
  149. else
  150. {
  151. std::cout << "danceGame result: left up. " << std::endl;
  152. return MOTION_LEFT_UP;
  153. }
  154. }
  155. else
  156. {
  157. if (angle > -1.5708)
  158. {
  159. std::cout << "danceGame result: right down. " << std::endl;
  160. return MOTION_RIGHT_DOWN;
  161. }
  162. else
  163. {
  164. std::cout << "danceGame result: left down. " << std::endl;
  165. return MOTION_LEFT_DOWN;
  166. }
  167. }
  168. return MOTION_STEP;
  169. }
  170. /*
  171. * 计算鞋子的全局位置
  172. */
  173. int DanceFoot::calGlobalPos(float pos_g[3], int rssi, int zupt, int press, int* acc)
  174. {
  175. float pos_zero[2] = { 0, 0 };
  176. float pos_offset[2] = {0, 0};
  177. float pos_g_offset[2] = { 0,0 };
  178. pos_g_offset[0] = pos_g[0] - last_pos_g[0];
  179. pos_g_offset[1] = pos_g[1] - last_pos_g[1];
  180. if (zupt == 1)
  181. {
  182. if (last_zupt == 1)
  183. {
  184. pos_g_offset[0] = 0;
  185. pos_g_offset[1] = 0;
  186. }
  187. }//略去惯导的补偿项,这样是避免补偿过大,导致跳舞的轨迹有拖尾情况
  188. last_pos_g[0] = pos_g[0];
  189. last_pos_g[1] = pos_g[1];
  190. pos[0] += pos_g_offset[0];
  191. pos[1] += pos_g_offset[1];
  192. pos_offset[0] = pos[0] - last_pos[0];
  193. pos_offset[1] = pos[1] - last_pos[1];
  194. /*
  195. if (fabsf(pos_offset[0]) > 10 || fabsf(pos_offset[1]) > 10)
  196. {
  197. std::cout << "dancegame receive unnormal pos_offset: " << pos_offset[0] << " " << pos_offset[1] << std::endl;
  198. pos_offset[0] = 0; pos_offset[1] = 0;
  199. }
  200. */
  201. if (last_zupt == 1 && zupt == 1)
  202. {
  203. data.clear();
  204. }
  205. if (zupt == 0 || (zupt == 1 && last_zupt == 0))
  206. {
  207. posData pos_data;
  208. pos_data.pos_x = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  209. pos_data.pos_y = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  210. pos_data.rssi = rssi;
  211. data.push_back(pos_data);
  212. }
  213. if (zupt == 1 && rssi < DANCEGAME_MIN_RSSI)
  214. {
  215. setLastPos(pos);
  216. setDanceTraj(pos_zero);
  217. }
  218. if (zupt == 1 && last_zupt == 0)
  219. {
  220. /*
  221. * 检测rssi的信号变化,为直线特征做准备
  222. */
  223. int rssi_min = data[0].rssi;
  224. int rssi_max = data[0].rssi;
  225. int rssi_min_index = 0;
  226. int rssi_max_index = 0;
  227. float pos_x_min = data[0].pos_x;
  228. float pos_x_max = data[0].pos_x;
  229. float pos_y_min = data[0].pos_y;
  230. float pos_y_max = data[0].pos_y;
  231. int len = data.size();
  232. for (int i = 0; i < data.size(); i++)
  233. {
  234. if (data[i].rssi < rssi_min)
  235. {
  236. rssi_min = data[i].rssi;
  237. rssi_min_index = i;
  238. }
  239. if (data[i].rssi >= rssi_max)
  240. {
  241. rssi_max = data[i].rssi;
  242. rssi_max_index = i;
  243. }
  244. if (data[i].pos_x > pos_x_max)
  245. {
  246. pos_x_max = data[i].pos_x;
  247. }
  248. if (data[i].pos_x < pos_x_min)
  249. {
  250. pos_x_min = data[i].pos_x;
  251. }
  252. if (data[i].pos_y > pos_y_max)
  253. {
  254. pos_y_max = data[i].pos_y;
  255. }
  256. if (data[i].pos_y < pos_y_min)
  257. {
  258. pos_y_min = data[i].pos_y;
  259. }
  260. }
  261. if (data.size() > 5)
  262. {
  263. int rssiSignal = rssiFeature(data);
  264. switch(rssiSignal)
  265. {
  266. case 1:
  267. break;
  268. case 2://回到原点
  269. if(left_or_right == LEFT_FOOT)
  270. danceTraj[0] = -0.15;
  271. else
  272. danceTraj[0] = 0.15;
  273. danceTraj[1] = 0.0;
  274. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  275. break;
  276. case 0:
  277. if (fabsf(data[0].pos_y - data[len - 1].pos_y) > 0.3&& fabsf(pos_x_max - pos_x_min) < 0.2)
  278. {
  279. /////////////////检测到有跨越象限的情况,左脚当然会经过y = 0的时候, 而x = 0.1则为一个经验值
  280. std::cout << "detect pass special point" << std::endl;
  281. float special_offset[2] = { 0, 0 };
  282. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  283. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  284. if (pos_x_max - pos_x_min < 0.2)
  285. {
  286. if (left_or_right == LEFT_FOOT)
  287. {
  288. special_offset[0] = -0.2 - mid_x;
  289. }
  290. else
  291. {
  292. special_offset[0] = 0.2 - mid_x;
  293. }
  294. }
  295. special_offset[1] = -mid_y;
  296. pos_offset[0] = pos_offset[0] + special_offset[0];
  297. pos_offset[1] = pos_offset[1] + special_offset[1];
  298. }
  299. /**/
  300. //检测到横移的情况
  301. else if (fabs(data[0].pos_x - data[len - 1].pos_x) > 0.3)
  302. {
  303. std::cout << "detected horizontal line by pos_x" << std::endl;
  304. float special_offset[2] = { 0,0 };
  305. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  306. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  307. if (pos_y_max - pos_y_min < 0.15)
  308. {
  309. if (left_or_right == LEFT_FOOT)
  310. {
  311. special_offset[0] = -0.15 - mid_x;
  312. }
  313. else
  314. {
  315. special_offset[0] = 0.15 - mid_x;
  316. }
  317. if (data[0].pos_y > 0)
  318. {
  319. special_offset[1] = 0.2 - mid_y;
  320. }
  321. else
  322. {
  323. special_offset[1] = -0.2 - mid_y;
  324. }
  325. std::cout << "debug :" << special_offset[0] << " " << special_offset[1] << std::endl;
  326. pos_offset[0] = pos_offset[0] + special_offset[0];
  327. pos_offset[1] = pos_offset[1] + special_offset[1];
  328. }
  329. /*
  330. else
  331. {
  332. special_offset[0] = -mid_x;
  333. if (rssi_min <= 35 && data[0].rssi - rssi_min > 10 && data[len - 1].rssi - rssi_min > 10)
  334. {
  335. special_offset[1] = -mid_y;
  336. }
  337. pos_offset[0] = pos_offset[0] + special_offset[0];
  338. pos_offset[1] = pos_offset[1] + special_offset[1];
  339. std::cout << "debug2 :" << special_offset[0] << " " << special_offset[1] << std::endl;
  340. }
  341. */
  342. }
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. if ((sqrt(pos_offset[0] * pos_offset[0] + pos_offset[1] * pos_offset[1]) < 0.1))
  349. {
  350. pos_offset[0] = 0; pos_offset[1] = 0;
  351. if (acc_status == 0)
  352. {
  353. std::cout << "appear error command on zero_vel status (dance__foot) " << endl;
  354. }
  355. }
  356. danceTraj[0] = danceTraj[0] + pos_offset[0];
  357. danceTraj[1] = danceTraj[1] + pos_offset[1];
  358. if (rssi < DANCEGAME_MIN_RSSI)
  359. {
  360. setDanceTraj(pos_zero);
  361. }
  362. setLastPos(pos);
  363. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  364. float dancePos[2];
  365. dancePos[0] = rotateCor[0] * danceTraj[0] + rotateCor[1] * danceTraj[1];
  366. dancePos[1] = rotateCor[2] * danceTraj[0] + rotateCor[3] * danceTraj[1];
  367. pos_index = getGameDiretion(dancePos);
  368. }
  369. float dancePos[2];
  370. dancePos[0] = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  371. dancePos[1] = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  372. setLastZupt(zupt);
  373. gamePos[0] = dancePos[0];
  374. gamePos[1] = dancePos[1];
  375. gamePos[2] = pos_g[2];
  376. if (zupt == 1)
  377. {
  378. zupt_count = 3;
  379. }
  380. else
  381. {
  382. zupt_count--;
  383. }
  384. if (zupt_count <= 0)
  385. {
  386. pos_index = -1;
  387. }
  388. acc_status = getAccStatus(zupt, acc, acc_max, acc_min);
  389. return pos_index;
  390. }
  391. /*
  392. * 打印pos和RSSI的
  393. */
  394. void DanceFoot::printPosData(vector<posData> data)
  395. {
  396. for (int i = 0; i < data.size(); i++)
  397. {
  398. std::cout << data[i].pos_x << " " <<data[i].pos_y << " "<< data[i].rssi << std::endl;
  399. }
  400. }
  401. /*
  402. * 通过位移以及RSSI来判断是否为原地踏步
  403. */
  404. int DanceFoot::isMove(vector<posData> data)
  405. {
  406. int n = data.size();
  407. if (n <= 1)
  408. {
  409. return 0;
  410. }
  411. int rssi_left = data[0].rssi;
  412. int rssi_right = data[(n-1)].rssi;
  413. float x = data[0].pos_x - data[(n-1)].pos_x;
  414. float y = data[0].pos_y - data[(n-1)].pos_y;
  415. int rssi_min = rssi_left;
  416. for (int i = 0; i < n; i++)
  417. {
  418. if (rssi_min > data[i].rssi)
  419. rssi_min = data[i].rssi;
  420. }
  421. if (abs(rssi_right - rssi_left) > 10 && rssi_min > 40
  422. && fabsf(x) > 0.1 )
  423. {
  424. std::cout << "检测到左右横移" << std::endl;
  425. if (x < 0)
  426. return -1;
  427. else
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. /*
  433. * 设置上一次的last_pos
  434. */
  435. void DanceFoot::setLastPos(float* lastPos)
  436. {
  437. last_pos[0] = lastPos[0];
  438. last_pos[1] = lastPos[1];
  439. }
  440. /*
  441. * 设置上一次的zupt
  442. */
  443. void DanceFoot::setLastZupt(int lastZupt)
  444. {
  445. last_zupt = lastZupt;
  446. }
  447. /*
  448. * 设置跳舞轨迹
  449. */
  450. void DanceFoot::setDanceTraj(float* traj)
  451. {
  452. if(left_or_right == LEFT_FOOT)
  453. danceTraj[0] = traj[0] - 0.15;
  454. else
  455. danceTraj[0] = traj[0] + 0.15;
  456. danceTraj[1] = traj[1];
  457. }
  458. /*
  459. * 计算斜率
  460. */
  461. float DanceFoot::calSlope(vector<posData> data)
  462. {
  463. int i = 0;
  464. int len = data.size();
  465. float x2_sum = 0, x_sum = 0, xy_sum = 0, y_sum = 0;
  466. float slope = 0.0f;
  467. for (i = 0; i < len; i++)
  468. {
  469. x2_sum += (data[i].pos_x * data[i].pos_x);
  470. x_sum += (data[i].pos_x);
  471. y_sum += (data[i].pos_y);
  472. xy_sum += (data[i].pos_x * data[i].pos_y);
  473. }
  474. //b = (x2_sum * y_sum - x_sum * xy_sum) / ((len * x2_sum) - x_sum * x_sum);
  475. slope = (len * xy_sum - x_sum * y_sum) / (len * x2_sum - x_sum * x_sum);
  476. std::cout << len << " " << x2_sum << " " << x_sum << " " << y_sum << " " << xy_sum << " " << atan(slope) << std::endl;
  477. return slope;
  478. }
  479. float DanceFoot::calCor(vector<posData> data)
  480. {
  481. //printPosData(data);
  482. float start_x = data[0].pos_x;
  483. float start_y = data[0].pos_y;
  484. int len = data.size();
  485. float end_x = data[len - 1].pos_x;
  486. float end_y = data[len - 1].pos_y;
  487. float a = start_y - end_y;
  488. float b = -(start_x - end_x);
  489. float c = start_x * end_y - start_y * end_x;
  490. float d = 1 / sqrt(a * a + b * b);
  491. float distance = 0;
  492. for (int i = 0; i < len - 1; i++)
  493. {
  494. float diatance_tmp = (a * data[i].pos_x + b * data[i].pos_y + c) * d;
  495. if (fabsf(diatance_tmp) > fabsf(distance))
  496. {
  497. distance = diatance_tmp;
  498. }
  499. }
  500. return distance;
  501. }
  502. float DanceFoot::getGamePos(int index)
  503. {
  504. if (index < 0 || index > 2)
  505. return -1;
  506. return gamePos[index];
  507. }
  508. int DanceFoot::rssiFeature(vector<posData> data)
  509. {
  510. //printPosData(data);
  511. int startRssi = data[0].rssi;
  512. int dataLength = data.size();
  513. int endRssi = data[dataLength - 1].rssi;
  514. int minDistance = 99;
  515. int minIndex = 0;//默认为从原点触发
  516. int i = 0;
  517. int min_rssi = data[0].rssi;
  518. while (i < dataLength)
  519. {
  520. if (min_rssi > data[i].rssi)
  521. {
  522. min_rssi = data[i].rssi;
  523. minIndex = i;
  524. }
  525. i++;
  526. }
  527. i = 0;
  528. int maybeStraight = 0;
  529. if (min_rssi < 25)
  530. {
  531. min_rssi = min_rssi + 2;
  532. }
  533. while (i < dataLength)
  534. {
  535. if (min_rssi >= data[i].rssi)
  536. {
  537. if (fabs(data[dataLength - 1].pos_y - data[i].pos_y) > 0.1 && fabs(data[0].pos_y - data[i].pos_y) > 0.1)
  538. {
  539. maybeStraight = 1;
  540. break;
  541. }
  542. }
  543. i++;
  544. }
  545. float length_tmp = sqrt((data[0].pos_y - data[dataLength - 1].pos_y) * (data[0].pos_y - data[dataLength - 1].pos_y)
  546. + (data[0].pos_x - data[dataLength - 1].pos_x) * (data[0].pos_x - data[dataLength - 1].pos_x));
  547. if (endRssi < 28)
  548. {
  549. return 2;
  550. }
  551. if ((maybeStraight)|| (fabsf(data[0].pos_y - data[dataLength - 1].pos_y)>0.4 && fabsf(data[0].pos_x - data[dataLength - 1].pos_x) < 0.1))
  552. {
  553. std::cout << "Maybe straight line" << std::endl;
  554. return 0;
  555. }
  556. if (startRssi < 28 && endRssi - startRssi > 10 && length_tmp > 0.08)
  557. {
  558. std::cout << "function debug: zero start!!!!!" << std::endl;
  559. return 1; //标志为从原点出发
  560. }
  561. if ( endRssi < 28 && startRssi - endRssi > 10 && length_tmp > 0.08)
  562. {
  563. std::cout << "function debug: zero end!!!!!" << std::endl;
  564. return 2; //标志回到原点
  565. }
  566. std::cout << "startRssi: " << startRssi << " endRssi: " << endRssi << " min_rssi: " << min_rssi << " length_tmp: " << length_tmp <<std::endl;
  567. return 0; //则是标志为其他的特征
  568. }
  569. int DanceFoot::getAccStatus(int zupt, int* acc, int* max_acc, int* min_acc)
  570. {
  571. /*
  572. * 在游戏测试的时候发现有左右乱飞的情况,现在以及乱跳的情况,现在用加速度过滤这一个情况
  573. * 尽量不在嵌入式上修改
  574. */
  575. if (zupt)
  576. {
  577. memcpy(max_acc, acc, 3 * sizeof(int));
  578. memcpy(min_acc, acc, 3 * sizeof(int));
  579. }
  580. else
  581. {
  582. for (int i = 0; i < 3; i++)
  583. {
  584. if (max_acc[i] < acc[i])
  585. {
  586. max_acc[i] = acc[i];
  587. }
  588. if (min_acc[i] > acc[i])
  589. {
  590. min_acc[i] = acc[i];
  591. }
  592. }
  593. }
  594. for (int i = 0; i < 3; i++)
  595. {
  596. if (max_acc[i] - min_acc[i] > 1024)
  597. {
  598. return 1;
  599. }
  600. }
  601. return 0;
  602. }