detect_step_by_mag.c 9.8 KB

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