Browse Source

normalize angles on a circle

Xℹ Ruoyao 4 years ago
parent
commit
c0385e611a
2 changed files with 25 additions and 3 deletions
  1. 24 3
      jump_rope_count_device.c
  2. 1 0
      jump_rope_count_device.h

+ 24 - 3
jump_rope_count_device.c

@@ -5,13 +5,19 @@
 
 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
 
-/* Before C99, sqrtf is not avaliable.  */
+/* Before C99, float variants of math functions are not avaliable.  */
 float _sqrtf(float x)
 {
 	return (float)sqrt(x);
 }
 #define sqrtf _sqrtf
 
+float _fmodf(float x, float y)
+{
+	return (float)fmod(x, y);
+}
+#define fmodf _fmodf
+
 /* Before C99, INFINITY is not avaliable.  */
 #if defined __GNUC__ && (__GNUC__ > 3 || \
 			 (__GNUC__ == 3 && __GNUC_MINOR >= 3))
@@ -66,6 +72,7 @@ int jump_rope_count_device_init(struct jump_rope_count_device *dev,
 	dev->wait_time = cfg->wait_time;
 
 	dev->cy_suppress = 0;
+	dev->last_cy = dev->last_cy_fixed = 0;
 
 	return 0;
 }
@@ -75,6 +82,16 @@ static float hypot3f(float x, float y, float z)
 	return sqrtf(x*x + y*y + z*z);
 }
 
+static float angle_change(float old, float new)
+{
+	float ret = fmodf(new - old, 360.0);
+	if (ret < -180.0)
+		ret += 360.0;
+	if (ret > 180.0)
+		ret -= 360.0;
+	return ret;
+}
+
 int process_packet(struct jump_rope_count_device *dev,
 		   struct sensor_packet *packet,
 		   enum jump_rope_count_result *result)
@@ -85,6 +102,10 @@ int process_packet(struct jump_rope_count_device *dev,
 	float g = hypot3f(packet->gx, packet->gy, packet->gz);
 	float a = hypot3f(packet->ax, packet->ay, packet->az);
 	float gz = packet->gz;
+	float cy = dev->last_cy_fixed + angle_change(dev->last_cy,
+						     packet->cy);
+	dev->last_cy_fixed = cy;
+	dev->last_cy = packet->cy;
 
 	ringbuf_push(&dev->rbuf_g, g);
 	ringbuf_push(&dev->rbuf_a, a);
@@ -92,7 +113,7 @@ int process_packet(struct jump_rope_count_device *dev,
 	schmidt_trig(&dev->trig_g, ringbuf_stdev(&dev->rbuf_g));
 	schmidt_trig(&dev->trig_a, ringbuf_stdev(&dev->rbuf_a));
 
-	ret = monotonic_queue_push(&dev->mq_min_cy, packet->cy);
+	ret = monotonic_queue_push(&dev->mq_min_cy, cy);
 	if (ret != 0)
 		return ret;
 
@@ -101,7 +122,7 @@ int process_packet(struct jump_rope_count_device *dev,
 	if (ret != 0)
 		return ret;
 
-	ret = monotonic_queue_push(&dev->mq_max_cy, packet->cy);
+	ret = monotonic_queue_push(&dev->mq_max_cy, cy);
 	if (ret != 0)
 		return ret;
 

+ 1 - 0
jump_rope_count_device.h

@@ -18,6 +18,7 @@ struct jump_rope_count_device
 	struct monotonic_queue mq_min_cy, mq_max_cy;
 	int cy_window, cy_crit, cy_suppress_time, wait_time;
 	int cy_suppress, remain_time;
+	float last_cy, last_cy_fixed;
 };
 
 struct sensor_packet