detect_step_by_mag.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include "detect_step_by_mag.h"
  2. #include "math.h"
  3. #include "stdlib.h"
  4. #include "hal_imu.h"
  5. #include "nrf_delay.h"
  6. uint16_t mag_sqrt =0;
  7. //static int16_t ShoeTouchFlag=1;
  8. int16_t Get_ShoeTouchFlag(void)
  9. {
  10. return ShoeTouchFlag;
  11. }
  12. //static int16_t Rcounter=0;
  13. int16_t Get_Rcounter(void)
  14. {
  15. return Rcounter;
  16. }
  17. static int16_t Tcounter=0;
  18. int16_t Get_Tcounter(void)
  19. {
  20. return Tcounter;
  21. }
  22. //static int16_t FKEY=0;
  23. //static int16_t BKEY=0;
  24. int16_t Get_FKEY(void)
  25. {
  26. return FKEY;
  27. }
  28. int16_t Get_BKEY(void)
  29. {
  30. return BKEY;
  31. }
  32. static int RealFrontLiftPoint;
  33. static int RealBackLiftPoint;
  34. int32_t Get_RealFrontLiftPoint(void)
  35. {
  36. return RealFrontLiftPoint;
  37. }
  38. int32_t Get_RealBackLiftPoint(void)
  39. {
  40. return RealBackLiftPoint;
  41. }
  42. uint8_t RealTimeStep(int16_t front[3], int16_t back[3], int16_t acc[3])
  43. {
  44. //static int FrontLiftPoint;
  45. //static int FrontDropPoint;
  46. //static int BackLiftPoint;
  47. //static int BackDropPoint;
  48. static int32_t SD;
  49. //static int16_t Rcounter;
  50. int32_t mag_temp_front[3];
  51. int32_t mag_temp_back[3];
  52. static int16_t accZ_buf[4];
  53. //static uint8_t acc_index = 0;
  54. static int mag_buf_front[3];
  55. static int mag_buf_back[3];
  56. //static uint8_t mag_index_front = 0;
  57. //static uint8_t mag_index_back = 0;
  58. int32_t mag_sqrt_front =0;
  59. int32_t mag_sqrt_back =0;
  60. //static int calculate_flag = 0;
  61. int16_t step=0;
  62. float MV;
  63. float D;
  64. //static int16_t Ucounter=0;//离地计时器
  65. //SEGGER_RTT_printf(0,"Enter RealTimeStep!\n");
  66. Tcounter++;
  67. for(int i = 0; i < 3; i ++)
  68. {
  69. mag_temp_front[i] = (int32_t) (front[i]);
  70. }
  71. mag_sqrt_front = abs(mag_temp_front[2]);
  72. for(int i = 0; i < 3; i ++)
  73. {
  74. mag_temp_back[i] = (int32_t) (back[i]);
  75. }
  76. mag_sqrt_back = abs(mag_temp_back[2]);
  77. /*读入磁力计数据*/
  78. if(mag_index_front >= 3)
  79. {
  80. mag_buf_front[0] = mag_buf_front[1];
  81. mag_buf_front[1] = mag_buf_front[2];
  82. mag_index_front = 2;
  83. calculate_flag = 1;
  84. }
  85. mag_buf_front[mag_index_front++] = mag_sqrt_front;//前脚掌数据
  86. if(mag_index_back >= 3)
  87. {
  88. mag_buf_back[0] = mag_buf_back[1];
  89. mag_buf_back[1] = mag_buf_back[2];
  90. mag_index_back = 2;
  91. calculate_flag = 1;
  92. }
  93. mag_buf_back[mag_index_back++] = mag_sqrt_back;//后脚掌数据
  94. /*读入xyZ轴加速度数据*/
  95. if(acc_index >= 3)
  96. {
  97. accZ_buf[0] = accZ_buf[1];
  98. accZ_buf[1] = accZ_buf[2];
  99. acc_index = 2;
  100. calculate_flag = 1;
  101. }
  102. accZ_buf[acc_index++] = -acc[2];
  103. if(calculate_flag==1)
  104. {
  105. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf_front[2]);
  106. SEGGER_RTT_printf(0,"mag_back3==:%d...\n",mag_buf_back[2]);
  107. SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  108. SEGGER_RTT_printf(0,"BackLiftPoint==:%d...\n",BackLiftPoint);
  109. /*不断更新前后脚掌磁力计的上升起始点或下降起始点*/
  110. if(mag_buf_front[2] - mag_buf_front[1]>=0)
  111. {
  112. if(FKEY==0)
  113. {
  114. FrontDropPoint = mag_buf_front[2];
  115. }
  116. else
  117. {
  118. if(mag_buf_front[2]>FrontDropPoint)
  119. {
  120. FrontDropPoint = mag_buf_front[2];
  121. }
  122. }
  123. }
  124. else
  125. {
  126. if(FKEY==0)//FKEY为前脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是前脚掌磁力计不处于波形进行阶段时,如常更新上升起点。
  127. {
  128. FrontLiftPoint = mag_buf_front[2];
  129. }
  130. else
  131. {
  132. ;//当波形进行中,不改变初始上升起点,有助于过滤杂波。
  133. }
  134. }
  135. if(mag_buf_back[2] - mag_buf_back[1]>=0)
  136. {
  137. if(BKEY==0)
  138. {
  139. BackDropPoint = mag_buf_back[2];
  140. }
  141. else
  142. {
  143. if(mag_buf_back[2]>BackDropPoint)
  144. {
  145. BackDropPoint = mag_buf_back[2];
  146. }
  147. }
  148. }
  149. else
  150. {
  151. if(BKEY==0)//BKEY为后脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是后脚掌磁力计不处于波形进行阶段时,如常更新上升起点
  152. {
  153. BackLiftPoint = mag_buf_back[2];
  154. }
  155. else
  156. {
  157. ;
  158. }
  159. }
  160. /*判断前后脚掌的状态*/
  161. if(mag_buf_front[2] - FrontLiftPoint>2200)
  162. {
  163. FKEY = 1;
  164. if(mag_buf_front[2] - FrontLiftPoint>FrontLiftDistance)
  165. {
  166. FrontLiftDistance = mag_buf_front[2] - FrontLiftPoint;
  167. }
  168. }
  169. if(mag_buf_back[2] - BackLiftPoint>2200)
  170. {
  171. BKEY = 1;
  172. if(mag_buf_back[2] - BackLiftPoint>BackLiftDistance)
  173. {
  174. BackLiftDistance = mag_buf_back[2] - BackLiftPoint;
  175. }
  176. }
  177. /*判断是否满足FKEY=0和BKEY=0,即是否满足一个升降波形*/
  178. if((mag_buf_front[2] - FrontLiftPoint<=1500)||(FrontDropPoint-mag_buf_front[2]>0.45*FrontLiftDistance)) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  179. //if(mag_buf_front[2] - FrontLiftPoint<=1500)
  180. {
  181. FKEY=0;
  182. FrontLiftDistance = 2200;
  183. }
  184. if((mag_buf_back[2] - BackLiftPoint<=1500)||(BackDropPoint-mag_buf_back[2]>0.45*BackLiftDistance))//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  185. //if(mag_buf_back[2] - BackLiftPoint<=1500)
  186. {
  187. BKEY=0;
  188. BackLiftDistance = 2200;
  189. }
  190. /*跳出死循环*/
  191. if(FrontLiftPoint==0) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  192. {
  193. FKEY=0;
  194. }
  195. if(BackLiftPoint==0)//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  196. {
  197. BKEY=0;
  198. }
  199. /*判断鞋子触地或离地*/
  200. if(ShoeTouchFlag==0)
  201. {
  202. if(Ucounter<300)
  203. {
  204. Ucounter++;
  205. }
  206. Rcounter=0;
  207. if(((FKEY==1)||(BKEY==1))&&(Ucounter>=6))
  208. {
  209. step=1;
  210. ShoeTouchFlag=1;
  211. SEGGER_RTT_printf(0,"You have one step!\n");
  212. }
  213. else
  214. {
  215. step=0;
  216. ShoeTouchFlag=0;
  217. }
  218. }
  219. else
  220. {
  221. step = 0;
  222. if(Rcounter<300)
  223. {
  224. Rcounter++;
  225. }
  226. Ucounter=0;
  227. if((Rcounter>=6)&&(FKEY==0)&&((abs(accZ_buf[0]-2000)>250)||(abs(accZ_buf[1]-2000)>250)||(abs(accZ_buf[2]-2000)>250))&&(BKEY==0))
  228. {
  229. ShoeTouchFlag=0;
  230. }
  231. else
  232. {
  233. ShoeTouchFlag=1;
  234. }
  235. /*
  236. if(Rcounter==300)//防止卡死
  237. {
  238. if(FrontDropPoint-mag_buf_front[2]>0.45*FrontLiftDistance)
  239. {
  240. FKEY=0;
  241. }
  242. if(BackDropPoint-mag_buf_back[2]>0.45*BackLiftDistance)
  243. {
  244. BKEY=0;
  245. }
  246. }
  247. */
  248. }
  249. SEGGER_RTT_printf(0,"Rcounter==:%d...\n",Rcounter);
  250. /*计算加速度的方差*/
  251. /*
  252. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  253. D=0;
  254. for(int i = 0; i < 3; i ++)
  255. {
  256. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  257. }
  258. D = D*0.33333333;
  259. SD = sqrt((float)D);
  260. */
  261. }
  262. else
  263. {
  264. step=0;
  265. FrontLiftPoint = mag_buf_front[2];
  266. FrontDropPoint = mag_buf_front[2];
  267. BackLiftPoint = mag_buf_back[2];
  268. BackDropPoint = mag_buf_back[2];
  269. SEGGER_RTT_printf(0,"Just Enter The Game! FrontLiftPoint==:%d...\n",FrontLiftPoint);
  270. SEGGER_RTT_printf(0,"Just Enter The Game! BackLiftPoint==:%d...\n",BackLiftPoint);
  271. }
  272. RealFrontLiftPoint = FrontLiftPoint;
  273. RealBackLiftPoint = BackLiftPoint;
  274. return step;
  275. }
  276. void ClearRealTimeStep()
  277. {
  278. FrontLiftPoint=30000;
  279. FrontDropPoint=30000;
  280. BackLiftPoint=30000;
  281. BackDropPoint=30000;
  282. calculate_flag=0;
  283. Ucounter=0;
  284. ShoeTouchFlag=1;
  285. Rcounter=0;
  286. FKEY=0;
  287. BKEY=0;
  288. acc_index = 0;
  289. mag_index_front = 0;
  290. mag_index_back = 0;
  291. FrontLiftDistance = 2200;
  292. BackLiftDistance = 2200;
  293. Lock = 0;
  294. }
  295. uint8_t detect_step_by_mag(int16_t *mag, int16_t acc_z)
  296. {
  297. static uint8_t up_flag = 0;
  298. int32_t mag_temp[3];
  299. static int16_t accZ_buf[3];
  300. static uint8_t acc_index = 0;
  301. //static uint16_t mag_buf[3];
  302. static int mag_buf[3];
  303. static uint8_t mag_index = 0;
  304. int step;
  305. int calculate_flag = 0;
  306. static int16_t FrontLiftPoint;
  307. static int16_t FrontDropPoint;
  308. static int16_t FrontTouchFlag=0;
  309. static int16_t PreFrontTouchFlag=0;
  310. float MV;
  311. float D;
  312. static int32_t SD;
  313. static int16_t counter=0;
  314. /*
  315. static float K;
  316. static float L;
  317. static float a=0;
  318. static float v=0;
  319. static float s=0;
  320. K = 9.8*0.0001*0.0005;//单位分别为厘米和毫秒
  321. */
  322. //SEGGER_RTT_printf(0,"mag_sqrt==:%d...\n",1000);
  323. for(int i = 0; i < 3; i ++)
  324. {
  325. mag_temp[i] = (int32_t) (mag[i]);
  326. }
  327. //mag_sqrt = (uint16_t)(sqrt((float) (mag_temp[0] * mag_temp[0] + mag_temp[1] * mag_temp[1] + mag_temp[2] * mag_temp[2])));
  328. mag_sqrt = abs(mag_temp[2]);
  329. // SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  330. /*读入磁力计数据*/
  331. if(mag_index >= 3)
  332. {
  333. mag_buf[0] = mag_buf[1];
  334. mag_buf[1] = mag_buf[2];
  335. mag_index = 2;
  336. calculate_flag = 1;
  337. }
  338. mag_buf[mag_index++] = mag_sqrt;
  339. /*读入xyZ轴加速度数据*/
  340. if(acc_index >= 3)
  341. {
  342. accZ_buf[0] = accZ_buf[1];
  343. accZ_buf[1] = accZ_buf[2];
  344. acc_index = 2;
  345. }
  346. accZ_buf[acc_index++] = -acc_z;
  347. /*开始计算*/
  348. if (calculate_flag == 1)
  349. {
  350. //static uint32_t time =0;
  351. /*
  352. if(TIME_GetTicks() - time >=1000){
  353. time = TIME_GetTicks();
  354. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  355. SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  356. SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  357. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  358. SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  359. SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  360. SEGGER_RTT_printf(0,"f_mx=%d,f_my=%d,f_mz=%d,acc_z:%d\r\n",mag[0],mag[1],mag[2],acc_z);
  361. }
  362. */
  363. if(mag_buf[1] - mag_buf[0]>=0)
  364. {
  365. FrontDropPoint = mag_buf[1];
  366. }
  367. else
  368. {
  369. FrontLiftPoint = mag_buf[1];
  370. }
  371. /*判断前后脚掌的状态*/
  372. if(mag_buf[1] - FrontLiftPoint>2000)
  373. {
  374. FrontTouchFlag = 2;
  375. }
  376. else if(mag_buf[1] - FrontDropPoint<-1000)
  377. {
  378. FrontTouchFlag = 0;
  379. }
  380. else if((abs(mag_buf[1]-mag_buf[0])<500)&&(abs(mag_buf[1]-mag_buf[2])<500))
  381. {
  382. FrontTouchFlag = 1;
  383. }
  384. /*计算步数*/
  385. if (up_flag == 0)
  386. {
  387. if(((FrontTouchFlag==2)&&(PreFrontTouchFlag!=2))&&(SD>60))
  388. {
  389. step = 1;
  390. up_flag = 1;
  391. counter=0;
  392. //a=0;
  393. //v=0;
  394. //s=0;
  395. /*
  396. SEGGER_RTT_printf(0,"step==%d\n",step);
  397. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  398. SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  399. SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  400. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  401. SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  402. SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  403. SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  404. SEGGER_RTT_printf(0,"FrontDropPoint==:%d...\n",FrontDropPoint);
  405. SEGGER_RTT_printf(0,"SD==:%d...\n",SD);
  406. */
  407. // static char string[50];
  408. // sprintf(string,"1 min=%f,mag_buf=%d\n",min,mag_buf[2]);
  409. // SEGGER_RTT_printf(0,"%s",string);
  410. // SEGGER_RTT_printf(0,"step==%d\n",step);
  411. // nrf_delay_ms(1000);
  412. // while(1);
  413. }
  414. else
  415. {
  416. step = 0;
  417. up_flag = 0;
  418. // nrf_delay_ms(1000);
  419. // NVIC_SystemReset();
  420. }
  421. }
  422. else
  423. {
  424. step = 0;
  425. counter++;
  426. //a = (accZ_buf[2]-L)*K;
  427. //v = v + a*10;
  428. //s = s + v*10;
  429. if((FrontTouchFlag!=2)&&(((accZ_buf[0]<1800)&&(accZ_buf[1]<1800)&&(accZ_buf[2]<1800))||((accZ_buf[0]>2500)&&(accZ_buf[1]>2500)&&(accZ_buf[2]>2500)))&&(counter>=12))
  430. {
  431. up_flag=0;
  432. }
  433. }
  434. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  435. D=0;
  436. for(int i = 0; i < 3; i ++)
  437. {
  438. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  439. }
  440. D = D/3;
  441. SD = sqrt((float)D);
  442. PreFrontTouchFlag = FrontTouchFlag;
  443. /*
  444. static char string[100];
  445. sprintf(string,"=====>time consuming:%02f us\r\n",(float)a);
  446. SEGGER_RTT_printf(0,"%s",string);
  447. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  448. sprintf(string,"=====>time consuming:%02f us\r\n",(float)v);
  449. SEGGER_RTT_printf(0,"%s",string);
  450. */
  451. }
  452. else
  453. {
  454. step = 0;
  455. }
  456. return step;
  457. }