all.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  5. /* Before C99, float variants of math functions are not avaliable. */
  6. static float _sqrtf(float x)
  7. {
  8. return (float)sqrt(x);
  9. }
  10. #define sqrtf _sqrtf
  11. static 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 FILE *data_recorder = NULL;
  27. static const char *column_names =
  28. "gx,gy,gz,ax,ay,az,cy,cy_fixed,"
  29. "g,a,g_std,a_std,g_up,a_up,is_out";
  30. struct schmidt
  31. {
  32. float high;
  33. float low;
  34. int value;
  35. };
  36. extern int schmidt_init(struct schmidt *schmidt, float low, float high);
  37. extern int schmidt_trig(struct schmidt *schmidt, float level);
  38. extern int schmidt_get(struct schmidt *schmidt);
  39. #define RINGBUF_MAX_SIZE 100
  40. struct ringbuf
  41. {
  42. float buf[RINGBUF_MAX_SIZE];
  43. float sum;
  44. float sum2;
  45. int cap;
  46. int p;
  47. int full;
  48. };
  49. extern int ringbuf_init(struct ringbuf *ringbuf, int cap);
  50. extern void ringbuf_push(struct ringbuf *ringbuf, float value);
  51. extern int ringbuf_size(struct ringbuf *ringbuf);
  52. extern float ringbuf_mean(struct ringbuf *ringbuf);
  53. extern float ringbuf_variance(struct ringbuf *ringbuf);
  54. extern float ringbuf_stdev(struct ringbuf *ringbuf);
  55. #define MONOTONIC_QUEUE_CAP 80
  56. typedef int (*monotonic_queue_cmp)(float, float);
  57. struct monotonic_queue
  58. {
  59. monotonic_queue_cmp cmp;
  60. float buf[MONOTONIC_QUEUE_CAP];
  61. int h, h_min, t;
  62. int len;
  63. };
  64. extern void monotonic_queue_init(struct monotonic_queue *mq,
  65. monotonic_queue_cmp cmp);
  66. extern int monotonic_queue_push(struct monotonic_queue *mq, float value);
  67. extern int monotonic_queue_pop(struct monotonic_queue *mq);
  68. extern int monotonic_queue_get_min(struct monotonic_queue *mq, float *dest);
  69. extern int monotonic_queue_get_len(struct monotonic_queue *mq);
  70. struct jump_rope_count_config
  71. {
  72. float lg, hg, la, ha, lgz, hgz, a_g_window;
  73. int cy_window, cy_crit, cy_suppress_time, wait_time, dead_zone_time;
  74. };
  75. struct jump_rope_count_device
  76. {
  77. struct schmidt trig_g, trig_a, trig_gz;
  78. struct ringbuf rbuf_g, rbuf_a;
  79. struct monotonic_queue mq_min_cy, mq_max_cy;
  80. int cy_window, cy_crit, cy_suppress_time, wait_time, dead_zone_time;
  81. int cy_suppress, wait_remain_time, dead_zone_remain_time;
  82. float last_cy, last_cy_fixed;
  83. };
  84. struct sensor_packet
  85. {
  86. float ax, ay, az, gx, gy, gz, cy;
  87. };
  88. enum jump_rope_count_result
  89. {
  90. RESULT_INACTIVE = -1,
  91. RESULT_NONE = 0,
  92. RESULT_TRIGGERED = 1
  93. };
  94. extern int jump_rope_count_device_init(struct jump_rope_count_device *dev,
  95. struct jump_rope_count_config *cfg);
  96. extern int process_packet(struct jump_rope_count_device *dev,
  97. struct sensor_packet *packet,
  98. enum jump_rope_count_result *result);
  99. static int less(float a, float b)
  100. {
  101. return a < b;
  102. }
  103. static int greater(float a, float b)
  104. {
  105. return a > b;
  106. }
  107. int jump_rope_count_device_init(struct jump_rope_count_device *dev,
  108. struct jump_rope_count_config *cfg)
  109. {
  110. int ret;
  111. ret = schmidt_init(&dev->trig_g, cfg->lg, cfg->hg);
  112. if (ret != 0)
  113. return ret;
  114. ret = schmidt_init(&dev->trig_a, cfg->la, cfg->ha);
  115. if (ret != 0)
  116. return ret;
  117. ret = schmidt_init(&dev->trig_gz, cfg->lgz, cfg->hgz);
  118. if (ret != 0)
  119. return ret;
  120. ret = ringbuf_init(&dev->rbuf_a, cfg->a_g_window);
  121. if (ret != 0)
  122. return ret;
  123. ret = ringbuf_init(&dev->rbuf_g, cfg->a_g_window);
  124. if (ret != 0)
  125. return ret;
  126. monotonic_queue_init(&dev->mq_min_cy, less);
  127. monotonic_queue_init(&dev->mq_max_cy, greater);
  128. dev->cy_window = cfg->cy_window;
  129. dev->cy_crit = cfg->cy_crit;
  130. dev->cy_suppress_time = cfg->cy_suppress_time;
  131. dev->wait_time = cfg->wait_time;
  132. dev->dead_zone_time = cfg->dead_zone_time;
  133. dev->cy_suppress = 0;
  134. dev->last_cy = dev->last_cy_fixed = 0;
  135. dev->wait_remain_time = dev->dead_zone_remain_time = 0;
  136. return 0;
  137. }
  138. static float hypot3f(float x, float y, float z)
  139. {
  140. return sqrtf(x*x + y*y + z*z);
  141. }
  142. static float angle_change(float old, float new)
  143. {
  144. float ret = fmodf(new - old, 360.0);
  145. if (ret < -180.0)
  146. ret += 360.0;
  147. if (ret > 180.0)
  148. ret -= 360.0;
  149. return ret;
  150. }
  151. int process_packet(struct jump_rope_count_device *dev,
  152. struct sensor_packet *packet,
  153. enum jump_rope_count_result *result)
  154. {
  155. int ret = 0;
  156. float min, max;
  157. float g = hypot3f(packet->gx, packet->gy, packet->gz);
  158. float a = hypot3f(packet->ax, packet->ay, packet->az);
  159. float gz = packet->gz;
  160. float cy = dev->last_cy_fixed + angle_change(dev->last_cy,
  161. packet->cy);
  162. float std_g, std_a;
  163. dev->last_cy_fixed = cy;
  164. dev->last_cy = packet->cy;
  165. ringbuf_push(&dev->rbuf_g, g);
  166. ringbuf_push(&dev->rbuf_a, a);
  167. std_g = ringbuf_stdev(&dev->rbuf_g);
  168. std_a = ringbuf_stdev(&dev->rbuf_a);
  169. schmidt_trig(&dev->trig_g, std_g);
  170. schmidt_trig(&dev->trig_a, std_a);
  171. ret = monotonic_queue_push(&dev->mq_min_cy, cy);
  172. if (ret != 0)
  173. return ret;
  174. if (monotonic_queue_get_len(&dev->mq_min_cy) > dev->cy_window)
  175. ret = monotonic_queue_pop(&dev->mq_min_cy);
  176. if (ret != 0)
  177. return ret;
  178. ret = monotonic_queue_push(&dev->mq_max_cy, cy);
  179. if (ret != 0)
  180. return ret;
  181. if (monotonic_queue_get_len(&dev->mq_max_cy) > dev->cy_window)
  182. ret = monotonic_queue_pop(&dev->mq_max_cy);
  183. if (ret != 0)
  184. return ret;
  185. ret = monotonic_queue_get_min(&dev->mq_min_cy, &min);
  186. if (ret != 0)
  187. return ret;
  188. ret = monotonic_queue_get_min(&dev->mq_max_cy, &max);
  189. if (ret != 0)
  190. return ret;
  191. if (max - min > dev->cy_crit)
  192. dev->cy_suppress = dev->cy_suppress_time;
  193. if (schmidt_get(&dev->trig_g) == 0 ||
  194. schmidt_get(&dev->trig_a) == 0 ||
  195. dev->cy_suppress > 0)
  196. {
  197. schmidt_trig(&dev->trig_gz, -INFINITY);
  198. *result = RESULT_INACTIVE;
  199. goto out;
  200. }
  201. if (schmidt_trig(&dev->trig_gz, gz) == 1 &&
  202. schmidt_get(&dev->trig_gz) == 0 &&
  203. dev->cy_suppress == 0 &&
  204. dev->dead_zone_remain_time == 0)
  205. {
  206. dev->wait_remain_time = dev->wait_time;
  207. dev->dead_zone_remain_time = dev->dead_zone_time;
  208. *result = RESULT_TRIGGERED;
  209. goto out;
  210. }
  211. if (dev->wait_remain_time)
  212. dev->wait_remain_time--;
  213. if (dev->dead_zone_remain_time)
  214. dev->dead_zone_remain_time--;
  215. *result = (dev->wait_remain_time > 0 ? RESULT_NONE :
  216. RESULT_INACTIVE);
  217. out:
  218. if (dev->cy_suppress > 0)
  219. dev->cy_suppress--;
  220. if (data_recorder == NULL)
  221. return 0;
  222. /* column names:
  223. "gx,gy,gz,ax,ay,az,cy,cy_fixed,"
  224. "g,a,g_std,a_std,g_up,a_up,is_out" */
  225. fprintf(data_recorder, "%.7f,%.7f,%.7f,",
  226. packet->gx, packet->gy, packet->gz);
  227. fprintf(data_recorder, "%.7f,%.7f,%.7f,",
  228. packet->ax, packet->ay, packet->az);
  229. fprintf(data_recorder, "%.7f,%.7f,",
  230. dev->last_cy, dev->last_cy_fixed);
  231. fprintf(data_recorder, "%.7f,%.7f,%.7f,%.7f,",
  232. g, a, std_g, std_a);
  233. fprintf(data_recorder, "%d,%d,%d\n",
  234. schmidt_get(&dev->trig_g),
  235. schmidt_get(&dev->trig_a),
  236. *result == RESULT_TRIGGERED);
  237. return 0;
  238. }
  239. void monotonic_queue_init(struct monotonic_queue *mq,
  240. monotonic_queue_cmp cmp)
  241. {
  242. mq->h = mq->h_min = mq->t = mq->len = 0;
  243. mq->cmp = cmp;
  244. }
  245. int monotonic_queue_push(struct monotonic_queue *mq, float value)
  246. {
  247. int t1;
  248. while (mq->h_min != mq->t && mq->cmp(value, mq->buf[mq->h_min]))
  249. mq->h_min = (mq->h_min + 1) % MONOTONIC_QUEUE_CAP;
  250. t1 = (mq->t + 1) % MONOTONIC_QUEUE_CAP;
  251. if (t1 == mq->h)
  252. /* over flow */
  253. return -1;
  254. mq->len++;
  255. mq->buf[mq->t] = value;
  256. mq->t = t1;
  257. return 0;
  258. }
  259. int monotonic_queue_pop(struct monotonic_queue *mq)
  260. {
  261. if (mq->h == mq->t)
  262. /* empty */
  263. return -1;
  264. mq->len--;
  265. if (mq->h == mq->h_min)
  266. mq->h_min = (mq->h_min + 1) % MONOTONIC_QUEUE_CAP;
  267. mq->h = (mq->h + 1) % MONOTONIC_QUEUE_CAP;
  268. return 0;
  269. }
  270. int monotonic_queue_get_min(struct monotonic_queue *mq, float *dest)
  271. {
  272. if (mq->h == mq->t)
  273. /* empty */
  274. return -1;
  275. *dest = mq->buf[mq->h_min];
  276. return 0;
  277. }
  278. int monotonic_queue_get_len(struct monotonic_queue *mq)
  279. {
  280. return mq->len;
  281. }
  282. int ringbuf_init(struct ringbuf *ringbuf, int cap)
  283. {
  284. if (cap > RINGBUF_MAX_SIZE)
  285. return -1;
  286. ringbuf->p = 0;
  287. ringbuf->full = 0;
  288. ringbuf->sum = ringbuf->sum2 = 0.0;
  289. ringbuf->cap = cap;
  290. return 0;
  291. }
  292. void ringbuf_push(struct ringbuf *ringbuf, float value)
  293. {
  294. if (ringbuf->full) {
  295. float old = ringbuf->buf[ringbuf->p];
  296. ringbuf->sum -= old;
  297. ringbuf->sum2 -= old * old;
  298. }
  299. ringbuf->sum += value;
  300. ringbuf->sum2 += value * value;
  301. ringbuf->buf[ringbuf->p++] = value;
  302. if (ringbuf->p == ringbuf->cap) {
  303. ringbuf->p = 0;
  304. ringbuf->full = 1;
  305. }
  306. }
  307. int ringbuf_size(struct ringbuf *ringbuf)
  308. {
  309. return ringbuf->full ? ringbuf->cap : ringbuf->p;
  310. }
  311. float ringbuf_mean(struct ringbuf *ringbuf)
  312. {
  313. int sz = ringbuf_size(ringbuf);
  314. if (sz == 0)
  315. return 0;
  316. return ringbuf->sum / sz;
  317. }
  318. float ringbuf_variance(struct ringbuf *ringbuf)
  319. {
  320. float mean;
  321. int sz = ringbuf_size(ringbuf);
  322. if (sz == 0)
  323. return 0;
  324. mean = ringbuf_mean(ringbuf);
  325. return ringbuf->sum2 / sz - mean * mean;
  326. }
  327. float ringbuf_stdev(struct ringbuf *ringbuf)
  328. {
  329. return sqrt(ringbuf_variance(ringbuf));
  330. }
  331. int schmidt_init(struct schmidt *schmidt, float low, float high)
  332. {
  333. if (low > high)
  334. return -1;
  335. schmidt->high = high;
  336. schmidt->low = low;
  337. schmidt->value = 0;
  338. return 0;
  339. }
  340. int schmidt_trig(struct schmidt *schmidt, float level)
  341. {
  342. if ((schmidt->value == 1 && level <= schmidt->low) ||
  343. (schmidt->value == 0 && level >= schmidt->high)) {
  344. schmidt->value ^= 1;
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. int schmidt_get(struct schmidt *schmidt)
  350. {
  351. return schmidt->value;
  352. }
  353. const int fs = 50;
  354. int main()
  355. {
  356. struct jump_rope_count_config cfg;
  357. struct jump_rope_count_device dev;
  358. struct sensor_packet packet;
  359. int count = 0, ret;
  360. enum jump_rope_count_result result;
  361. data_recorder = fopen("data_record.csv", "w");
  362. if (data_recorder == NULL)
  363. fprintf(stderr, "can not open data_record.csv\n");
  364. else
  365. fprintf(data_recorder, "%s\n", column_names);
  366. cfg.lg = 0.5;
  367. cfg.hg = 1.5;
  368. cfg.la = 4.0;
  369. cfg.ha = 6.0;
  370. cfg.lgz = -1.5;
  371. cfg.hgz = 2;
  372. cfg.a_g_window = 100;
  373. cfg.cy_window = fs * 0.1;
  374. cfg.cy_crit = 200;
  375. cfg.cy_suppress_time = fs * 0.2;
  376. cfg.wait_time = fs * 1;
  377. cfg.dead_zone_time = fs * 0.2;
  378. if (jump_rope_count_device_init(&dev, &cfg) != 0)
  379. abort();
  380. while (scanf("%f%f%f%f%f%f%f",
  381. &packet.gx, &packet.gy, &packet.gz,
  382. &packet.ax, &packet.ay, &packet.az,
  383. &packet.cy) == 7) {
  384. ret = process_packet(&dev, &packet, &result);
  385. if (ret != 0)
  386. abort();
  387. if (result == RESULT_INACTIVE) {
  388. if (count > 1)
  389. printf("%d\n", count - 1);
  390. count = 0;
  391. } else if (result == RESULT_TRIGGERED)
  392. count += 1;
  393. }
  394. if (count > 1)
  395. printf("%d\n", count - 1);
  396. return 0;
  397. }
  398. /* vim: set ts=8 sw=8 sts=8 noet: */