detect_step_by_mag.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 detect_step_by_mag(int16_t *mag, int16_t acc_z)
  8. {
  9. static uint8_t up_flag = 0;
  10. int32_t mag_temp[3];
  11. static int16_t accZ_buf[3];
  12. static uint8_t acc_index = 0;
  13. static uint16_t mag_buf[3];
  14. static uint8_t mag_index = 0;
  15. int16_t D;
  16. static int16_t K = 30000;
  17. static int16_t counter = 0;
  18. float min;
  19. float max;
  20. int step;
  21. int calculate_flag = 0;
  22. int accZ_flag;
  23. //SEGGER_RTT_printf(0,"mag_sqrt==:%d...\n",1000);
  24. D =-1500;
  25. for(int i = 0; i < 3; i ++)
  26. {
  27. mag_temp[i] = (int32_t) (mag[i]);
  28. }
  29. mag_sqrt = (uint16_t)(sqrt((float) (mag_temp[0] * mag_temp[0] + mag_temp[1] * mag_temp[1] + mag_temp[2] * mag_temp[2])));
  30. // SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  31. /*读入磁力计数据*/
  32. if(mag_index >= 3)
  33. {
  34. mag_buf[0] = mag_buf[1];
  35. mag_buf[1] = mag_buf[2];
  36. mag_index = 2;
  37. calculate_flag = 1;
  38. }
  39. mag_buf[mag_index++] = mag_sqrt;
  40. /*读入xyZ轴加速度数据*/
  41. if(acc_index >= 3)
  42. {
  43. accZ_buf[0] = accZ_buf[1];
  44. accZ_buf[1] = accZ_buf[2];
  45. acc_index = 2;
  46. }
  47. accZ_buf[acc_index++] = acc_z;
  48. /*计算站稳时的磁力计数值*/
  49. /*
  50. if((abs(accZ_buf[2]-accZ_buf[1])<50)&&(abs(accZ_buf[1]-accZ_buf[0])<50))
  51. {
  52. mag_stable = (mag_buf[0]+mag_buf[1]+mag_buf[2])/3;
  53. SEGGER_RTT_printf(0,"mag_stable==:%d...\n",mag_stable);
  54. }
  55. */
  56. //SEGGER_RTT_printf(0,"mag_buf[2]==:%d...accZ_buf[3] = %d \n",mag_sqrt, IMU_GetAcc_Z());
  57. /*判断z轴加速度*/
  58. accZ_flag = 0;
  59. 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))
  60. {
  61. accZ_flag = 1;
  62. }
  63. /*开始计算*/
  64. if (calculate_flag == 1)
  65. {
  66. /*求取最大值和最小值*/
  67. max = mag_buf[0];
  68. min = mag_buf[0];
  69. for(int i=0;i<2;i++)
  70. {
  71. if(max<mag_buf[i])
  72. {
  73. max = mag_buf[i];
  74. }
  75. if(min>mag_buf[i])
  76. {
  77. min = mag_buf[i];
  78. }
  79. }
  80. /*计算步数*/
  81. if (up_flag == 0)
  82. {
  83. //if((mag_buf[2] - min > 1600)&&(accZ_flag==1))//min
  84. //K = mag_buf[2] - min;
  85. //SEGGER_RTT_printf(0,"K==%d\n",K);
  86. counter = 0;
  87. if((mag_buf[2] - min > 800)&&(accZ_flag==1))
  88. {
  89. step = 1;
  90. up_flag = 1;
  91. K = min;
  92. SEGGER_RTT_printf(0,"step==%d\n",step);
  93. // static char string[50];
  94. // sprintf(string,"1 min=%f,mag_buf=%d\n",min,mag_buf[2]);
  95. // SEGGER_RTT_printf(0,"%s",string);
  96. // SEGGER_RTT_printf(0,"step==%d\n",step);
  97. // nrf_delay_ms(1000);
  98. // while(1);
  99. }
  100. else
  101. {
  102. step = 0;
  103. up_flag = 0;
  104. // nrf_delay_ms(1000);
  105. // NVIC_SystemReset();
  106. }
  107. }
  108. else
  109. {
  110. step = 0;
  111. counter++;
  112. if(counter==15)
  113. {
  114. counter=0;
  115. up_flag = 0;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. step = 0;
  122. }
  123. return step;
  124. }