all.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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;
  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;
  81. int cy_suppress, 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->cy_suppress = 0;
  133. dev->last_cy = dev->last_cy_fixed = 0;
  134. return 0;
  135. }
  136. static float hypot3f(float x, float y, float z)
  137. {
  138. return sqrtf(x*x + y*y + z*z);
  139. }
  140. static float angle_change(float old, float new)
  141. {
  142. float ret = fmodf(new - old, 360.0);
  143. if (ret < -180.0)
  144. ret += 360.0;
  145. if (ret > 180.0)
  146. ret -= 360.0;
  147. return ret;
  148. }
  149. int process_packet(struct jump_rope_count_device *dev,
  150. struct sensor_packet *packet,
  151. enum jump_rope_count_result *result)
  152. {
  153. int ret = 0;
  154. float min, max;
  155. float g = hypot3f(packet->gx, packet->gy, packet->gz);
  156. float a = hypot3f(packet->ax, packet->ay, packet->az);
  157. float gz = packet->gz;
  158. float cy = dev->last_cy_fixed + angle_change(dev->last_cy,
  159. packet->cy);
  160. float std_g, std_a;
  161. dev->last_cy_fixed = cy;
  162. dev->last_cy = packet->cy;
  163. ringbuf_push(&dev->rbuf_g, g);
  164. ringbuf_push(&dev->rbuf_a, a);
  165. std_g = ringbuf_stdev(&dev->rbuf_g);
  166. std_a = ringbuf_stdev(&dev->rbuf_a);
  167. schmidt_trig(&dev->trig_g, std_g);
  168. schmidt_trig(&dev->trig_a, std_a);
  169. ret = monotonic_queue_push(&dev->mq_min_cy, cy);
  170. if (ret != 0)
  171. return ret;
  172. if (monotonic_queue_get_len(&dev->mq_min_cy) > dev->cy_window)
  173. ret = monotonic_queue_pop(&dev->mq_min_cy);
  174. if (ret != 0)
  175. return ret;
  176. ret = monotonic_queue_push(&dev->mq_max_cy, cy);
  177. if (ret != 0)
  178. return ret;
  179. if (monotonic_queue_get_len(&dev->mq_max_cy) > dev->cy_window)
  180. ret = monotonic_queue_pop(&dev->mq_max_cy);
  181. if (ret != 0)
  182. return ret;
  183. ret = monotonic_queue_get_min(&dev->mq_min_cy, &min);
  184. if (ret != 0)
  185. return ret;
  186. ret = monotonic_queue_get_min(&dev->mq_max_cy, &max);
  187. if (ret != 0)
  188. return ret;
  189. if (max - min > dev->cy_crit)
  190. dev->cy_suppress = dev->cy_suppress_time;
  191. if (schmidt_get(&dev->trig_g) == 0 ||
  192. schmidt_get(&dev->trig_a) == 0 ||
  193. dev->cy_suppress > 0)
  194. {
  195. schmidt_trig(&dev->trig_gz, -INFINITY);
  196. *result = RESULT_INACTIVE;
  197. goto out;
  198. }
  199. if (schmidt_trig(&dev->trig_gz, gz) == 1 &&
  200. schmidt_get(&dev->trig_gz) == 0 &&
  201. dev->cy_suppress == 0)
  202. {
  203. dev->remain_time = dev->wait_time;
  204. *result = RESULT_TRIGGERED;
  205. goto out;
  206. }
  207. if (dev->remain_time)
  208. dev->remain_time--;
  209. *result = (dev->remain_time > 0 ? RESULT_NONE : RESULT_INACTIVE);
  210. out:
  211. if (dev->cy_suppress > 0)
  212. dev->cy_suppress--;
  213. if (data_recorder == NULL)
  214. return 0;
  215. /* column names:
  216. "gx,gy,gz,ax,ay,az,cy,cy_fixed,"
  217. "g,a,g_std,a_std,g_up,a_up,is_out" */
  218. fprintf(data_recorder, "%.7f,%.7f,%.7f,",
  219. packet->gx, packet->gy, packet->gz);
  220. fprintf(data_recorder, "%.7f,%.7f,%.7f,",
  221. packet->ax, packet->ay, packet->az);
  222. fprintf(data_recorder, "%.7f,%.7f,",
  223. dev->last_cy, dev->last_cy_fixed);
  224. fprintf(data_recorder, "%.7f,%.7f,%.7f,%.7f,",
  225. g, a, std_g, std_a);
  226. fprintf(data_recorder, "%d,%d,%d\n",
  227. schmidt_get(&dev->trig_g),
  228. schmidt_get(&dev->trig_a),
  229. *result == RESULT_TRIGGERED);
  230. return 0;
  231. }
  232. void monotonic_queue_init(struct monotonic_queue *mq,
  233. monotonic_queue_cmp cmp)
  234. {
  235. mq->h = mq->h_min = mq->t = mq->len = 0;
  236. mq->cmp = cmp;
  237. }
  238. int monotonic_queue_push(struct monotonic_queue *mq, float value)
  239. {
  240. int t1;
  241. while (mq->h_min != mq->t && mq->cmp(value, mq->buf[mq->h_min]))
  242. mq->h_min = (mq->h_min + 1) % MONOTONIC_QUEUE_CAP;
  243. t1 = (mq->t + 1) % MONOTONIC_QUEUE_CAP;
  244. if (t1 == mq->h)
  245. /* over flow */
  246. return -1;
  247. mq->len++;
  248. mq->buf[mq->t] = value;
  249. mq->t = t1;
  250. return 0;
  251. }
  252. int monotonic_queue_pop(struct monotonic_queue *mq)
  253. {
  254. if (mq->h == mq->t)
  255. /* empty */
  256. return -1;
  257. mq->len--;
  258. if (mq->h == mq->h_min)
  259. mq->h_min = (mq->h_min + 1) % MONOTONIC_QUEUE_CAP;
  260. mq->h = (mq->h + 1) % MONOTONIC_QUEUE_CAP;
  261. return 0;
  262. }
  263. int monotonic_queue_get_min(struct monotonic_queue *mq, float *dest)
  264. {
  265. if (mq->h == mq->t)
  266. /* empty */
  267. return -1;
  268. *dest = mq->buf[mq->h_min];
  269. return 0;
  270. }
  271. int monotonic_queue_get_len(struct monotonic_queue *mq)
  272. {
  273. return mq->len;
  274. }
  275. int ringbuf_init(struct ringbuf *ringbuf, int cap)
  276. {
  277. if (cap > RINGBUF_MAX_SIZE)
  278. return -1;
  279. ringbuf->p = 0;
  280. ringbuf->full = 0;
  281. ringbuf->sum = ringbuf->sum2 = 0.0;
  282. ringbuf->cap = cap;
  283. return 0;
  284. }
  285. void ringbuf_push(struct ringbuf *ringbuf, float value)
  286. {
  287. if (ringbuf->full) {
  288. float old = ringbuf->buf[ringbuf->p];
  289. ringbuf->sum -= old;
  290. ringbuf->sum2 -= old * old;
  291. }
  292. ringbuf->sum += value;
  293. ringbuf->sum2 += value * value;
  294. ringbuf->buf[ringbuf->p++] = value;
  295. if (ringbuf->p == ringbuf->cap) {
  296. ringbuf->p = 0;
  297. ringbuf->full = 1;
  298. }
  299. }
  300. int ringbuf_size(struct ringbuf *ringbuf)
  301. {
  302. return ringbuf->full ? ringbuf->cap : ringbuf->p;
  303. }
  304. float ringbuf_mean(struct ringbuf *ringbuf)
  305. {
  306. int sz = ringbuf_size(ringbuf);
  307. if (sz == 0)
  308. return 0;
  309. return ringbuf->sum / sz;
  310. }
  311. float ringbuf_variance(struct ringbuf *ringbuf)
  312. {
  313. float mean;
  314. int sz = ringbuf_size(ringbuf);
  315. if (sz == 0)
  316. return 0;
  317. mean = ringbuf_mean(ringbuf);
  318. return ringbuf->sum2 / sz - mean * mean;
  319. }
  320. float ringbuf_stdev(struct ringbuf *ringbuf)
  321. {
  322. return sqrt(ringbuf_variance(ringbuf));
  323. }
  324. int schmidt_init(struct schmidt *schmidt, float low, float high)
  325. {
  326. if (low > high)
  327. return -1;
  328. schmidt->high = high;
  329. schmidt->low = low;
  330. schmidt->value = 0;
  331. return 0;
  332. }
  333. int schmidt_trig(struct schmidt *schmidt, float level)
  334. {
  335. if ((schmidt->value == 1 && level <= schmidt->low) ||
  336. (schmidt->value == 0 && level >= schmidt->high)) {
  337. schmidt->value ^= 1;
  338. return 1;
  339. }
  340. return 0;
  341. }
  342. int schmidt_get(struct schmidt *schmidt)
  343. {
  344. return schmidt->value;
  345. }
  346. const int fs = 50;
  347. int main()
  348. {
  349. struct jump_rope_count_config cfg;
  350. struct jump_rope_count_device dev;
  351. struct sensor_packet packet;
  352. int count = 0, ret;
  353. enum jump_rope_count_result result;
  354. data_recorder = fopen("data_record.csv", "w");
  355. if (data_recorder == NULL)
  356. fprintf(stderr, "can not open data_record.csv\n");
  357. else
  358. fprintf(data_recorder, "%s\n", column_names);
  359. cfg.lg = 0.5;
  360. cfg.hg = 1.5;
  361. cfg.la = 4.0;
  362. cfg.ha = 6.0;
  363. cfg.lgz = -1.5;
  364. cfg.hgz = 2;
  365. cfg.a_g_window = 100;
  366. cfg.cy_window = fs * 0.1;
  367. cfg.cy_crit = 200;
  368. cfg.cy_suppress_time = fs * 0.2;
  369. cfg.wait_time = fs * 1;
  370. if (jump_rope_count_device_init(&dev, &cfg) != 0)
  371. abort();
  372. while (scanf("%f%f%f%f%f%f%f",
  373. &packet.gx, &packet.gy, &packet.gz,
  374. &packet.ax, &packet.ay, &packet.az,
  375. &packet.cy) == 7) {
  376. ret = process_packet(&dev, &packet, &result);
  377. if (ret != 0)
  378. abort();
  379. if (result == RESULT_INACTIVE) {
  380. if (count > 1)
  381. printf("%d\n", count - 1);
  382. count = 0;
  383. } else if (result == RESULT_TRIGGERED)
  384. count += 1;
  385. }
  386. if (count > 1)
  387. printf("%d\n", count - 1);
  388. return 0;
  389. }
  390. /* vim: set ts=8 sw=8 sts=8 noet: */