summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2009-05-23 21:16:22 -0700
committerKeith Packard <keithp@keithp.com>2009-05-23 21:16:22 -0700
commitd6f5a0689023546464a71561f53fa2c943077c88 (patch)
tree48fd63d1c4c381b6ec574575eb147473ec2ad424
parentaa6d87aeb616dd62f0debaded297232022b4f8bd (diff)
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 <keithp@keithp.com>
-rw-r--r--ao_flight.c2
1 files changed, 1 insertions, 1 deletions
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 */