detect_step_by_mag.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "detect_step_by_mag.h"
  2. #include "math.h"
  3. #include "stdlib.h"
  4. #include "system.h"
  5. #include "nrf_delay.h"
  6. uint16_t mag_sqrt =0;
  7. uint8_t RealTimeStep(int16_t front[3], int16_t back[3], int16_t acc[3])
  8. {
  9. static int16_t FKEY=0;
  10. static int16_t BKEY=0;
  11. static int FrontLiftPoint;
  12. static int FrontDropPoint;
  13. static int BackLiftPoint;
  14. static int BackDropPoint;
  15. static uint8_t mag_index_front = 0;
  16. static uint8_t mag_index_back = 0;
  17. static int16_t Rcounter=0;
  18. static int16_t Ucounter=0;//离地计时器
  19. static int16_t ShoeTouchFlag=1;
  20. static uint8_t acc_index = 0;
  21. static int calculate_flag = 0;
  22. int32_t mag_temp_front[3];
  23. int32_t mag_temp_back[3];
  24. static int16_t accZ_buf[4];
  25. static int mag_buf_front[3];
  26. static int mag_buf_back[3];
  27. int32_t mag_sqrt_front =0;
  28. int32_t mag_sqrt_back =0;
  29. int16_t step=0;
  30. static int FrontLiftDistance;
  31. static int BackLiftDistance;
  32. //DEBUG_LOG("Enter RealTimeStep!\n");
  33. //Tcounter++;
  34. for(int i = 0; i < 3; i ++)
  35. {
  36. mag_temp_front[i] = (int32_t) (front[i]);
  37. }
  38. mag_sqrt_front = abs(mag_temp_front[2]);
  39. for(int i = 0; i < 3; i ++)
  40. {
  41. mag_temp_back[i] = (int32_t) (back[i]);
  42. }
  43. mag_sqrt_back = abs(mag_temp_back[2]);
  44. /*读入磁力计数据*/
  45. if(mag_index_front >= 3)
  46. {
  47. mag_buf_front[0] = mag_buf_front[1];
  48. mag_buf_front[1] = mag_buf_front[2];
  49. mag_index_front = 2;
  50. calculate_flag = 1;
  51. }
  52. mag_buf_front[mag_index_front++] = mag_sqrt_front;//前脚掌数据
  53. if(mag_index_back >= 3)
  54. {
  55. mag_buf_back[0] = mag_buf_back[1];
  56. mag_buf_back[1] = mag_buf_back[2];
  57. mag_index_back = 2;
  58. calculate_flag = 1;
  59. }
  60. mag_buf_back[mag_index_back++] = mag_sqrt_back;//后脚掌数据
  61. /*读入xyZ轴加速度数据*/
  62. if(acc_index >= 3)
  63. {
  64. accZ_buf[0] = accZ_buf[1];
  65. accZ_buf[1] = accZ_buf[2];
  66. acc_index = 2;
  67. calculate_flag = 1;
  68. }
  69. accZ_buf[acc_index++] = -acc[2];
  70. if(calculate_flag==1)
  71. {
  72. // DEBUG_LOG("mag_front3==:%d...\n",mag_buf_front[2]);
  73. // DEBUG_LOG("mag_back3==:%d...\n",mag_buf_back[2]);
  74. // DEBUG_LOG("FrontLiftPoint==:%d...\n",FrontLiftPoint);
  75. // DEBUG_LOG("BackLiftPoint==:%d...\n",BackLiftPoint);
  76. // DEBUG_LOG("FrontDropPoint==:%d...\n",FrontDropPoint);
  77. // DEBUG_LOG("BackDropPoint==:%d...\n",BackDropPoint);
  78. // DEBUG_LOG("FrontLiftDistance==:%d...\n",FrontLiftDistance);
  79. // DEBUG_LOG("BackLiftDistance==:%d...\n",BackLiftDistance);
  80. /*不断更新前后脚掌磁力计的上升起始点或下降起始点*/
  81. if(mag_buf_front[2] - mag_buf_front[1]>=0)
  82. {
  83. if(FKEY==0)
  84. {
  85. FrontDropPoint = mag_buf_front[2];
  86. }
  87. else
  88. {
  89. if(mag_buf_front[2]>FrontDropPoint)
  90. {
  91. FrontDropPoint = mag_buf_front[2];
  92. }
  93. }
  94. }
  95. else
  96. {
  97. if(FKEY==0)//FKEY为前脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是前脚掌磁力计不处于波形进行阶段时,如常更新上升起点。
  98. {
  99. FrontLiftPoint = mag_buf_front[2];
  100. }
  101. else
  102. {
  103. ;//当波形进行中,不改变初始上升起点,有助于过滤杂波。
  104. }
  105. }
  106. if(mag_buf_back[2] - mag_buf_back[1]>=0)
  107. {
  108. if(BKEY==0)
  109. {
  110. BackDropPoint = mag_buf_back[2];
  111. }
  112. else
  113. {
  114. if(mag_buf_back[2]>BackDropPoint)
  115. {
  116. BackDropPoint = mag_buf_back[2];
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if(BKEY==0)//BKEY为后脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是后脚掌磁力计不处于波形进行阶段时,如常更新上升起点
  123. {
  124. BackLiftPoint = mag_buf_back[2];
  125. }
  126. else
  127. {
  128. ;
  129. }
  130. }
  131. /*判断前后脚掌的状态*/
  132. if(mag_buf_front[2] - FrontLiftPoint>1100)
  133. {
  134. FKEY = 1;
  135. if(mag_buf_front[2] - FrontLiftPoint>FrontLiftDistance)
  136. {
  137. FrontLiftDistance = mag_buf_front[2] - FrontLiftPoint;
  138. }
  139. }
  140. if(mag_buf_back[2] - BackLiftPoint>1100)
  141. {
  142. BKEY = 1;
  143. if(mag_buf_back[2] - BackLiftPoint>BackLiftDistance)
  144. {
  145. BackLiftDistance = mag_buf_back[2] - BackLiftPoint;
  146. }
  147. }
  148. /*判断是否满足FKEY=0和BKEY=0,即是否满足一个升降波形*/
  149. if((mag_buf_front[2] - FrontLiftPoint<=750)||((FrontDropPoint-mag_buf_front[2]>0.4*FrontLiftDistance)&&(abs(mag_buf_front[2]-mag_buf_front[1])<100))) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  150. {
  151. FKEY=0;
  152. FrontLiftDistance = 1100;
  153. }
  154. if((mag_buf_back[2] - BackLiftPoint<=750)||((BackDropPoint-mag_buf_back[2]>0.4*BackLiftDistance)&&(abs(mag_buf_back[2]-mag_buf_back[1])<100)))//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  155. {
  156. BKEY=0;
  157. BackLiftDistance = 1100;
  158. }
  159. /*跳出死循环*/
  160. if(FrontLiftPoint==0) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  161. {
  162. FKEY=0;
  163. }
  164. if(BackLiftPoint==0)//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  165. {
  166. BKEY=0;
  167. }
  168. /*判断鞋子触地或离地*/
  169. if(ShoeTouchFlag==0)
  170. {
  171. if(Ucounter<300)
  172. {
  173. Ucounter++;
  174. }
  175. Rcounter=0;
  176. if(((FKEY==1)||(BKEY==1))&&(Ucounter>=8))
  177. {
  178. step=1;
  179. ShoeTouchFlag=1;
  180. //DEBUG_LOG("You have one step!\n");
  181. }
  182. else
  183. {
  184. step=0;
  185. ShoeTouchFlag=0;
  186. }
  187. }
  188. else
  189. {
  190. step = 0;
  191. if(Rcounter<300)
  192. {
  193. Rcounter++;
  194. }
  195. Ucounter=0;
  196. if((Rcounter>=6)&&(FKEY==0)&&((abs(accZ_buf[0]-2000)>300)||(abs(accZ_buf[1]-2000)>300)||(abs(accZ_buf[2]-2000)>300))&&(BKEY==0))
  197. {
  198. ShoeTouchFlag=0;
  199. }
  200. else
  201. {
  202. ShoeTouchFlag=1;
  203. }
  204. }
  205. }
  206. else
  207. {
  208. step=0;
  209. FrontLiftPoint = mag_buf_front[2];
  210. FrontDropPoint = mag_buf_front[2];
  211. BackLiftPoint = mag_buf_back[2];
  212. BackDropPoint = mag_buf_back[2];
  213. }
  214. return step;
  215. }
  216. uint8_t detect_step_by_mag(int16_t *mag, int16_t acc_z)
  217. {
  218. //利用前脚磁力计以及后脚来判断
  219. static int32_t mag_front[5] = {40000};
  220. static int16_t acc_front_z[5];
  221. static int32_t mag_cur_min = 40000;
  222. static int time_count;
  223. static int last_floor_status = 1;
  224. static int press_up_wait_acc_time = 0;
  225. static int up_count = 0;
  226. static int un_on_floor_count = 0;
  227. static int max_acc_z = 0;
  228. static int min_acc_z = 0;
  229. uint8_t on_floor = 0;
  230. memcpy(mag_front, mag_front + 1, 4 * sizeof(int32_t));
  231. memcpy(acc_front_z, acc_front_z + 1, 4 * sizeof(int16_t));
  232. mag_front[4] = abs(mag[2]);
  233. acc_front_z[4] = acc_z;
  234. if (time_count > 4)
  235. {
  236. //寻找上升沿
  237. // DEBUG_LOG("mag_front[3] : %d, mag_front[4] : %d , acc_front_z[4] : %d \n", mag_front[3], mag_front[4], acc_front_z[4]);
  238. if (mag_front[4] >= mag_front[3])
  239. {
  240. // DEBUG_LOG("mag_front[4] >= mag_front[3] \n");
  241. mag_cur_min = mag_cur_min > mag_front[3] ? mag_front[3] : mag_cur_min;
  242. // DEBUG_LOG("mag_cur_min : %d, mag_front[4] : %d \n", mag_cur_min, mag_front[4]);
  243. }
  244. else
  245. {
  246. // DEBUG_LOG("else ------------------------> mag_front[4] >= mag_front[3] \n");
  247. mag_cur_min = 40000;
  248. }
  249. if (mag_front[4] > mag_cur_min + 500)
  250. {
  251. up_count ++;
  252. }
  253. else
  254. {
  255. up_count = 0;
  256. }
  257. // DEBUG_LOG("up_count : %d \n", up_count);
  258. if(up_count > 0)
  259. {
  260. on_floor = 1;
  261. }
  262. if (last_floor_status == 1 && abs(acc_front_z[4] - acc_front_z[3]) < 203)
  263. {
  264. on_floor = 1;
  265. }
  266. // if (on_floor)
  267. // {
  268. // press_up_wait_acc_time = 3; //拖延3个触地状态
  269. // }
  270. // else if (press_up_wait_acc_time > 0)
  271. // {
  272. // on_floor = 1;
  273. // }
  274. //
  275. // if (press_up_wait_acc_time > 0)
  276. // {
  277. // press_up_wait_acc_time--;
  278. // }
  279. // DEBUG_LOG("on_floor : %d \n", on_floor);
  280. }
  281. else
  282. {
  283. time_count++;
  284. }
  285. uint8_t step = 0;
  286. if(last_floor_status == 0 && on_floor == 1 && max_acc_z - min_acc_z > 512)
  287. {
  288. step = 1;
  289. DEBUG_LOG("step + 1, mag_cur_min : %d ,mag_front[4]: %d \n", mag_cur_min, mag_front[4])
  290. }
  291. if(on_floor == 0)
  292. {
  293. max_acc_z = max_acc_z > acc_z ? max_acc_z : acc_z;
  294. min_acc_z = min_acc_z < acc_z ? min_acc_z : acc_z;
  295. }
  296. else
  297. {
  298. max_acc_z = acc_z;
  299. min_acc_z = acc_z;
  300. }
  301. last_floor_status = on_floor;
  302. return step;
  303. }