Browse Source

compare a valley to previous one and ignore suspicious valleys

Xℹ Ruoyao 4 years ago
parent
commit
bb29bb93b3
1 changed files with 33 additions and 7 deletions
  1. 33 7
      all.c

+ 33 - 7
all.c

@@ -85,11 +85,14 @@ extern int monotonic_queue_pop(struct monotonic_queue *mq);
 extern int monotonic_queue_get_min(struct monotonic_queue *mq, float *dest);
 extern int monotonic_queue_get_len(struct monotonic_queue *mq);
 
+typedef int (*jump_rope_count_valley_cmp)(float valley, float prev_valley);
+
 struct jump_rope_count_config
 {
 	float lg, hg, la, ha, lgz, hgz, a_g_window;
 	int cy_window, cy_crit, cy_suppress_time, wait_time, dead_zone_time;
-	int record_valley_time;
+	int record_valley_time, prev_valley_lifetime;
+	jump_rope_count_valley_cmp valley_cmp;
 };
 
 struct jump_rope_count_device
@@ -98,12 +101,13 @@ struct jump_rope_count_device
 	struct ringbuf rbuf_g, rbuf_a;
 	struct monotonic_queue mq_min_cy, mq_max_cy;
 	int cy_window, cy_crit, cy_suppress_time, wait_time, dead_zone_time;
-	int record_valley_time;
+	int record_valley_time, prev_valley_lifetime;
+	jump_rope_count_valley_cmp valley_cmp;
 
 	int cy_suppress, wait_remain_time, dead_zone_remain_time;
-	int recording_valley;
+	int recording_valley, has_prev_valley;
 	float last_cy, last_cy_fixed;
-	float valley;
+	float valley, prev_valley;
 };
 
 struct sensor_packet
@@ -173,9 +177,14 @@ int jump_rope_count_device_init(struct jump_rope_count_device *dev,
 	dev->wait_remain_time = dev->dead_zone_remain_time = 0;
 
 	dev->record_valley_time = cfg->record_valley_time;
+	dev->prev_valley_lifetime = cfg->prev_valley_lifetime;
+	dev->valley_cmp = cfg->valley_cmp;
+
 	if (dev->record_valley_time < 0)
 		dev->record_valley_time = 1;
 
+	dev->has_prev_valley = 0;
+
 	/* an initial value only used for data recording */
 	dev->valley = cfg->lgz;
 
@@ -240,15 +249,25 @@ int process_packet(struct jump_rope_count_device *dev,
 	if (ret != 0)
 		return ret;
 
+	if (dev->has_prev_valley > 0)
+		dev->has_prev_valley--;
+
 	if (dev->recording_valley) {
-		if (--dev->recording_valley == 0)
+		if (--dev->recording_valley == 0 &&
+		    (!dev->has_prev_valley ||
+		     dev->valley_cmp(dev->valley, dev->prev_valley))) {
+			dev->has_prev_valley = dev->prev_valley_lifetime;
+			dev->prev_valley = dev->valley;
 			*result = RESULT_TRIGGERED;
-		else {
+			goto out;
+		}
+
+		if (dev->recording_valley) {
 			if (dev->valley > gz)
 				dev->valley = gz;
 			*result = RESULT_NONE;
+			goto out;
 		}
-		goto out;
 	}
 
 	ret = monotonic_queue_get_min(&dev->mq_min_cy, &min);
@@ -463,6 +482,11 @@ int schmidt_get(struct schmidt *schmidt)
 
 const int fs = 50;
 
+int valley_cmp_naive(float valley, float prev_valley)
+{
+	return prev_valley + 2.0 > valley;
+}
+
 int main()
 {
 	struct jump_rope_count_config cfg;
@@ -490,6 +514,8 @@ int main()
 	cfg.wait_time = fs * 1;
 	cfg.dead_zone_time = fs * 0.2;
 	cfg.record_valley_time = fs * 0.1;
+	cfg.prev_valley_lifetime = fs * 0.5;
+	cfg.valley_cmp = &valley_cmp_naive;
 
 	if (jump_rope_count_device_init(&dev, &cfg) != 0)
 		abort();