detect_step_by_mag.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. uint8_t RealTimeStep(int16_t front[3], int16_t back[3], int16_t acc[3])
  8. {
  9. static int16_t ShoeTouchFlag=0;
  10. static int16_t FrontTouchFlag=0;
  11. static int16_t BackTouchFlag=0;
  12. static int16_t FrontLiftPoint;
  13. static int16_t FrontDropPoint;
  14. static int16_t BackLiftPoint;
  15. static int16_t BackDropPoint;
  16. static int32_t SD;
  17. static int16_t accl;
  18. int32_t mag_temp_front[3];
  19. int32_t mag_temp_back[3];
  20. static int16_t accZ_buf[3];
  21. static uint8_t acc_index = 0;
  22. static uint16_t mag_buf_front[3];
  23. static uint16_t mag_buf_back[3];
  24. static uint8_t mag_index_front = 0;
  25. static uint8_t mag_index_back = 0;
  26. int32_t mag_sqrt_front =0;
  27. int32_t mag_sqrt_back =0;
  28. int calculate_flag = 0;
  29. int16_t step;
  30. float MV;
  31. float D;
  32. // SEGGER_RTT_printf(0,"Enter RealTimeStep!\n");
  33. for(int i = 0; i < 3; i ++)
  34. {
  35. mag_temp_front[i] = (int32_t) (front[i]);
  36. }
  37. mag_sqrt_front = (uint16_t)(sqrt((float) (mag_temp_front[0] * mag_temp_front[0] + mag_temp_front[1] * mag_temp_front[1] + mag_temp_front[2] * mag_temp_front[2])));
  38. for(int i = 0; i < 3; i ++)
  39. {
  40. mag_temp_back[i] = (int32_t) (back[i]);
  41. }
  42. mag_sqrt_back = (uint16_t)(sqrt((float) (mag_temp_back[0] * mag_temp_back[0] + mag_temp_back[1] * mag_temp_back[1] + mag_temp_back[2] * mag_temp_back[2])));
  43. // SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  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. }
  68. accZ_buf[acc_index++] = -acc[2];
  69. if(calculate_flag==1)
  70. {
  71. /*不断更新前后脚掌磁力计的上升起始点或下降起始点*/
  72. if(mag_buf_front[1] - mag_buf_front[0]>=0)
  73. {
  74. FrontDropPoint = mag_buf_front[1];
  75. }
  76. else
  77. {
  78. FrontLiftPoint = mag_buf_front[1];
  79. }
  80. if(mag_buf_back[1] - mag_buf_back[0]>=0)
  81. {
  82. BackDropPoint = mag_buf_back[1];
  83. }
  84. else
  85. {
  86. BackLiftPoint = mag_buf_back[1];
  87. }
  88. /*判断前后脚掌的状态*/
  89. if(mag_buf_front[1] - FrontLiftPoint>2200)
  90. {
  91. FrontTouchFlag = 2;
  92. }
  93. else if(mag_buf_front[1] - FrontDropPoint<-1000)
  94. {
  95. FrontTouchFlag = 0;
  96. }
  97. else if((abs(mag_buf_front[1]-mag_buf_front[0])<500)&&(abs(mag_buf_front[1]-mag_buf_front[2])<500))
  98. {
  99. FrontTouchFlag = 1;
  100. }
  101. if(mag_buf_back[1] - BackLiftPoint>2200)
  102. {
  103. BackTouchFlag = 2;
  104. }
  105. else if(mag_buf_back[1] - BackDropPoint<-1000)
  106. {
  107. BackTouchFlag = 0;
  108. }
  109. else if((abs(mag_buf_back[1]-mag_buf_back[0])<500)&&(abs(mag_buf_back[1]-mag_buf_back[2])<500))
  110. {
  111. BackTouchFlag = 1;
  112. }
  113. /*判断鞋子触地或离地*/
  114. if(ShoeTouchFlag==0)
  115. {
  116. if((FrontTouchFlag==2||BackTouchFlag==2)&&(SD>60))
  117. //if((FrontTouchFlag==2||BackTouchFlag==2))
  118. {
  119. step=1;
  120. ShoeTouchFlag=1;
  121. accl = 0;
  122. // SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf_front[2]);
  123. // SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf_front[1]);
  124. // SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf_front[0]);
  125. //
  126. // SEGGER_RTT_printf(0,"mag_back3==:%d...\n",mag_buf_back[2]);
  127. // SEGGER_RTT_printf(0,"mag_back2==:%d...\n",mag_buf_back[1]);
  128. // SEGGER_RTT_printf(0,"mag_back1==:%d...\n",mag_buf_back[0]);
  129. //
  130. // SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  131. // SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  132. // SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  133. //
  134. // SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  135. // SEGGER_RTT_printf(0,"FrontDropPoint==:%d...\n",FrontDropPoint);
  136. // SEGGER_RTT_printf(0,"BackLiftPoint==:%d...\n",BackLiftPoint);
  137. // SEGGER_RTT_printf(0,"BackDropPoint==:%d...\n",BackDropPoint);
  138. //
  139. // SEGGER_RTT_printf(0,"SD==:%d...\n",SD);
  140. }
  141. else
  142. {
  143. step=0;
  144. ShoeTouchFlag=0;
  145. }
  146. }
  147. else
  148. {
  149. step=0;
  150. //if(abs(accZ_buf[0]-2000)<600||abs(accZ_buf[1]-2000)<600||abs(accZ_buf[2]-2000)<600)
  151. if(abs(accZ_buf[0]-2000)<600&&abs(accZ_buf[1]-2000)<600&&abs(accZ_buf[2]-2000)<600)
  152. {
  153. accl++;
  154. }
  155. if((FrontTouchFlag!=2)&&(BackTouchFlag!=2)&&(accl>=2)&&(abs(accZ_buf[2]-2000)>500))
  156. {
  157. ShoeTouchFlag=0;
  158. }
  159. else
  160. {
  161. ShoeTouchFlag=1;
  162. }
  163. }
  164. /*计算加速度的方差*/
  165. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  166. D=0;
  167. for(int i = 0; i < 3; i ++)
  168. {
  169. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  170. }
  171. D = D/3;
  172. SD = sqrt((float)D);
  173. //SEGGER_RTT_printf(0,"step==:%d...\n",step);
  174. }
  175. else
  176. {
  177. step=0;
  178. }
  179. return step;
  180. }
  181. uint8_t detect_step_by_mag(int16_t *mag, int16_t acc_z)
  182. {
  183. static uint8_t up_flag = 0;
  184. int32_t mag_temp[3];
  185. static int16_t accZ_buf[3];
  186. static uint8_t acc_index = 0;
  187. static uint16_t mag_buf[3];
  188. static uint8_t mag_index = 0;
  189. static int16_t K = 30000;
  190. static int16_t counter = 0;
  191. float min;
  192. float max;
  193. int step;
  194. int calculate_flag = 0;
  195. int accZ_flag;
  196. static int16_t FrontLiftPoint;
  197. static int16_t FrontDropPoint;
  198. static int16_t FrontTouchFlag=0;
  199. float MV;
  200. float D;
  201. static int32_t SD;
  202. //SEGGER_RTT_printf(0,"mag_sqrt==:%d...\n",1000);
  203. for(int i = 0; i < 3; i ++)
  204. {
  205. mag_temp[i] = (int32_t) (mag[i]);
  206. }
  207. mag_sqrt = (uint16_t)(sqrt((float) (mag_temp[0] * mag_temp[0] + mag_temp[1] * mag_temp[1] + mag_temp[2] * mag_temp[2])));
  208. // SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  209. /*读入磁力计数据*/
  210. if(mag_index >= 3)
  211. {
  212. mag_buf[0] = mag_buf[1];
  213. mag_buf[1] = mag_buf[2];
  214. mag_index = 2;
  215. calculate_flag = 1;
  216. }
  217. mag_buf[mag_index++] = mag_sqrt;
  218. /*读入xyZ轴加速度数据*/
  219. if(acc_index >= 3)
  220. {
  221. accZ_buf[0] = accZ_buf[1];
  222. accZ_buf[1] = accZ_buf[2];
  223. acc_index = 2;
  224. }
  225. accZ_buf[acc_index++] = -acc_z;
  226. /*判断z轴加速度*/
  227. /*
  228. accZ_flag = 0;
  229. if((abs(accZ_buf[2]-accZ_buf[1])>100)||(abs(accZ_buf[1]-accZ_buf[0])>100)||(abs(accZ_buf[2]-accZ_buf[0])>100))
  230. {
  231. accZ_flag = 1;
  232. }
  233. */
  234. /*开始计算*/
  235. if (calculate_flag == 1)
  236. {
  237. /*求取最大值和最小值*/
  238. /*
  239. max = mag_buf[0];
  240. min = mag_buf[0];
  241. for(int i=0;i<2;i++)
  242. {
  243. if(max<mag_buf[i])
  244. {
  245. max = mag_buf[i];
  246. }
  247. if(min>mag_buf[i])
  248. {
  249. min = mag_buf[i];
  250. }
  251. }
  252. */
  253. // static uint32_t time =0;
  254. // if(TIME_GetTicks() - time >=1000){
  255. // time = TIME_GetTicks();
  256. // SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  257. // SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  258. // SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  259. //
  260. //
  261. // SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  262. // SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  263. // SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  264. // 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);
  265. //
  266. // }
  267. if(mag_buf[1] - mag_buf[0]>=0)
  268. {
  269. FrontDropPoint = mag_buf[1];
  270. }
  271. else
  272. {
  273. FrontLiftPoint = mag_buf[1];
  274. }
  275. /*判断前后脚掌的状态*/
  276. if(mag_buf[1] - FrontLiftPoint>2200)
  277. {
  278. FrontTouchFlag = 2;
  279. }
  280. else if(mag_buf[1] - FrontDropPoint<-1000)
  281. {
  282. FrontTouchFlag = 0;
  283. }
  284. else if((abs(mag_buf[1]-mag_buf[0])<500)&&(abs(mag_buf[1]-mag_buf[2])<500))
  285. {
  286. FrontTouchFlag = 1;
  287. }
  288. /*计算步数*/
  289. if (up_flag == 0)
  290. {
  291. if(FrontTouchFlag==2)
  292. {
  293. step = 1;
  294. up_flag = 1;
  295. // SEGGER_RTT_printf(0,"step==%d\n",step);
  296. counter = 0;
  297. //
  298. // SEGGER_RTT_printf(0,"mag_front3==:%d...\n",mag_buf[2]);
  299. // SEGGER_RTT_printf(0,"mag_front2==:%d...\n",mag_buf[1]);
  300. // SEGGER_RTT_printf(0,"mag_front1==:%d...\n",mag_buf[0]);
  301. //
  302. //
  303. // SEGGER_RTT_printf(0,"acc3==:%d...\n",accZ_buf[2]);
  304. // SEGGER_RTT_printf(0,"acc2==:%d...\n",accZ_buf[1]);
  305. // SEGGER_RTT_printf(0,"acc1==:%d...\n",accZ_buf[0]);
  306. //
  307. // SEGGER_RTT_printf(0,"FrontLiftPoint==:%d...\n",FrontLiftPoint);
  308. // SEGGER_RTT_printf(0,"FrontDropPoint==:%d...\n",FrontDropPoint);
  309. //
  310. // SEGGER_RTT_printf(0,"SD==:%d...\n",SD);
  311. //// static char string[50];
  312. // sprintf(string,"1 min=%f,mag_buf=%d\n",min,mag_buf[2]);
  313. // SEGGER_RTT_printf(0,"%s",string);
  314. // SEGGER_RTT_printf(0,"step==%d\n",step);
  315. // nrf_delay_ms(1000);
  316. // while(1);
  317. }
  318. else
  319. {
  320. step = 0;
  321. up_flag = 0;
  322. // nrf_delay_ms(1000);
  323. // NVIC_SystemReset();
  324. }
  325. }
  326. else
  327. {
  328. step = 0;
  329. if(abs(accZ_buf[0]-2000)<600&&abs(accZ_buf[1]-2000)<600&&abs(accZ_buf[2]-2000)<600)
  330. {
  331. counter++;
  332. }
  333. if((counter>=2)&&(abs(accZ_buf[2]-2000)>500))
  334. {
  335. counter=0;
  336. up_flag = 0;
  337. }
  338. }
  339. MV = (accZ_buf[0]+accZ_buf[1]+accZ_buf[2])/3;
  340. D=0;
  341. for(int i = 0; i < 3; i ++)
  342. {
  343. D = D + (accZ_buf[i]-MV)*(accZ_buf[i]-MV);
  344. }
  345. D = D/3;
  346. SD = sqrt((float)D);
  347. }
  348. else
  349. {
  350. step = 0;
  351. }
  352. return step;
  353. }