From: Keith Packard Date: Thu, 24 Aug 2017 23:45:54 +0000 (-0700) Subject: altos: Perform time comparisons using 16-bit arithmetic to handle wrap X-Git-Tag: 1.8.1~18 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=cf20e213f39fb24f15e0ac94307c2d138fcadecb altos: Perform time comparisons using 16-bit arithmetic to handle wrap Subtracting two 16-bit unsigned values to perform time comparisons yields mystic results unless we carefully cast that to int16_t. Signed-off-by: Keith Packard --- diff --git a/src/kernel/ao_flight.c b/src/kernel/ao_flight.c index 50f2b68f..b0d911ed 100644 --- a/src/kernel/ao_flight.c +++ b/src/kernel/ao_flight.c @@ -269,7 +269,7 @@ ao_flight(void) * number of seconds. */ if (ao_config.apogee_lockout) { - if ((ao_sample_tick - ao_boost_tick) < + if ((int16_t) (ao_sample_tick - ao_boost_tick) < AO_SEC_TO_TICKS(ao_config.apogee_lockout)) break; } diff --git a/src/kernel/ao_kalman.c b/src/kernel/ao_kalman.c index 69a1b3de..d401a3c8 100644 --- a/src/kernel/ao_kalman.c +++ b/src/kernel/ao_kalman.c @@ -55,14 +55,14 @@ static void ao_kalman_predict(void) { #ifdef AO_FLIGHT_TEST - if (ao_sample_tick - ao_sample_prev_tick > 50) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) { ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 + (ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4; ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1; return; } - if (ao_sample_tick - ao_sample_prev_tick > 5) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) { ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 + (ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4; ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10; @@ -141,13 +141,13 @@ ao_kalman_correct_baro(void) { ao_kalman_err_height(); #ifdef AO_FLIGHT_TEST - if (ao_sample_tick - ao_sample_prev_tick > 50) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) { ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h; ao_k_speed += (ao_k_t) AO_BARO_K1_1 * ao_error_h; ao_k_accel += (ao_k_t) AO_BARO_K2_1 * ao_error_h; return; } - if (ao_sample_tick - ao_sample_prev_tick > 5) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) { ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h; ao_k_speed += (ao_k_t) AO_BARO_K1_10 * ao_error_h; ao_k_accel += (ao_k_t) AO_BARO_K2_10 * ao_error_h; @@ -180,7 +180,7 @@ ao_kalman_correct_both(void) ao_kalman_err_accel(); #ifdef AO_FLIGHT_TEST - if (ao_sample_tick - ao_sample_prev_tick > 50) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) { if (ao_flight_debug) { printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n", ao_k_speed / (65536.0 * 16.0), @@ -201,7 +201,7 @@ ao_kalman_correct_both(void) (ao_k_t) AO_BOTH_K21_1 * ao_error_a; return; } - if (ao_sample_tick - ao_sample_prev_tick > 5) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) { if (ao_flight_debug) { printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n", ao_k_speed / (65536.0 * 16.0), @@ -250,7 +250,7 @@ ao_kalman_correct_accel(void) { ao_kalman_err_accel(); - if (ao_sample_tick - ao_sample_prev_tick > 5) { + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) { ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a; ao_k_speed += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a; ao_k_accel += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a; @@ -285,9 +285,9 @@ ao_kalman(void) ao_max_height = ao_height; ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height; #ifdef AO_FLIGHT_TEST - if (ao_sample_tick - ao_sample_prev_tick > 50) + if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) ao_avg_height = (ao_avg_height_scaled + 1) >> 1; - else if (ao_sample_tick - ao_sample_prev_tick > 5) + else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) ao_avg_height = (ao_avg_height_scaled + 7) >> 4; else #endif diff --git a/src/kernel/ao_sample.c b/src/kernel/ao_sample.c index 90ea07ad..f0ab0169 100644 --- a/src/kernel/ao_sample.c +++ b/src/kernel/ao_sample.c @@ -180,7 +180,7 @@ static void ao_sample_rotate(void) { #ifdef AO_FLIGHT_TEST - float dt = (ao_sample_tick - ao_sample_prev_tick) / TIME_DIV; + float dt = (int16_t) (ao_sample_tick - ao_sample_prev_tick) / TIME_DIV; #else static const float dt = 1/TIME_DIV; #endif