jump_rope_count_device.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <math.h>
  2. #include "jump_rope_count_device.h"
  3. #include "schmidt_trigger.h"
  4. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  5. /* Before C99, float variants of math functions are not avaliable. */
  6. float _sqrtf(float x)
  7. {
  8. return (float)sqrt(x);
  9. }
  10. #define sqrtf _sqrtf
  11. float _fmodf(float x, float y)
  12. {
  13. return (float)fmod(x, y);
  14. }
  15. #define fmodf _fmodf
  16. /* Before C99, INFINITY is not avaliable. */
  17. #if defined __GNUC__ && (__GNUC__ > 3 || \
  18. (__GNUC__ == 3 && __GNUC_MINOR >= 3))
  19. #define INFINITY (__builtin_inff())
  20. #else
  21. #define INFINITY 1e10000f
  22. #endif
  23. #endif
  24. static int less(float a, float b)
  25. {
  26. return a < b;
  27. }
  28. static int greater(float a, float b)
  29. {
  30. return a > b;
  31. }
  32. int jump_rope_count_device_init(struct jump_rope_count_device *dev,
  33. struct jump_rope_count_config *cfg)
  34. {
  35. int ret;
  36. ret = schmidt_init(&dev->trig_g, cfg->lg, cfg->hg);
  37. if (ret != 0)
  38. return ret;
  39. ret = schmidt_init(&dev->trig_a, cfg->la, cfg->ha);
  40. if (ret != 0)
  41. return ret;
  42. ret = schmidt_init(&dev->trig_gz, cfg->lgz, cfg->hgz);
  43. if (ret != 0)
  44. return ret;
  45. ret = ringbuf_init(&dev->rbuf_a, cfg->a_g_window);
  46. if (ret != 0)
  47. return ret;
  48. ret = ringbuf_init(&dev->rbuf_g, cfg->a_g_window);
  49. if (ret != 0)
  50. return ret;
  51. monotonic_queue_init(&dev->mq_min_cy, less);
  52. monotonic_queue_init(&dev->mq_max_cy, greater);
  53. dev->cy_window = cfg->cy_window;
  54. dev->cy_crit = cfg->cy_crit;
  55. dev->cy_suppress_time = cfg->cy_suppress_time;
  56. dev->wait_time = cfg->wait_time;
  57. dev->cy_suppress = 0;
  58. dev->last_cy = dev->last_cy_fixed = 0;
  59. return 0;
  60. }
  61. static float hypot3f(float x, float y, float z)
  62. {
  63. return sqrtf(x*x + y*y + z*z);
  64. }
  65. static float angle_change(float old, float new)
  66. {
  67. float ret = fmodf(new - old, 360.0);
  68. if (ret < -180.0)
  69. ret += 360.0;
  70. if (ret > 180.0)
  71. ret -= 360.0;
  72. return ret;
  73. }
  74. int process_packet(struct jump_rope_count_device *dev,
  75. struct sensor_packet *packet,
  76. enum jump_rope_count_result *result)
  77. {
  78. int ret = 0;
  79. float min, max;
  80. float g = hypot3f(packet->gx, packet->gy, packet->gz);
  81. float a = hypot3f(packet->ax, packet->ay, packet->az);
  82. float gz = packet->gz;
  83. float cy = dev->last_cy_fixed + angle_change(dev->last_cy,
  84. packet->cy);
  85. dev->last_cy_fixed = cy;
  86. dev->last_cy = packet->cy;
  87. ringbuf_push(&dev->rbuf_g, g);
  88. ringbuf_push(&dev->rbuf_a, a);
  89. schmidt_trig(&dev->trig_g, ringbuf_stdev(&dev->rbuf_g));
  90. schmidt_trig(&dev->trig_a, ringbuf_stdev(&dev->rbuf_a));
  91. ret = monotonic_queue_push(&dev->mq_min_cy, cy);
  92. if (ret != 0)
  93. return ret;
  94. if (monotonic_queue_get_len(&dev->mq_min_cy) > dev->cy_window)
  95. ret = monotonic_queue_pop(&dev->mq_min_cy);
  96. if (ret != 0)
  97. return ret;
  98. ret = monotonic_queue_push(&dev->mq_max_cy, cy);
  99. if (ret != 0)
  100. return ret;
  101. if (monotonic_queue_get_len(&dev->mq_max_cy) > dev->cy_window)
  102. ret = monotonic_queue_pop(&dev->mq_max_cy);
  103. if (ret != 0)
  104. return ret;
  105. ret = monotonic_queue_get_min(&dev->mq_min_cy, &min);
  106. if (ret != 0)
  107. return ret;
  108. ret = monotonic_queue_get_min(&dev->mq_max_cy, &max);
  109. if (ret != 0)
  110. return ret;
  111. if (max - min > dev->cy_crit)
  112. dev->cy_suppress = dev->cy_suppress_time;
  113. if (schmidt_get(&dev->trig_g) == 0 ||
  114. schmidt_get(&dev->trig_a) == 0 ||
  115. dev->cy_suppress > 0)
  116. {
  117. schmidt_trig(&dev->trig_gz, -INFINITY);
  118. *result = RESULT_INACTIVE;
  119. goto out;
  120. }
  121. if (schmidt_trig(&dev->trig_gz, gz) == 1 &&
  122. schmidt_get(&dev->trig_gz) == 0 &&
  123. dev->cy_suppress == 0)
  124. {
  125. dev->remain_time = dev->wait_time;
  126. *result = RESULT_TRIGGERED;
  127. goto out;
  128. }
  129. if (dev->remain_time)
  130. dev->remain_time--;
  131. *result = (dev->remain_time > 0 ? RESULT_NONE : RESULT_INACTIVE);
  132. out:
  133. if (dev->cy_suppress > 0)
  134. dev->cy_suppress--;
  135. return 0;
  136. }
  137. /* vim: set ts=8 sw=8 sts=8 noet: */