detect_step_by_mag.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "detect_step_by_mag.h"
  2. #include "math.h"
  3. #include "stdlib.h"
  4. uint16_t mag_sqrt =0;
  5. uint8_t detect_step_by_mag(float *mag)
  6. {
  7. static uint8_t up_flag = 0;
  8. int32_t mag_temp[3];
  9. static uint16_t mag_buf[2];
  10. static uint8_t mag_index = 0;
  11. float D = 800;
  12. int step;
  13. int calculate_flag = 0;
  14. for(int i = 0; i < 3; i ++)
  15. {
  16. mag_temp[i] = (int32_t) (mag[i] *10);
  17. }
  18. mag_sqrt = (uint16_t)(sqrt((float) (mag_temp[0] * mag_temp[0] + mag_temp[1] * mag_temp[1] + mag_temp[2] * mag_temp[2])));
  19. SEGGER_RTT_printf(0,"mag_sqrt:%d...\n",mag_sqrt);
  20. if(mag_index >= 2)
  21. {
  22. mag_buf[0] = mag_buf[1];
  23. mag_index = 1;
  24. calculate_flag = 1;
  25. }
  26. mag_buf[mag_index++] = mag_sqrt;
  27. if (calculate_flag == 1)
  28. {
  29. if (up_flag == 0)
  30. {
  31. if(mag_buf[1] - mag_buf[0] > 2*D)
  32. {
  33. step = 1;
  34. up_flag = 1;
  35. }
  36. else
  37. {
  38. step = 0;
  39. up_flag = 0;
  40. }
  41. }
  42. else
  43. {
  44. step = 0;
  45. if(mag_buf[1] - mag_buf[0] > 2*D)
  46. {
  47. up_flag = 1;
  48. }
  49. else
  50. {
  51. up_flag = 0;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. step = 0;
  58. }
  59. return step;
  60. }