jump_rope_count_device.c 3.7 KB

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