SingleFootAction.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #include "SingleFootAction.h"
  2. void SingleFootAction::run(uint32_t _time, int x, int y, int z,int zupt, int rssi, float pitch, float *acc)
  3. {
  4. //std::cout << "pitch : " << pitch <<endl;
  5. /*
  6. int enter_signal_result = -1;
  7. int direction_signal_result = -1;
  8. can_listen_enter_signal(acc, pitch);
  9. int enter_signal_tmp = enter_signal_listen(acc, pitch);
  10. if (last_enter_signal != enter_signal_tmp && enter_signal_tmp > 0 && enter_signal_cooling == 0)
  11. {
  12. enter_signal = enter_signal_tmp;
  13. enter_signal_result = ENTER_KEY;
  14. enter_signal_cooling = 20;
  15. std::cout << "输出垫脚信号 : " << enter_signal << endl;
  16. }
  17. if (enter_signal_tmp > 0)
  18. {
  19. enter_signal_cooling = 20;
  20. }
  21. last_enter_signal = enter_signal_tmp;
  22. can_listen_direction_signal(rssi);
  23. direction_signal = direction_signal_listen(last_x, last_y, zupt, rssi);
  24. if (direction_signal > 0 && can_report_motion == 1)
  25. {
  26. can_report_motion = 0;
  27. if (pitch < 0.78f)
  28. {
  29. direction_signal_result = direction_signal;
  30. }
  31. std::cout << "direction_signal : " << direction_signal << endl;
  32. }
  33. */
  34. direction_signal_listen_new(_time, last_x, last_y, zupt, rssi, pitch);
  35. int zupt_trigger = 0;
  36. if (last_zupt == 0 && zupt == 1)
  37. {
  38. zupt_trigger = 1;
  39. }
  40. enter_signal_listen_new(_time, acc, pitch, zupt_trigger);
  41. last_zupt = zupt;
  42. last_x = x; last_y = y; last_z = z;
  43. /*
  44. if (enter_signal_cooling > 0)
  45. {
  46. enter_signal_cooling--;
  47. }
  48. if (enter_signal_result != -1 )
  49. {
  50. if (footActionState.FootState == ENTER_KEY)
  51. {
  52. footActionState.StateCount ++;
  53. footActionState.Triggering_time = _time;
  54. }
  55. else
  56. {
  57. footActionState.FootState = ENTER_KEY;
  58. footActionState.StateCount = 1;
  59. footActionState.Triggering_time = _time;
  60. }
  61. }
  62. if (zupt == 1)
  63. {
  64. footActionState.Triggering_time = _time;
  65. }
  66. */
  67. }
  68. void SingleFootAction::reset_interation_state()
  69. {
  70. footActionState.FootState = 0;
  71. footActionState.StateCount = 0;
  72. footActionState.Triggering_time = 0;
  73. }
  74. float cal_acc_var(float* acc, int length)
  75. {
  76. //计算均值
  77. float mean_val = 0;
  78. for (int i = 0; i < length; i++)
  79. {
  80. mean_val += acc[i];
  81. }
  82. mean_val /= length;
  83. //计算总体方差
  84. float var_val = 0;
  85. for (int i = 0; i < length; i++)
  86. {
  87. var_val += ((acc[i] - mean_val) * (acc[i] - mean_val));
  88. }
  89. return var_val / length;
  90. }
  91. int acc_is_valid(float* acc_window, int length)
  92. {
  93. //要求窗口窗口长度大于10
  94. //先判断最后五个元素是平稳的,阈值设置为0.06
  95. float station_max = acc_window[length - 1];
  96. float station_min = acc_window[length - 1];
  97. float window_max = acc_window[length - 1];
  98. float window_min = acc_window[length - 1];
  99. int window_max_index = length - 1;
  100. int window_min_index = length - 1;
  101. for (int i = length - 1; i >= 0; i--)
  102. {
  103. if (acc_window[i] > window_max)
  104. {
  105. window_max = acc_window[i];
  106. window_max_index = i;
  107. }
  108. if (acc_window[i] < window_min)
  109. {
  110. window_min = acc_window[i];
  111. window_min_index = i;
  112. }
  113. }
  114. for(int i = length - 1; i >= length - 3; i--)
  115. {
  116. if (acc_window[i] > station_max)
  117. {
  118. station_max = acc_window[i];
  119. }
  120. if (acc_window[i] < station_min)
  121. {
  122. station_min = acc_window[i];
  123. }
  124. }
  125. if (window_max - window_min < 0.2f)
  126. {
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. int SingleFootAction::enter_signal_listen_new(uint32_t _time, float* acc, float pitch, int zupt_trigger)
  132. {
  133. memcpy(acc_x_buff, acc_x_buff + 1, 9 * sizeof(float));
  134. acc_x_buff[9] = acc[0];
  135. //计算加速度的总体方差(n)
  136. float acc_var = cal_acc_var(acc_x_buff, 10);
  137. acc_x_max = acc_x_max > acc[0] ? acc_x_max : acc[0];
  138. acc_x_min = acc_x_min < acc[0] ? acc_x_min : acc[0];
  139. if(acc_is_valid(acc_x_buff, 10))
  140. {
  141. station_count ++;
  142. }
  143. else
  144. {
  145. station_count = 0;
  146. }
  147. //x轴速度剧烈抖动,而且俯仰角大于45度,则证明是踮脚
  148. if (station_count > 20 && pitch > 0.6f && acc_x_max - acc_x_min > 1.0f)
  149. {
  150. //can_report_enter_signal = 0;
  151. //std::cout << "抖动状态下判断脚掂起来" << endl;
  152. acc_x_max = acc[0];
  153. acc_x_min = acc[0];
  154. if (footActionState.FootState != ENTER_KEY)
  155. {
  156. std::cout << "抖动状态下判断脚掂起来" << endl;
  157. footActionState.FootState = ENTER_KEY;
  158. footActionState.StateCount = 1;
  159. }
  160. else
  161. {
  162. if (_time - footActionState.Triggering_time > 10)
  163. {
  164. //std::cout << "抖动状态下判断脚掂起来" << endl;
  165. footActionState.StateCount ++ ;
  166. std::cout << "test" << endl;
  167. }
  168. }
  169. footActionState.Triggering_time = _time;
  170. }
  171. return -1;
  172. }
  173. int SingleFootAction::enter_signal_listen(float *acc, float pitch)
  174. {
  175. memcpy(acc_x_buff, acc_x_buff + 1, 9 * sizeof(float));
  176. acc_x_buff[9] = acc[0];
  177. //计算加速度的总体方差(n)
  178. float acc_var = cal_acc_var(acc_x_buff, 10);
  179. //x轴速度剧烈抖动,而且俯仰角大于45度,则证明是踮脚
  180. if (acc_var > 1.0f && pitch > 0.78f)
  181. {
  182. can_report_enter_signal = 0;
  183. //std::cout << "抖动状态下判断脚掂起来" << endl;
  184. return 1;
  185. }
  186. //如果抖动检测不到,转入平稳状态, 判断俯仰角持续两秒
  187. float acc_norm = sqrt(acc[0] * acc[0] + acc[1] * acc[1] + acc[2] * acc[2]);
  188. //std::cout << "acc_norm : " << acc[0] << ", " << acc[1] << ", " << acc[2] << endl;
  189. if (acc_norm < 1.1f && acc_norm > 0.9f && pitch > 0.78f)
  190. {
  191. big_pitch_count++;
  192. }
  193. else
  194. {
  195. big_pitch_count = 0;
  196. }
  197. if (big_pitch_count > 90 && can_report_enter_signal)
  198. {
  199. can_report_enter_signal = 0;
  200. big_pitch_count = 0;
  201. std::cout << "平稳状态下判断脚掂起来" << endl;
  202. return 1;
  203. }
  204. return -1;
  205. }
  206. int SingleFootAction::can_listen_enter_signal(float* acc, float pitch)
  207. {
  208. if (sqrt(acc[0] * acc[0] + acc[1] * acc[1] + acc[2] * acc[2]) > 1.5f || fabsf(pitch) < 0.2f)
  209. {
  210. can_report_enter_signal = 1;
  211. }
  212. return 0;
  213. }
  214. FootActionState SingleFootAction::get_interation_state()
  215. {
  216. return footActionState;
  217. }
  218. int SingleFootAction::direction_signal_listen_new(uint32_t _time, int x, int y, int zupt, int rssi, float pitch)
  219. {
  220. float x_offset = x;
  221. float y_offset = y;
  222. if (pitch > 0.5f)
  223. {
  224. return -1;
  225. }
  226. if (last_zupt == 0 && zupt == 1)
  227. {
  228. if (_time - footActionState.Triggering_time > 10)
  229. {
  230. if (left_or_right == LEFT_FOOT)
  231. {
  232. footActionState.FootState = BACK_LEFT;
  233. }
  234. else
  235. {
  236. footActionState.FootState = BACK_RIGHT;
  237. }
  238. footActionState.StateCount++;
  239. }
  240. }
  241. if (zupt)
  242. {
  243. footActionState.Triggering_time = _time;
  244. }
  245. return -1;
  246. }
  247. /*
  248. int SingleFootAction::direction_signal_listen_new(uint32_t _time, int x, int y, int zupt, int rssi, float pitch)
  249. {
  250. float x_offset = x;
  251. float y_offset = y;
  252. if (pitch > 0.78f)
  253. {
  254. return -1;
  255. }
  256. if ( last_zupt == 0 && zupt == 1)
  257. {
  258. if (_time - footActionState.FootState < 10)
  259. {
  260. return -1;
  261. }
  262. //触发判断大概的位置, 短距离通过RSSI判断
  263. if (rssi < 30)
  264. {
  265. //先判断当前状态是什么状态,再判断次数
  266. std::cout << "rssi 小于阈值" << endl;
  267. if (footActionState.FootState == 0 || (footActionState.FootState != BACK_LEFT && footActionState.FootState != BACK_RIGHT))
  268. {
  269. if (left_or_right == LEFT_FOOT)
  270. {
  271. footActionState.FootState = BACK_LEFT;
  272. }
  273. else
  274. {
  275. footActionState.FootState = BACK_RIGHT;
  276. }
  277. footActionState.StateCount = 1;
  278. footActionState.Triggering_time = _time;
  279. }
  280. else
  281. {
  282. footActionState.StateCount ++;
  283. footActionState.Triggering_time = _time;
  284. }
  285. }
  286. //长距离移动判断, 通过IMU算出来的距离来判断
  287. else if (sqrt(x_offset * x_offset + y_offset * y_offset) > 10.f)
  288. {
  289. if (left_or_right == LEFT_FOOT)
  290. {
  291. x_offset -= 5;
  292. }
  293. else
  294. {
  295. x_offset += 5;
  296. }
  297. float angle = atan2(y_offset, x_offset);
  298. std::cout << "距离 大于阈值" << endl;
  299. if (left_or_right == LEFT_FOOT && angle > PI / 2.0 && angle < PI)
  300. {
  301. if (footActionState.FootState == FRONT_LEFT)
  302. {
  303. footActionState.StateCount++;
  304. footActionState.Triggering_time = _time;
  305. }
  306. else
  307. {
  308. footActionState.FootState = FRONT_LEFT;
  309. footActionState.StateCount = 1;
  310. footActionState.Triggering_time = _time;
  311. }
  312. }
  313. else if (left_or_right == RIGHT_FOOT && angle < PI / 2.0 && angle >0.0f)
  314. {
  315. if (footActionState.FootState == FRONT_RIGHT)
  316. {
  317. footActionState.StateCount++;
  318. footActionState.Triggering_time = _time;
  319. }
  320. else
  321. {
  322. footActionState.FootState = FRONT_RIGHT;
  323. footActionState.StateCount = 1;
  324. footActionState.Triggering_time = _time;
  325. }
  326. }
  327. }
  328. else
  329. {
  330. std::cout << "距离、rssi 不符合阈值" << endl;
  331. //rssi > 35 && length < 0.15
  332. footActionState.StateCount++;
  333. footActionState.Triggering_time = _time;
  334. }
  335. }
  336. return -1;
  337. }
  338. */
  339. int SingleFootAction::direction_signal_listen(int x, int y, int zupt, int rssi)
  340. {
  341. float x_offset = x;
  342. float y_offset = y;
  343. if (left_or_right == LEFT_FOOT)
  344. {
  345. x_offset -= 5;
  346. }
  347. else
  348. {
  349. y_offset += 5;
  350. }
  351. //当触地、双脚靠拢的时候,判断归位,触发位置的判断
  352. if (zupt == 1 && rssi < 35)
  353. {
  354. has_reset_signal = 1;
  355. }
  356. if (has_reset_signal == 1 && last_zupt == 0 && zupt == 1)
  357. {
  358. //触发判断大概的位置
  359. if (rssi < 30 && on_floor_count == 0)
  360. {
  361. //短距离通过RSSI判断
  362. if (left_or_right == LEFT_FOOT)
  363. {
  364. cmd_stage = BACK_LEFT;
  365. }
  366. else
  367. {
  368. cmd_stage = BACK_RIGHT;
  369. }
  370. time_s = clock();
  371. on_floor_count = 1;
  372. return -1;
  373. }
  374. //长距离移动判断, 通过IMU算出来的距离来判断
  375. else if (sqrt(x_offset * x_offset + y_offset * y_offset) > 15.f)
  376. {
  377. float angle = atan2(y_offset, x_offset);
  378. if (left_or_right == LEFT_FOOT && angle > PI / 2.0 && angle < PI)
  379. {
  380. cmd_stage = FRONT_LEFT;
  381. on_floor_count = 1;
  382. time_s = clock();
  383. return -1;
  384. }
  385. else if (left_or_right == RIGHT_FOOT && angle < PI / 2.0 && angle >0.0f)
  386. {
  387. cmd_stage = FRONT_RIGHT;
  388. on_floor_count = 1;
  389. time_s = clock();
  390. return -1;
  391. }
  392. }
  393. /*
  394. //第一次触地,触发命令类别的判断
  395. if (on_floor_count == 0)
  396. {
  397. on_floor_count ++;
  398. //记录触发的时间,用以确认的时候,判断是不是在有效时间里面
  399. if (cmd_stage == FRONT_LEFT || cmd_stage == FRONT_RIGHT || cmd_stage == BACK_LEFT || cmd_stage == BACK_RIGHT)
  400. {
  401. time_s = clock();
  402. }
  403. return -1;
  404. }
  405. */
  406. else if (on_floor_count == 1)
  407. {
  408. //已经确认了位置
  409. //先判断是否在有效时间内
  410. int result = -1;
  411. double time_interval = (double)(clock() - time_s) / CLOCKS_PER_SEC;
  412. if (time_interval > 1.5)
  413. {
  414. std::cout << "debug : 两步之间超过限制 -- " << endl;
  415. }
  416. if (time_interval < 0.06)
  417. {
  418. std::cout << "抹去0.06s的噪声" << endl;
  419. return result;
  420. }
  421. if (time_interval < 1.5)
  422. {
  423. if ((cmd_stage == FRONT_LEFT || cmd_stage == FRONT_RIGHT) && sqrt(x_offset * x_offset + y_offset * y_offset) < 15.f)
  424. {
  425. result = cmd_stage;
  426. }
  427. else if ((cmd_stage == BACK_LEFT || cmd_stage == BACK_RIGHT) && rssi < 35)
  428. {
  429. result = cmd_stage;
  430. }
  431. else
  432. {
  433. std::cout << "出现其他问题了" << std::endl;
  434. }
  435. }
  436. on_floor_count = 0;
  437. has_reset_signal = 0;
  438. cmd_stage = 0;
  439. return result;
  440. }
  441. }
  442. return -1;
  443. }
  444. void SingleFootAction::can_listen_direction_signal(int rssi)
  445. {
  446. if (rssi < 30)
  447. {
  448. can_report_motion = 1;
  449. }
  450. }