FootStep.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "FootStep.h"
  2. FootStep::FootStep()
  3. {
  4. /*
  5. 初始化均为0
  6. */
  7. stepStatus = 0;
  8. stepFreq = 0;
  9. stepTag = 0;
  10. zuptCount = 0;
  11. lastZupt = 1;
  12. leaveFloorTime1 = 0;
  13. leaveFloorTime2 = 0;
  14. leaveFloorTime = 0;
  15. leaveTimeStatus = 1;
  16. catchFloorTime = 0;
  17. localTime = 0;
  18. last_time_stamp = 0;
  19. stepCount = 0;
  20. memset(last_pos, 0, 3 * sizeof(int));
  21. memset(step_vel_buff, 0, 3 * sizeof(float));
  22. mean_vel = 0;
  23. mean_window_vel = 0;
  24. }
  25. void calStepFreq(vector<FLOOR_TIME_STYPE>& time_vector, float &mean_step_vel)
  26. {
  27. int length = time_vector.size();
  28. if (length < 2)
  29. {
  30. cout << "FootStep::calStepFreq vector_length < 2" << endl;
  31. return;
  32. }
  33. FLOOR_TIME_STYPE cur_floor_time_stype = time_vector[length - 1];
  34. FLOOR_TIME_STYPE last_floor_time_stype = time_vector[length - 3];
  35. if (cur_floor_time_stype.time_type != LEAVE_TIME_STYPE || last_floor_time_stype.time_type != LEAVE_TIME_STYPE
  36. || time_vector[length - 2].time_type != CATCH_TIME_STYPE)
  37. {
  38. cout << "FootStep::calStepFreq time_stype is not valid " << endl;
  39. for (int i = 0; i < length; i++)
  40. {
  41. cout << time_vector[i].time << ", ";
  42. if (time_vector[i].time_type == LEAVE_TIME_STYPE)
  43. {
  44. cout << "LEAVE_TIME_STYPE," << endl;
  45. }
  46. else
  47. {
  48. cout << "CATCH_TIME_STYPE," << endl;
  49. }
  50. }
  51. }
  52. else
  53. {
  54. mean_step_vel = 100.f / (cur_floor_time_stype.time - last_floor_time_stype.time);
  55. }
  56. //删除所有的点
  57. time_vector.clear();
  58. time_vector.push_back(cur_floor_time_stype);
  59. }
  60. int check_time_vector(vector<FLOOR_TIME_STYPE>& time_vector)
  61. {
  62. int length = time_vector.size();
  63. if (length < 2)
  64. {
  65. return 0;
  66. }
  67. FLOOR_TIME_STYPE cur_floor_time_stype = time_vector[length - 1];
  68. vector<FLOOR_TIME_STYPE> time_vector_tmp(time_vector);
  69. cout << "FootStep::time_vector before" << endl;
  70. for (int i = 0; i < time_vector.size(); i++)
  71. {
  72. cout << time_vector[i].time << ", ";
  73. if (time_vector[i].time_type == LEAVE_TIME_STYPE)
  74. {
  75. cout << "LEAVE_TIME_STYPE, ";
  76. }
  77. else
  78. {
  79. cout << "CATCH_TIME_STYPE, ";
  80. }
  81. }
  82. cout << endl;
  83. time_vector.clear();
  84. time_vector.push_back(time_vector_tmp[0]);
  85. for (int i = 1; i < time_vector_tmp.size(); i++)
  86. {
  87. //按照人步伐,也不可能触地以及在空中的时间是小于50ms的
  88. if (time_vector_tmp[i].time - time_vector_tmp[i - 1].time >= 5)
  89. {
  90. time_vector.push_back(time_vector_tmp[i]);
  91. }
  92. else
  93. {
  94. std::cout << "delect time_vector_tmp cell " << endl;
  95. }
  96. }
  97. cout << "FootStep::time_vector mid " << endl;
  98. for (int i = 0; i < time_vector.size(); i++)
  99. {
  100. cout << time_vector[i].time << ", ";
  101. if (time_vector[i].time_type == LEAVE_TIME_STYPE)
  102. {
  103. cout << "LEAVE_TIME_STYPE, " ;
  104. }
  105. else
  106. {
  107. cout << "CATCH_TIME_STYPE, ";
  108. }
  109. }
  110. cout << endl;
  111. time_vector_tmp = time_vector;
  112. time_vector.clear();
  113. time_vector.push_back(time_vector_tmp[0]);
  114. int index = 1;
  115. while (index < time_vector_tmp.size())
  116. {
  117. if (time_vector_tmp[index].time_type != time_vector[time_vector.size() - 1].time_type)
  118. {
  119. time_vector.push_back(time_vector_tmp[index]);
  120. }
  121. index++;
  122. }
  123. cout << "FootStep::time_vector end" << endl;
  124. for (int i = 0; i < time_vector.size(); i++)
  125. {
  126. cout << time_vector[i].time << ", ";
  127. if (time_vector[i].time_type == LEAVE_TIME_STYPE)
  128. {
  129. cout << "LEAVE_TIME_STYPE, ";
  130. }
  131. else
  132. {
  133. cout << "CATCH_TIME_STYPE, ";
  134. }
  135. }
  136. cout << endl;
  137. if (time_vector.size() < 2)
  138. {
  139. return 0;
  140. }
  141. if (time_vector[time_vector.size() - 1].time_type == LEAVE_TIME_STYPE &&
  142. time_vector[time_vector.size() - 2].time_type == CATCH_TIME_STYPE &&
  143. time_vector[time_vector.size() -3].time_type == LEAVE_TIME_STYPE)
  144. {
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. void FootStep::stepCal(int timeStamp, int pos[3], int zupt, int vel_int)
  150. {
  151. vel_sqrt_sum += (vel_int) * 0.0001f;
  152. vel_sqrt_num++;
  153. int timeDistance;
  154. if (timeStamp - last_time_stamp < 0)
  155. {
  156. timeDistance = 256 + timeStamp - last_time_stamp;
  157. }
  158. else
  159. {
  160. timeDistance = timeStamp - last_time_stamp;
  161. }
  162. last_time_stamp = timeStamp;
  163. localTime += timeDistance;
  164. //if (lastZupt == 0 && zupt == 1 && stepTag == 1 )
  165. //{
  166. //
  167. // int walkTime = 8 * (localTime - leaveFloorTime1);
  168. // // 模块发过来的频率为40ms发一次,所以一秒能发25次,
  169. // //所以计算步频的话一分钟除以walkTime 再除以一个2
  170. // stepFreq = 60 * 1000 / walkTime;
  171. // if (girl_shoes)
  172. // {
  173. // stepFreq *= 2;
  174. // }
  175. //
  176. // leaveFloorTime1 = localTime;
  177. //
  178. // stepTag = 0;
  179. // stepCount++;
  180. //}
  181. if (lastZupt == 0 && zupt == 1)
  182. {
  183. catchFloorTime = localTime;
  184. time_vector.push_back({ CATCH_TIME_STYPE, localTime});
  185. }
  186. if (zupt == 0 && lastZupt == 1)
  187. {
  188. leaveFloorTime = localTime;
  189. time_vector.push_back({ LEAVE_TIME_STYPE, localTime });
  190. if (check_time_vector(time_vector))
  191. {
  192. calStepFreq(time_vector, mean_vel);
  193. memcpy(step_vel_buff, step_vel_buff + 1, 2 * sizeof(float));
  194. step_vel_buff[2] = mean_vel;
  195. cout << "now step mean_vel is " << mean_vel << endl;
  196. mean_window_vel = (step_vel_buff[0] + step_vel_buff[1] + step_vel_buff[2]) / 3.0f;
  197. stepFreq = mean_window_vel * 60;
  198. cout << "now step mean_window_vel is " << mean_window_vel << endl;
  199. memcpy(vel_sqrt_buff, vel_sqrt_buff + 1, 2 * sizeof(float));
  200. vel_sqrt_buff[2] = vel_sqrt_sum / vel_sqrt_num * mean_vel;
  201. cout << "now step vel_sqrt_buff_mean is " << (vel_sqrt_buff[0] + vel_sqrt_buff[1] + vel_sqrt_buff[2]) / 3.0f << endl;
  202. vel_sqrt_sum = 0;
  203. vel_sqrt_num = 0;
  204. stepCount++;
  205. std::cout << "step_count : " << stepCount << endl;
  206. }
  207. }
  208. lastZupt = zupt;
  209. if (zupt == 1)
  210. zuptCount++;
  211. else
  212. zuptCount = 0;
  213. if (zuptCount > 50)
  214. {
  215. memcpy(step_vel_buff, step_vel_buff + 1, 2 * sizeof(float));
  216. step_vel_buff[2] = 0.0f;
  217. mean_window_vel = (step_vel_buff[0] + step_vel_buff[1] + step_vel_buff[2]) / 3.0f;
  218. stepFreq = mean_window_vel * 60;
  219. cout << "stop signal : now step mean_window_vel is " << mean_window_vel << endl;
  220. zuptCount = 0;
  221. memcpy(vel_sqrt_buff, vel_sqrt_buff + 1, 2 * sizeof(float));
  222. vel_sqrt_buff[2] = 0;
  223. vel_sqrt_sum = 0;
  224. vel_sqrt_num = 0;
  225. }
  226. }
  227. int FootStep::getStepFreq()
  228. {
  229. return mean_window_vel * 60;
  230. }
  231. int FootStep::getStepStatus()
  232. {
  233. if (stepFreq == 0)
  234. return 0;
  235. else if (stepFreq > 130)
  236. return 2;
  237. else
  238. return 1;
  239. }
  240. int FootStep::getStepCount()
  241. {
  242. return stepCount;
  243. }