From: Keith Packard Date: Sun, 24 May 2009 04:16:22 +0000 (-0700) Subject: Avoid 16-bit overflow in velocity computation. X-Git-Tag: 0.5~69 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=d6f5a0689023546464a71561f53fa2c943077c88;hp=aa6d87aeb616dd62f0debaded297232022b4f8bd 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 --- 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 */