From d6f5a0689023546464a71561f53fa2c943077c88 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 23 May 2009 21:16:22 -0700 Subject: [PATCH 1/1] Avoid 16-bit overflow in velocity computation. Adding two 16 bit integers together can wrap around to negative numbers, this resulted in velocity values which never decreased, making the switch from coast to apogee state not occur. Signed-off-by: Keith Packard --- ao_flight.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ao_flight.c b/ao_flight.c index 3e747d06..51b2cd5e 100644 --- a/ao_flight.c +++ b/ao_flight.c @@ -170,7 +170,7 @@ ao_flight(void) * so subtract instead of add. */ ticks = ao_flight_tick - ao_flight_prev_tick; - ao_vel_change = (((ao_raw_accel + ao_raw_accel_prev) >> 1) - ao_ground_accel); + ao_vel_change = (((ao_raw_accel >> 1) + (ao_raw_accel_prev >> 1)) - ao_ground_accel); ao_raw_accel_prev = ao_raw_accel; /* one is a common interval */ -- 2.30.2