Avoid 16-bit overflow in velocity computation.
authorKeith Packard <keithp@keithp.com>
Sun, 24 May 2009 04:16:22 +0000 (21:16 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 24 May 2009 04:16:22 +0000 (21:16 -0700)
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 <keithp@keithp.com>
ao_flight.c

index 3e747d0668f02399c0d3ec15850d38e36dead71a..51b2cd5eff138d8ff937f2fc3917ca4ca7aa0820 100644 (file)
@@ -170,7 +170,7 @@ ao_flight(void)
                         * so subtract instead of add.
                         */
                        ticks = ao_flight_tick - ao_flight_prev_tick;
                         * 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 */
                        ao_raw_accel_prev = ao_raw_accel;
 
                        /* one is a common interval */