DanceFoot.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 = 0;
  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. }
  24. /*
  25. * 设置脚的姿态,当前位置,鞋子上来的位置偏移量, rssi, zupt变量
  26. */
  27. int DanceFoot::setAttitude(float att[3])
  28. {
  29. memcpy(attitude, att, 3 * sizeof(float));
  30. return 0;
  31. }
  32. int DanceFoot::setPosGlobal(float pos[3])
  33. {
  34. memcpy(globalPos, pos, 3 * sizeof(float));
  35. return 0;
  36. }
  37. int DanceFoot::setPosOffset(float pos[3])
  38. {
  39. memcpy(offsetPos, pos, 3 * sizeof(float));
  40. return 0;
  41. }
  42. int DanceFoot::setFootZupt(int zupt)
  43. {
  44. foot_zupt = zupt;
  45. return 0;
  46. }
  47. int DanceFoot::setFootRssi(int rssi)
  48. {
  49. foot_rssi = rssi;
  50. return 0;
  51. }
  52. int DanceFoot::setMaxRssi(int rssi)
  53. {
  54. max_rssi = rssi;
  55. return 0;
  56. }
  57. int DanceFoot::setMinRssi(int rssi)
  58. {
  59. min_rssi = rssi;
  60. return 0;
  61. }
  62. /*
  63. * 通过判断rssi的变化范围来确定是否没有位移,
  64. * 原地踏步返回0 ,有明显变化的返回1
  65. */
  66. int DanceFoot::isMoveRssi(int rssi, int zupt)
  67. {
  68. int result = 1;
  69. if (zupt == 0)
  70. {
  71. if (rssi > max_rssi)
  72. {
  73. setMaxRssi(rssi);
  74. }
  75. if (rssi < min_rssi)
  76. {
  77. setMinRssi(rssi);
  78. }
  79. }
  80. else
  81. {
  82. if (rssi_zupt == 0)
  83. {
  84. if (max_rssi - min_rssi < 3)
  85. {
  86. result = 0;
  87. }
  88. }
  89. setMaxRssi(0);
  90. setMinRssi(100);
  91. }
  92. rssi_zupt = zupt;
  93. return result;
  94. }
  95. /*
  96. * 处理全局位置的边界问题,全局位置应为40cmX40cm区域
  97. */
  98. int DanceFoot::posBoundary(float pos[3])
  99. {
  100. float posLength = sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
  101. if (posLength > 0.25)
  102. {
  103. pos[0] = pos[0] / posLength * 0.25;
  104. pos[1] = pos[1] / posLength * 0.25;
  105. }
  106. return 0;
  107. }
  108. /*
  109. * 通过角度来判断跳舞毯的方向
  110. *
  111. */
  112. int DanceFoot::getGameDiretion(float *pos)
  113. {
  114. float x = pos[0] ;
  115. float y = pos[1] ;
  116. if (left_or_right == LEFT_FOOT)
  117. {
  118. std::cout << "left_foot ";
  119. }
  120. else
  121. {
  122. std::cout << "right_foot ";
  123. }
  124. if(sqrt(x * x + y * y) < 0.08)
  125. {
  126. std::cout << "danceGame result: zeros point. " << std::endl;
  127. return MOTION_STEP;
  128. }
  129. float angle = atan2(y, x);
  130. if (angle > 0.0f)
  131. {
  132. if (angle < 1.5708)
  133. {
  134. std::cout << "danceGame result: right up. " << std::endl;
  135. return MOTION_RIGHT_UP;
  136. }
  137. else
  138. {
  139. std::cout << "danceGame result: left up. " << std::endl;
  140. return MOTION_LEFT_UP;
  141. }
  142. }
  143. else
  144. {
  145. if (angle > -1.5708)
  146. {
  147. std::cout << "danceGame result: right down. " << std::endl;
  148. return MOTION_RIGHT_DOWN;
  149. }
  150. else
  151. {
  152. std::cout << "danceGame result: left down. " << std::endl;
  153. return MOTION_LEFT_DOWN;
  154. }
  155. }
  156. return MOTION_STEP;
  157. }
  158. /*
  159. * 计算鞋子的全局位置
  160. */
  161. int DanceFoot::calGlobalPos(float pos_g[3], int rssi, int zupt)
  162. {
  163. int pos_index = -1;
  164. float pos_zero[2] = { 0, 0 };
  165. float pos_offset[2] = {0, 0};
  166. float pos_g_offset[2] = { 0,0 };
  167. pos_g_offset[0] = pos_g[0] - last_pos_g[0];
  168. pos_g_offset[1] = pos_g[1] - last_pos_g[1];
  169. if (zupt == 1)
  170. {
  171. if (last_zupt == 1)
  172. {
  173. pos_g_offset[0] = 0;
  174. pos_g_offset[1] = 0;
  175. }
  176. }//略去惯导的补偿项,这样是避免补偿过大,导致跳舞的轨迹有拖尾情况
  177. last_pos_g[0] = pos_g[0];
  178. last_pos_g[1] = pos_g[1];
  179. pos[0] += pos_g_offset[0];
  180. pos[1] += pos_g_offset[1];
  181. if (zupt == 1 && rssi < DANCEGAME_MIN_RSSI)
  182. {
  183. setLastPos(pos);
  184. setDanceTraj(pos_zero);
  185. }
  186. pos_offset[0] = pos[0] - last_pos[0];
  187. pos_offset[1] = pos[1] - last_pos[1];
  188. if (fabsf(pos_offset[0]) > 10 || fabsf(pos_offset[1]) > 10)
  189. {
  190. std::cout << "dancegame receive unnormal pos_offset: " << pos_offset[0] << " " << pos_offset[1] << std::endl;
  191. }
  192. if (last_zupt == 1 && zupt == 1)
  193. {
  194. data.clear();
  195. }
  196. posData pos_data;
  197. pos_data.pos_x = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  198. pos_data.pos_y = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  199. pos_data.rssi = rssi;
  200. data.push_back(pos_data);
  201. if (zupt == 1 && last_zupt == 0)
  202. {
  203. /*
  204. * 检测rssi的信号变化,为直线特征做准备
  205. */
  206. if (data.size() > 5)
  207. {
  208. int rssi_min = data[0].rssi;
  209. int rssi_max = data[0].rssi;
  210. int rssi_min_index = 0;
  211. float pos_x_min = data[0].pos_x;
  212. float pos_x_max = data[0].pos_x;
  213. float pos_y_min = data[0].pos_y;
  214. float pos_y_max = data[0].pos_y;
  215. int len = data.size();
  216. for (int i = 0; i < data.size(); i++)
  217. {
  218. if (data[i].rssi < rssi_min)
  219. {
  220. rssi_min = data[i].rssi;
  221. rssi_min_index = i;
  222. }
  223. if (data[i].rssi > rssi_max)
  224. {
  225. rssi_max = data[i].rssi;
  226. }
  227. if (data[i].pos_x > pos_x_max)
  228. {
  229. pos_x_max = data[i].pos_x;
  230. }
  231. if (data[i].pos_x < pos_x_min)
  232. {
  233. pos_x_min = data[i].pos_x;
  234. }
  235. if (data[i].pos_y > pos_y_max)
  236. {
  237. pos_y_max = data[i].pos_y;
  238. }
  239. if (data[i].pos_y < pos_y_min)
  240. {
  241. pos_y_min = data[i].pos_y;
  242. }
  243. }
  244. if (data.size() > 9 && fabsf(data[0].pos_y - data[len - 1].pos_y) > 0.3 && fabsf(pos_x_max - pos_x_min) < 0.2 && data[0].rssi >= DANCEGAME_MIN_RSSI)
  245. {
  246. /////////////////检测到有跨越象限的情况,左脚当然会经过y = 0的时候, 而x = 0.1则为一个经验值
  247. std::cout << "detect pass special point" << std::endl;
  248. //计算斜率,最小二乘拟合
  249. /*
  250. float slope = calSlope(data);
  251. float angle = atan(slope);
  252. if (angle > 0)
  253. {
  254. rotate_err = PI / 2 - angle;
  255. }
  256. else
  257. {
  258. rotate_err = -PI / 2 - angle;
  259. }
  260. if (fabsf(rotate_err) < 0.3 && pos_x_max - pos_x_min < 0.2)
  261. {
  262. rotate_err = 0.5 * rotate_err;
  263. std::cout << "cal rotate_err by straight line: " << rotate_err << std::endl;
  264. float rotateTmp[4] = { 1,0,0,1 };
  265. memcpy(rotateTmp, rotateCor, 4 * sizeof(float));
  266. rotateCor[0] = cos(rotate_err) * rotateTmp[0] - sin(rotate_err) * rotateTmp[2];
  267. rotateCor[1] = cos(rotate_err) * rotateTmp[1] - sin(rotate_err) * rotateTmp[3];
  268. rotateCor[2] = sin(rotate_err) * rotateTmp[0] + cos(rotate_err) * rotateTmp[2];
  269. rotateCor[3] = sin(rotate_err) * rotateTmp[1] + cos(rotate_err) * rotateTmp[3];
  270. if (fabs(rotateCor[0]) > 1 || fabs(rotateCor[1]) > 1 || fabs(rotateCor[2]) > 1 || fabs(rotateCor[3]) > 1)
  271. {
  272. std::cout << "appear unnormal val" << std::endl;
  273. }
  274. //rotateCor[0] = cos(rotate_err); rotateCor[1] = -sin(rotate_err);
  275. //rotateCor[2] = sin(rotate_err); rotateCor[3] = cos(rotate_err);
  276. }
  277. */
  278. float special_offset[2] = {0, 0};
  279. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  280. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  281. if (pos_x_max - pos_x_min < 0.2)
  282. {
  283. if (left_or_right == LEFT_FOOT)
  284. {
  285. special_offset[0] = -0.2 - mid_x;
  286. }
  287. else
  288. {
  289. special_offset[0] = 0.2 - mid_x;
  290. }
  291. }
  292. special_offset[1] = - mid_y;
  293. pos_offset[0] = pos_offset[0] + special_offset[0];
  294. pos_offset[1] = pos_offset[1] + special_offset[1];
  295. }
  296. //检测到横移的情况
  297. else if (data.size() > 9 && fabs(data[0].pos_x - data[len - 1].pos_x) > 0.3 && data[0].rssi >= DANCEGAME_MIN_RSSI)
  298. {
  299. std::cout << "detected horizontal line by pos_x" << std::endl;
  300. float special_offset[2] = { 0,0 };
  301. float mid_x = 0.5 * (data[0].pos_x + data[len - 1].pos_x);
  302. float mid_y = 0.5 * (data[0].pos_y + data[len - 1].pos_y);
  303. if (pos_y_max - pos_y_min < 0.15)
  304. {
  305. if (left_or_right == LEFT_FOOT)
  306. {
  307. special_offset[0] = -0.05 - mid_x;
  308. }
  309. else
  310. {
  311. special_offset[0] = 0.05 - mid_x;
  312. }
  313. if (data[0].pos_y > 0)
  314. {
  315. special_offset[1] = 0.2 - mid_y;
  316. }
  317. else
  318. {
  319. special_offset[1] = -0.2 - mid_y;
  320. }
  321. std::cout << "debug :" << special_offset[0] << " " << special_offset[1] << std::endl;
  322. pos_offset[0] = pos_offset[0] + special_offset[0];
  323. pos_offset[1] = pos_offset[1] + special_offset[1];
  324. }
  325. else
  326. {
  327. special_offset[0] = - mid_x;
  328. if (rssi_min <= DANCEGAME_MIN_RSSI && data[0].rssi - rssi_min > 10 && data[len - 1].rssi - rssi_min > 10)
  329. special_offset[1] = - mid_y;
  330. pos_offset[0] = pos_offset[0] + special_offset[0];
  331. pos_offset[1] = pos_offset[1] + special_offset[1];
  332. std::cout << "debug2 :" << special_offset[0] << " " << special_offset[1] << std::endl;
  333. }
  334. }
  335. else if (sqrt(pos_offset[0] * pos_offset[0] + pos_offset[1] * pos_offset[1]) < 0.1)
  336. {
  337. std::cout << "dectect no move by disance!" << std::endl;
  338. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  339. }
  340. }
  341. danceTraj[0] = danceTraj[0] + pos_offset[0];
  342. danceTraj[1] = danceTraj[1] + pos_offset[1];
  343. /*
  344. if (data[0].rssi < 30)
  345. {
  346. //从原点出发必须可以相信
  347. //暂时先将rotateCor 设置为单位矩阵
  348. std::cout << "debug: set rotateCor as eye(2)";
  349. rotateCor[0] = 1; rotateCor[1] = 0; rotateCor[2] = 0; rotateCor[0] = 1;
  350. }
  351. */
  352. if (rssi < DANCEGAME_MIN_RSSI || sqrt(danceTraj[0] * danceTraj[0] + danceTraj[1] * danceTraj[1] ) < 0.05)
  353. {
  354. danceTraj[0] = 0.0; danceTraj[1] = 0.0;
  355. }
  356. setLastPos(pos);
  357. pos_offset[0] = 0.0; pos_offset[1] = 0.0;
  358. float dancePos[2];
  359. dancePos[0] = rotateCor[0] * danceTraj[0] + rotateCor[1] * danceTraj[1];
  360. dancePos[1] = rotateCor[2] * danceTraj[0] + rotateCor[3] * danceTraj[1];
  361. pos_index = getGameDiretion(dancePos);
  362. }
  363. float dancePos[2];
  364. dancePos[0] = rotateCor[0] * (danceTraj[0] + pos_offset[0]) + rotateCor[1] * (danceTraj[1] + pos_offset[1]);
  365. dancePos[1] = rotateCor[2] * (danceTraj[0] + pos_offset[0]) + rotateCor[3] * (danceTraj[1] + pos_offset[1]);
  366. if (last_zupt == 0)
  367. {
  368. if (left_or_right == LEFT_FOOT)
  369. std::cout << "left ";
  370. else
  371. std::cout << "right ";
  372. std::cout << "dancePos: " << dancePos[0] << " " << dancePos[1] << " " << std::endl;
  373. }
  374. setLastZupt(zupt);
  375. gamePos[0] = dancePos[0];
  376. gamePos[1] = dancePos[1];
  377. gamePos[2] = pos_g[2];
  378. return pos_index;
  379. }
  380. /*
  381. * 打印pos和RSSI的
  382. */
  383. void DanceFoot::printPosData(vector<posData> data)
  384. {
  385. for (int i = 0; i < data.size(); i++)
  386. {
  387. std::cout << data[i].pos_x << " " <<data[i].pos_y << " "<< data[i].rssi << std::endl;
  388. }
  389. }
  390. /*
  391. * 通过位移以及RSSI来判断是否为原地踏步
  392. */
  393. int DanceFoot::isMove(vector<posData> data)
  394. {
  395. int n = data.size();
  396. if (n <= 1)
  397. {
  398. return 0;
  399. }
  400. int rssi_left = data[0].rssi;
  401. int rssi_right = data[(n-1)].rssi;
  402. float x = data[0].pos_x - data[(n-1)].pos_x;
  403. float y = data[0].pos_y - data[(n-1)].pos_y;
  404. int rssi_min = rssi_left;
  405. for (int i = 0; i < n; i++)
  406. {
  407. if (rssi_min > data[i].rssi)
  408. rssi_min = data[i].rssi;
  409. }
  410. if (abs(rssi_right - rssi_left) > 10 && rssi_min > 40
  411. && fabsf(x) > 0.1 )
  412. {
  413. std::cout << "检测到左右横移" << std::endl;
  414. if (x < 0)
  415. return -1;
  416. else
  417. return 1;
  418. }
  419. return 0;
  420. }
  421. /*
  422. * 设置上一次的last_pos
  423. */
  424. void DanceFoot::setLastPos(float* lastPos)
  425. {
  426. last_pos[0] = lastPos[0];
  427. last_pos[1] = lastPos[1];
  428. }
  429. /*
  430. * 设置上一次的zupt
  431. */
  432. void DanceFoot::setLastZupt(int lastZupt)
  433. {
  434. last_zupt = lastZupt;
  435. }
  436. /*
  437. * 设置跳舞轨迹
  438. */
  439. void DanceFoot::setDanceTraj(float* traj)
  440. {
  441. danceTraj[0] = traj[0];
  442. danceTraj[1] = traj[1];
  443. }
  444. /*
  445. * 计算斜率
  446. */
  447. float DanceFoot::calSlope(vector<posData> data)
  448. {
  449. int i = 0;
  450. int len = data.size();
  451. float x2_sum = 0, x_sum = 0, xy_sum = 0, y_sum = 0;
  452. float slope = 0.0f;
  453. for (i = 0; i < len; i++)
  454. {
  455. x2_sum += (data[i].pos_x * data[i].pos_x);
  456. x_sum += (data[i].pos_x);
  457. y_sum += (data[i].pos_y);
  458. xy_sum += (data[i].pos_x * data[i].pos_y);
  459. }
  460. //b = (x2_sum * y_sum - x_sum * xy_sum) / ((len * x2_sum) - x_sum * x_sum);
  461. slope = (len * xy_sum - x_sum * y_sum) / (len * x2_sum - x_sum * x_sum);
  462. std::cout << len << " " << x2_sum << " " << x_sum << " " << y_sum << " " << xy_sum << " " << atan(slope) << std::endl;
  463. return slope;
  464. }
  465. float DanceFoot::getGamePos(int index)
  466. {
  467. if (index < 0 || index > 2)
  468. return -1;
  469. return gamePos[index];
  470. }