DanceFoot.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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.05) * (x+0.05) + y * y) < 0.1)
  127. || (left_or_right == RIGHT_FOOT && sqrt((x - 0.05) * (x - 0.05) + 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)
  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. if (fabsf(pos_offset[0]) > 10 || fabsf(pos_offset[1]) > 10)
  195. {
  196. std::cout << "dancegame receive unnormal pos_offset: " << pos_offset[0] << " " << pos_offset[1] << std::endl;
  197. pos_offset[0] = 0; pos_offset[1] = 0;
  198. }
  199. if (last_zupt == 1 && zupt == 1)
  200. {
  201. data.clear();
  202. }
  203. if (zupt == 0 || (zupt == 1 && last_zupt == 0))
  204. {
  205. posData pos_data;
  206. pos_data.pos_x = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  207. pos_data.pos_y = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  208. pos_data.rssi = rssi;
  209. data.push_back(pos_data);
  210. }
  211. if (zupt == 1 && rssi < DANCEGAME_MIN_RSSI)
  212. {
  213. setLastPos(pos);
  214. setDanceTraj(pos_zero);
  215. }
  216. if (zupt == 1 && last_zupt == 0)
  217. {
  218. /*
  219. * 检测rssi的信号变化,为直线特征做准备
  220. */
  221. int rssi_min = data[0].rssi;
  222. int rssi_max = data[0].rssi;
  223. int rssi_min_index = 0;
  224. int rssi_max_index = 0;
  225. float pos_x_min = data[0].pos_x;
  226. float pos_x_max = data[0].pos_x;
  227. float pos_y_min = data[0].pos_y;
  228. float pos_y_max = data[0].pos_y;
  229. int len = data.size();
  230. for (int i = 0; i < data.size(); i++)
  231. {
  232. if (data[i].rssi < rssi_min)
  233. {
  234. rssi_min = data[i].rssi;
  235. rssi_min_index = i;
  236. }
  237. if (data[i].rssi >= rssi_max)
  238. {
  239. rssi_max = data[i].rssi;
  240. rssi_max_index = i;
  241. }
  242. if (data[i].pos_x > pos_x_max)
  243. {
  244. pos_x_max = data[i].pos_x;
  245. }
  246. if (data[i].pos_x < pos_x_min)
  247. {
  248. pos_x_min = data[i].pos_x;
  249. }
  250. if (data[i].pos_y > pos_y_max)
  251. {
  252. pos_y_max = data[i].pos_y;
  253. }
  254. if (data[i].pos_y < pos_y_min)
  255. {
  256. pos_y_min = data[i].pos_y;
  257. }
  258. }
  259. if (data.size() > 5)
  260. {
  261. int rssiSignal = rssiFeature(data);
  262. switch(rssiSignal)
  263. {
  264. case 1:
  265. break;
  266. case 2://回到原点
  267. if(left_or_right == LEFT_FOOT)
  268. danceTraj[0] = -0.05;
  269. else
  270. danceTraj[0] = 0.05;
  271. danceTraj[1] = 0.0;
  272. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  273. break;
  274. case 0:
  275. if (fabsf(data[0].pos_y - data[len - 1].pos_y) > 0.3&& fabsf(pos_x_max - pos_x_min) < 0.2)
  276. {
  277. /////////////////检测到有跨越象限的情况,左脚当然会经过y = 0的时候, 而x = 0.1则为一个经验值
  278. std::cout << "detect pass special point" << std::endl;
  279. float special_offset[2] = { 0, 0 };
  280. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  281. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  282. if (pos_x_max - pos_x_min < 0.2)
  283. {
  284. if (left_or_right == LEFT_FOOT)
  285. {
  286. special_offset[0] = -0.2 - mid_x;
  287. }
  288. else
  289. {
  290. special_offset[0] = 0.2 - mid_x;
  291. }
  292. }
  293. special_offset[1] = -mid_y;
  294. pos_offset[0] = pos_offset[0] + special_offset[0];
  295. pos_offset[1] = pos_offset[1] + special_offset[1];
  296. }
  297. //检测到横移的情况
  298. else if (fabs(data[0].pos_x - data[len - 1].pos_x) > 0.3)
  299. {
  300. std::cout << "detected horizontal line by pos_x" << std::endl;
  301. float special_offset[2] = { 0,0 };
  302. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  303. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  304. if (pos_y_max - pos_y_min < 0.15)
  305. {
  306. if (left_or_right == LEFT_FOOT)
  307. {
  308. special_offset[0] = -0.05 - mid_x;
  309. }
  310. else
  311. {
  312. special_offset[0] = 0.05 - mid_x;
  313. }
  314. if (data[0].pos_y > 0)
  315. {
  316. special_offset[1] = 0.2 - mid_y;
  317. }
  318. else
  319. {
  320. special_offset[1] = -0.2 - mid_y;
  321. }
  322. std::cout << "debug :" << special_offset[0] << " " << special_offset[1] << std::endl;
  323. pos_offset[0] = pos_offset[0] + special_offset[0];
  324. pos_offset[1] = pos_offset[1] + special_offset[1];
  325. }
  326. else
  327. {
  328. special_offset[0] = -mid_x;
  329. if (rssi_min <= 35 && data[0].rssi - rssi_min > 10 && data[len - 1].rssi - rssi_min > 10)
  330. {
  331. special_offset[1] = -mid_y;
  332. }
  333. pos_offset[0] = pos_offset[0] + special_offset[0];
  334. pos_offset[1] = pos_offset[1] + special_offset[1];
  335. std::cout << "debug2 :" << special_offset[0] << " " << special_offset[1] << std::endl;
  336. }
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. if (sqrt(pos_offset[0] * pos_offset[0] + pos_offset[1] * pos_offset[1]) < 0.1)
  344. {
  345. pos_offset[0] = 0; pos_offset[1] = 0;
  346. }
  347. danceTraj[0] = danceTraj[0] + pos_offset[0];
  348. danceTraj[1] = danceTraj[1] + pos_offset[1];
  349. if (rssi < DANCEGAME_MIN_RSSI)
  350. {
  351. setDanceTraj(pos_zero);
  352. }
  353. setLastPos(pos);
  354. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  355. float dancePos[2];
  356. dancePos[0] = rotateCor[0] * danceTraj[0] + rotateCor[1] * danceTraj[1];
  357. dancePos[1] = rotateCor[2] * danceTraj[0] + rotateCor[3] * danceTraj[1];
  358. pos_index = getGameDiretion(dancePos);
  359. }
  360. float dancePos[2];
  361. dancePos[0] = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  362. dancePos[1] = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  363. setLastZupt(zupt);
  364. gamePos[0] = dancePos[0];
  365. gamePos[1] = dancePos[1];
  366. gamePos[2] = pos_g[2];
  367. if (zupt == 1)
  368. {
  369. zupt_count = 10;
  370. }
  371. else
  372. {
  373. zupt_count--;
  374. }
  375. if (zupt_count <= 0)
  376. {
  377. pos_index = -1;
  378. }
  379. return pos_index;
  380. }
  381. /*
  382. * 打印pos和RSSI的
  383. */
  384. void DanceFoot::printPosData(vector<posData> data)
  385. {
  386. for (int i = 0; i < data.size(); i++)
  387. {
  388. std::cout << data[i].pos_x << " " <<data[i].pos_y << " "<< data[i].rssi << std::endl;
  389. }
  390. }
  391. /*
  392. * 通过位移以及RSSI来判断是否为原地踏步
  393. */
  394. int DanceFoot::isMove(vector<posData> data)
  395. {
  396. int n = data.size();
  397. if (n <= 1)
  398. {
  399. return 0;
  400. }
  401. int rssi_left = data[0].rssi;
  402. int rssi_right = data[(n-1)].rssi;
  403. float x = data[0].pos_x - data[(n-1)].pos_x;
  404. float y = data[0].pos_y - data[(n-1)].pos_y;
  405. int rssi_min = rssi_left;
  406. for (int i = 0; i < n; i++)
  407. {
  408. if (rssi_min > data[i].rssi)
  409. rssi_min = data[i].rssi;
  410. }
  411. if (abs(rssi_right - rssi_left) > 10 && rssi_min > 40
  412. && fabsf(x) > 0.1 )
  413. {
  414. std::cout << "检测到左右横移" << std::endl;
  415. if (x < 0)
  416. return -1;
  417. else
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. /*
  423. * 设置上一次的last_pos
  424. */
  425. void DanceFoot::setLastPos(float* lastPos)
  426. {
  427. last_pos[0] = lastPos[0];
  428. last_pos[1] = lastPos[1];
  429. }
  430. /*
  431. * 设置上一次的zupt
  432. */
  433. void DanceFoot::setLastZupt(int lastZupt)
  434. {
  435. last_zupt = lastZupt;
  436. }
  437. /*
  438. * 设置跳舞轨迹
  439. */
  440. void DanceFoot::setDanceTraj(float* traj)
  441. {
  442. if(left_or_right == LEFT_FOOT)
  443. danceTraj[0] = traj[0] - 0.05;
  444. else
  445. danceTraj[0] = traj[0] + 0.05;
  446. danceTraj[1] = traj[1];
  447. }
  448. /*
  449. * 计算斜率
  450. */
  451. float DanceFoot::calSlope(vector<posData> data)
  452. {
  453. int i = 0;
  454. int len = data.size();
  455. float x2_sum = 0, x_sum = 0, xy_sum = 0, y_sum = 0;
  456. float slope = 0.0f;
  457. for (i = 0; i < len; i++)
  458. {
  459. x2_sum += (data[i].pos_x * data[i].pos_x);
  460. x_sum += (data[i].pos_x);
  461. y_sum += (data[i].pos_y);
  462. xy_sum += (data[i].pos_x * data[i].pos_y);
  463. }
  464. //b = (x2_sum * y_sum - x_sum * xy_sum) / ((len * x2_sum) - x_sum * x_sum);
  465. slope = (len * xy_sum - x_sum * y_sum) / (len * x2_sum - x_sum * x_sum);
  466. std::cout << len << " " << x2_sum << " " << x_sum << " " << y_sum << " " << xy_sum << " " << atan(slope) << std::endl;
  467. return slope;
  468. }
  469. float DanceFoot::calCor(vector<posData> data)
  470. {
  471. //printPosData(data);
  472. float start_x = data[0].pos_x;
  473. float start_y = data[0].pos_y;
  474. int len = data.size();
  475. float end_x = data[len - 1].pos_x;
  476. float end_y = data[len - 1].pos_y;
  477. float a = start_y - end_y;
  478. float b = -(start_x - end_x);
  479. float c = start_x * end_y - start_y * end_x;
  480. float d = 1 / sqrt(a * a + b * b);
  481. float distance = 0;
  482. for (int i = 0; i < len - 1; i++)
  483. {
  484. float diatance_tmp = (a * data[i].pos_x + b * data[i].pos_y + c) * d;
  485. if (fabsf(diatance_tmp) > fabsf(distance))
  486. {
  487. distance = diatance_tmp;
  488. }
  489. }
  490. return distance;
  491. }
  492. float DanceFoot::getGamePos(int index)
  493. {
  494. if (index < 0 || index > 2)
  495. return -1;
  496. return gamePos[index];
  497. }
  498. int DanceFoot::rssiFeature(vector<posData> data)
  499. {
  500. //printPosData(data);
  501. int startRssi = data[0].rssi;
  502. int dataLength = data.size();
  503. int endRssi = data[dataLength - 1].rssi;
  504. int minDistance = 99;
  505. int minIndex = 0;//默认为从原点触发
  506. int i = 0;
  507. int min_rssi = data[0].rssi;
  508. while (i < dataLength)
  509. {
  510. if (min_rssi > data[i].rssi)
  511. {
  512. min_rssi = data[i].rssi;
  513. minIndex = i;
  514. }
  515. i++;
  516. }
  517. i = 0;
  518. int maybeStraight = 0;
  519. if (min_rssi < 25)
  520. {
  521. min_rssi = min_rssi + 2;
  522. }
  523. while (i < dataLength)
  524. {
  525. if (min_rssi >= data[i].rssi)
  526. {
  527. if (fabs(data[dataLength - 1].pos_y - data[i].pos_y) > 0.1 && fabs(data[0].pos_y - data[i].pos_y) > 0.1)
  528. {
  529. maybeStraight = 1;
  530. break;
  531. }
  532. }
  533. i++;
  534. }
  535. float length_tmp = sqrt((data[0].pos_y - data[dataLength - 1].pos_y) * (data[0].pos_y - data[dataLength - 1].pos_y)
  536. + (data[0].pos_x - data[dataLength - 1].pos_x) * (data[0].pos_x - data[dataLength - 1].pos_x));
  537. if (endRssi < 28)
  538. {
  539. return 2;
  540. }
  541. 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))
  542. {
  543. std::cout << "Maybe straight line" << std::endl;
  544. return 0;
  545. }
  546. if (startRssi < 28 && endRssi - startRssi > 10 && length_tmp > 0.08)
  547. {
  548. std::cout << "function debug: zero start!!!!!" << std::endl;
  549. return 1; //标志为从原点出发
  550. }
  551. if ( endRssi < 28 && startRssi - endRssi > 10 && length_tmp > 0.08)
  552. {
  553. std::cout << "function debug: zero end!!!!!" << std::endl;
  554. return 2; //标志回到原点
  555. }
  556. std::cout << "startRssi: " << startRssi << " endRssi: " << endRssi << " min_rssi: " << min_rssi << " length_tmp: " << length_tmp <<std::endl;
  557. return 0; //则是标志为其他的特征
  558. }