detect_step_by_mag - 副本.c 8.0 KB

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