detect_step_by_mag.c 9.4 KB

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