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. //SEGGER_RTT_printf(0,"mag_index_front==:%d...\n",mag_index_front);
  104. //SEGGER_RTT_printf(0,"mag_index_back==:%d...\n",mag_index_back);
  105. //SEGGER_RTT_printf(0,"calculate_flag==:%d...\n",calculate_flag);
  106. if(calculate_flag==1)
  107. {
  108. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf_front[2]);
  109. SEGGER_RTT_printf(0,"mag_back3==:%d...\n",mag_buf_back[2]);
  110. SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  111. SEGGER_RTT_printf(0,"BackLiftPoint==:%d...\n",BackLiftPoint);
  112. /*不断更新前后脚掌磁力计的上升起始点或下降起始点*/
  113. if(mag_buf_front[2] - mag_buf_front[1]>=0)
  114. {
  115. //if(FKEY==0)
  116. //{
  117. FrontDropPoint = mag_buf_front[2];
  118. //}
  119. //else
  120. //{
  121. //if(mag_buf_front[2]>FrontDropPoint)
  122. //{
  123. //FrontDropPoint = mag_buf_front[2];
  124. //}
  125. //}
  126. }
  127. else
  128. {
  129. if(FKEY==0)//FKEY为前脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是前脚掌磁力计不处于波形进行阶段时,如常更新上升起点。
  130. {
  131. FrontLiftPoint = mag_buf_front[2];
  132. }
  133. else
  134. {
  135. ;//当波形进行中,不改变初始上升起点,有助于过滤杂波。
  136. }
  137. }
  138. if(mag_buf_back[2] - mag_buf_back[1]>=0)
  139. {
  140. //if(BKEY==0)
  141. //{
  142. BackDropPoint = mag_buf_back[2];
  143. //}
  144. //else
  145. //{
  146. //if(mag_buf_back[2]>BackDropPoint)
  147. //{
  148. //BackDropPoint = mag_buf_back[2];
  149. //}
  150. //}
  151. }
  152. else
  153. {
  154. if(BKEY==0)//BKEY为后脚波形状态变量,1代表波形进行中,0代表波形结束。该语句意思是后脚掌磁力计不处于波形进行阶段时,如常更新上升起点
  155. {
  156. BackLiftPoint = mag_buf_back[2];
  157. }
  158. else
  159. {
  160. ;
  161. }
  162. }
  163. /*判断前后脚掌的状态*/
  164. if(mag_buf_front[2] - FrontLiftPoint>2200)
  165. {
  166. FKEY = 1;
  167. /*
  168. if(mag_buf_front[2] - FrontLiftPoint>FrontLiftDistance)
  169. {
  170. FrontLiftDistance = mag_buf_front[2] - FrontLiftPoint;
  171. }
  172. */
  173. }
  174. if(mag_buf_back[2] - BackLiftPoint>2200)
  175. {
  176. BKEY = 1;
  177. /*
  178. if(mag_buf_back[2] - BackLiftPoint>BackLiftDistance)
  179. {
  180. BackLiftDistance = mag_buf_back[2] - BackLiftPoint;
  181. }
  182. */
  183. }
  184. /*判断是否满足FKEY=0和BKEY=0,即是否满足一个升降波形*/
  185. //if((mag_buf_front[2] - FrontLiftPoint<=1500)||(FrontDropPoint-mag_buf_front[2]>0.68*FrontLiftDistance)) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  186. if(mag_buf_front[2] - FrontLiftPoint<=1500)
  187. {
  188. FKEY=0;
  189. //FrontLiftDistance = 2200;
  190. }
  191. //if((mag_buf_back[2] - BackLiftPoint<=1500)||(BackDropPoint-mag_buf_back[2]>0.68*BackLiftDistance))//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  192. if(mag_buf_back[2] - BackLiftPoint<=1500)
  193. {
  194. BKEY=0;
  195. //BackLiftDistance = 2200;
  196. }
  197. /*跳出死循环*/
  198. if(FrontLiftPoint==0) //当磁力计回降到小于上升起点+1000的数值时,FKEY归零,预示前脚波形结束。
  199. {
  200. FKEY=0;
  201. }
  202. if(BackLiftPoint==0)//当磁力计回降到小于上升起点+1000的数值时,BKEY归零,预示后脚波形结束。
  203. {
  204. BKEY=0;
  205. }
  206. /*判断鞋子触地或离地*/
  207. if(ShoeTouchFlag==0)
  208. {
  209. Ucounter++;
  210. Rcounter=0;
  211. if(((FKEY==1)||(BKEY==1))&&(Ucounter>=6))
  212. {
  213. step=1;
  214. ShoeTouchFlag=1;
  215. }
  216. else
  217. {
  218. step=0;
  219. ShoeTouchFlag=0;
  220. }
  221. }
  222. else
  223. {
  224. step = 0;
  225. Rcounter++;
  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. Lock=0;
  231. }
  232. else
  233. {
  234. ShoeTouchFlag=1;
  235. }
  236. if((Rcounter>3*100)&&(Lock==0))//防止卡死
  237. {
  238. FrontLiftPoint= FrontLiftPoint + 3000;
  239. BackLiftPoint = BackLiftPoint + 3000;
  240. Lock=1;
  241. }
  242. }
  243. /*计算加速度的方差*/
  244. /*
  245. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  246. D=0;
  247. for(int i = 0; i < 3; i ++)
  248. {
  249. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  250. }
  251. D = D*0.33333333;
  252. SD = sqrt((float)D);
  253. */
  254. }
  255. else
  256. {
  257. step=0;
  258. FrontLiftPoint = mag_buf_front[2];
  259. FrontDropPoint = mag_buf_front[2];
  260. BackLiftPoint = mag_buf_back[2];
  261. BackDropPoint = mag_buf_back[2];
  262. SEGGER_RTT_printf(0,"Just Enter The Game! FrontLiftPoint==:%d...\n",FrontLiftPoint);
  263. SEGGER_RTT_printf(0,"Just Enter The Game! BackLiftPoint==:%d...\n",BackLiftPoint);
  264. }
  265. RealFrontLiftPoint = FrontLiftPoint;
  266. RealBackLiftPoint = BackLiftPoint;
  267. return step;
  268. }
  269. void ClearRealTimeStep()
  270. {
  271. FrontLiftPoint=30000;
  272. FrontDropPoint=30000;
  273. BackLiftPoint=30000;
  274. BackDropPoint=30000;
  275. calculate_flag=0;
  276. Ucounter=0;
  277. ShoeTouchFlag=1;
  278. Rcounter=0;
  279. FKEY=0;
  280. BKEY=0;
  281. acc_index = 0;
  282. mag_index_front = 0;
  283. mag_index_back = 0;
  284. FrontLiftDistance = 2200;
  285. BackLiftDistance = 2200;
  286. Lock = 0;
  287. }
  288. uint8_t detect_step_by_mag(int16_t *mag, int16_t acc_z)
  289. {
  290. static uint8_t up_flag = 0;
  291. int32_t mag_temp[3];
  292. static int16_t accZ_buf[3];
  293. static uint8_t acc_index = 0;
  294. //static uint16_t mag_buf[3];
  295. static int mag_buf[3];
  296. static uint8_t mag_index = 0;
  297. int step;
  298. int calculate_flag = 0;
  299. static int16_t FrontLiftPoint;
  300. static int16_t FrontDropPoint;
  301. static int16_t FrontTouchFlag=0;
  302. static int16_t PreFrontTouchFlag=0;
  303. float MV;
  304. float D;
  305. static int32_t SD;
  306. static int16_t counter=0;
  307. /*
  308. static float K;
  309. static float L;
  310. static float a=0;
  311. static float v=0;
  312. static float s=0;
  313. K = 9.8*0.0001*0.0005;//单位分别为厘米和毫秒
  314. */
  315. //SEGGER_RTT_printf(0,"mag_sqrt==:%d...\n",1000);
  316. for(int i = 0; i < 3; i ++)
  317. {
  318. mag_temp[i] = (int32_t) (mag[i]);
  319. }
  320. //mag_sqrt = (uint16_t)(sqrt((float) (mag_temp[0] * mag_temp[0] + mag_temp[1] * mag_temp[1] + mag_temp[2] * mag_temp[2])));
  321. mag_sqrt = abs(mag_temp[2]);
  322. // SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  323. /*读入磁力计数据*/
  324. if(mag_index >= 3)
  325. {
  326. mag_buf[0] = mag_buf[1];
  327. mag_buf[1] = mag_buf[2];
  328. mag_index = 2;
  329. calculate_flag = 1;
  330. }
  331. mag_buf[mag_index++] = mag_sqrt;
  332. /*读入xyZ轴加速度数据*/
  333. if(acc_index >= 3)
  334. {
  335. accZ_buf[0] = accZ_buf[1];
  336. accZ_buf[1] = accZ_buf[2];
  337. acc_index = 2;
  338. }
  339. accZ_buf[acc_index++] = -acc_z;
  340. /*开始计算*/
  341. if (calculate_flag == 1)
  342. {
  343. //static uint32_t time =0;
  344. /*
  345. if(TIME_GetTicks() - time >=1000){
  346. time = TIME_GetTicks();
  347. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  348. SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  349. SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  350. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  351. SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  352. SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  353. 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);
  354. }
  355. */
  356. if(mag_buf[1] - mag_buf[0]>=0)
  357. {
  358. FrontDropPoint = mag_buf[1];
  359. }
  360. else
  361. {
  362. FrontLiftPoint = mag_buf[1];
  363. }
  364. /*判断前后脚掌的状态*/
  365. if(mag_buf[1] - FrontLiftPoint>2000)
  366. {
  367. FrontTouchFlag = 2;
  368. }
  369. else if(mag_buf[1] - FrontDropPoint<-1000)
  370. {
  371. FrontTouchFlag = 0;
  372. }
  373. else if((abs(mag_buf[1]-mag_buf[0])<500)&&(abs(mag_buf[1]-mag_buf[2])<500))
  374. {
  375. FrontTouchFlag = 1;
  376. }
  377. /*计算步数*/
  378. if (up_flag == 0)
  379. {
  380. if(((FrontTouchFlag==2)&&(PreFrontTouchFlag!=2))&&(SD>60))
  381. {
  382. step = 1;
  383. up_flag = 1;
  384. counter=0;
  385. //a=0;
  386. //v=0;
  387. //s=0;
  388. /*
  389. SEGGER_RTT_printf(0,"step==%d\n",step);
  390. SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  391. SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  392. SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  393. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  394. SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  395. SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  396. SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  397. SEGGER_RTT_printf(0,"FrontDropPoint==:%d...\n",FrontDropPoint);
  398. SEGGER_RTT_printf(0,"SD==:%d...\n",SD);
  399. */
  400. // static char string[50];
  401. // sprintf(string,"1 min=%f,mag_buf=%d\n",min,mag_buf[2]);
  402. // SEGGER_RTT_printf(0,"%s",string);
  403. // SEGGER_RTT_printf(0,"step==%d\n",step);
  404. // nrf_delay_ms(1000);
  405. // while(1);
  406. }
  407. else
  408. {
  409. step = 0;
  410. up_flag = 0;
  411. // nrf_delay_ms(1000);
  412. // NVIC_SystemReset();
  413. }
  414. }
  415. else
  416. {
  417. step = 0;
  418. counter++;
  419. //a = (accZ_buf[2]-L)*K;
  420. //v = v + a*10;
  421. //s = s + v*10;
  422. 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))
  423. {
  424. up_flag=0;
  425. }
  426. }
  427. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  428. D=0;
  429. for(int i = 0; i < 3; i ++)
  430. {
  431. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  432. }
  433. D = D/3;
  434. SD = sqrt((float)D);
  435. PreFrontTouchFlag = FrontTouchFlag;
  436. /*
  437. static char string[100];
  438. sprintf(string,"=====>time consuming:%02f us\r\n",(float)a);
  439. SEGGER_RTT_printf(0,"%s",string);
  440. SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  441. sprintf(string,"=====>time consuming:%02f us\r\n",(float)v);
  442. SEGGER_RTT_printf(0,"%s",string);
  443. */
  444. }
  445. else
  446. {
  447. step = 0;
  448. }
  449. return step;
  450. }