From cf20e213f39fb24f15e0ac94307c2d138fcadecb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 24 Aug 2017 16:45:54 -0700 Subject: 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 --- src/kernel/ao_flight.c | 2 +- src/kernel/ao_kalman.c | 18 +++++++++--------- src/kernel/ao_sample.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/kernel') 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 -- cgit v1.2.3 From 12ef994a24eb996458092dc35c671d6b824b1576 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 27 Aug 2017 16:57:13 -0700 Subject: altos: Eliminate separate height error filter for accelerometer devices We don't use the error value in flight for those models anyways; it's only useful on baro-only hardware. Signed-off-by: Keith Packard --- src/kernel/ao_kalman.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/kernel') diff --git a/src/kernel/ao_kalman.c b/src/kernel/ao_kalman.c index d401a3c8..87f1bf66 100644 --- a/src/kernel/ao_kalman.c +++ b/src/kernel/ao_kalman.c @@ -96,13 +96,8 @@ ao_kalman_err_height(void) e = -e; if (e > 127) e = 127; -#if HAS_ACCEL - ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2; - ao_error_h_sq_avg += (e * e) >> 2; -#else ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4; ao_error_h_sq_avg += (e * e) >> 4; -#endif if (ao_flight_state >= ao_flight_drogue) return; -- cgit v1.2.3 From ea6fe21d78744d7e6225a56c369d54f7cd956767 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 26 Aug 2017 19:16:47 -0700 Subject: altos: Don't eliminate baro above mach speed, just trust it less Instead of completely eliminating the baro sensor above mach speed, just derate it a bit so that the accel will dominate for speed computation and keep the device from false-triggering across mach transitions. When we completely ignored the baro sensor above mach, and the flight spent considerable time in that speed range, then the estimated height could be far from the real value. When the estimated speed dropped back down and the baro values were brought back into the computation, then the resulting rapid shift in estimated speed could trigger accidental apogee detection. By mixing in a bit of baro data even above mach, we keep the estimated height closer to the baro value and prevent this error, at least in flights measured so far. The flight known to have this problem is: 2015-09-26-serial-2093-flight-0012.eeprom Signed-off-by: Keith Packard --- src/kernel/ao_flight.c | 4 +++- src/kernel/ao_kalman.c | 8 ++++---- src/kernel/ao_sample.h | 7 ++++++- 3 files changed, 13 insertions(+), 6 deletions(-) (limited to 'src/kernel') diff --git a/src/kernel/ao_flight.c b/src/kernel/ao_flight.c index b0d911ed..f06125cd 100644 --- a/src/kernel/ao_flight.c +++ b/src/kernel/ao_flight.c @@ -282,9 +282,11 @@ ao_flight(void) * the measured altitude reasonably closely; otherwise * we're probably transsonic. */ +#define AO_ERROR_BOUND 100 + if (ao_speed < 0 #if !HAS_ACCEL - && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100) + && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND) #endif ) { diff --git a/src/kernel/ao_kalman.c b/src/kernel/ao_kalman.c index 87f1bf66..82315c48 100644 --- a/src/kernel/ao_kalman.c +++ b/src/kernel/ao_kalman.c @@ -103,13 +103,13 @@ ao_kalman_err_height(void) return; height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT; #if HAS_ACCEL - /* speed is stored * 16, but we need to ramp between 200 and 328, so + /* speed is stored * 16, but we need to ramp between 248 and 328, so * we want to multiply by 2. The result is a shift by 3. */ speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1); - if (speed_distrust <= 0) - speed_distrust = 0; - else if (speed_distrust > height_distrust) + if (speed_distrust > AO_MAX_SPEED_DISTRUST) + speed_distrust = AO_MAX_SPEED_DISTRUST; + if (speed_distrust > height_distrust) height_distrust = speed_distrust; #endif if (height_distrust > 0) { diff --git a/src/kernel/ao_sample.h b/src/kernel/ao_sample.h index da40187b..f89d6a4c 100644 --- a/src/kernel/ao_sample.h +++ b/src/kernel/ao_sample.h @@ -95,7 +95,12 @@ typedef int16_t ao_v_t; /* * Above this speed, baro measurements are unreliable */ -#define AO_MAX_BARO_SPEED 200 +#define AO_MAX_BARO_SPEED 248 + +/* The maximum amount (in a range of 0-256) to de-rate the + * baro sensor data based on speed. + */ +#define AO_MAX_SPEED_DISTRUST 160 #define ACCEL_NOSE_UP (ao_accel_2g >> 2) -- cgit v1.2.3 From 4775b1d9b50a8732d66a0ad3b73ff74901a8cb7f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 28 Aug 2017 00:15:43 -0700 Subject: altos: Don't compute filtered average of height error when HAS_ACCEL We only use this for baro-only devices to avoid firing drogue charges at mach transitions; we trust the combination of accel+baro to do the right thing when available. Signed-off-by: Keith Packard --- src/kernel/ao_kalman.c | 6 ++++++ src/kernel/ao_sample.h | 2 ++ 2 files changed, 8 insertions(+) (limited to 'src/kernel') diff --git a/src/kernel/ao_kalman.c b/src/kernel/ao_kalman.c index 82315c48..ac41085d 100644 --- a/src/kernel/ao_kalman.c +++ b/src/kernel/ao_kalman.c @@ -45,7 +45,9 @@ static __pdata ao_k_t ao_avg_height_scaled; __xdata ao_v_t ao_avg_height; __pdata ao_v_t ao_error_h; +#if !HAS_ACCEL __pdata ao_v_t ao_error_h_sq_avg; +#endif #if HAS_ACCEL __pdata ao_v_t ao_error_a; @@ -83,7 +85,9 @@ ao_kalman_predict(void) static void ao_kalman_err_height(void) { +#if !HAS_ACCEL ao_v_t e; +#endif ao_v_t height_distrust; #if HAS_ACCEL ao_v_t speed_distrust; @@ -91,6 +95,7 @@ ao_kalman_err_height(void) ao_error_h = ao_sample_height - (ao_v_t) (ao_k_height >> 16); +#if !HAS_ACCEL e = ao_error_h; if (e < 0) e = -e; @@ -98,6 +103,7 @@ ao_kalman_err_height(void) e = 127; ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4; ao_error_h_sq_avg += (e * e) >> 4; +#endif if (ao_flight_state >= ao_flight_drogue) return; diff --git a/src/kernel/ao_sample.h b/src/kernel/ao_sample.h index f89d6a4c..fbef031d 100644 --- a/src/kernel/ao_sample.h +++ b/src/kernel/ao_sample.h @@ -180,7 +180,9 @@ extern __xdata ao_v_t ao_max_height; /* max of ao_height */ extern __xdata ao_v_t ao_avg_height; /* running average of height */ extern __pdata ao_v_t ao_error_h; +#if !HAS_ACCEL extern __pdata ao_v_t ao_error_h_sq_avg; +#endif #if HAS_ACCEL extern __pdata ao_v_t ao_error_a; -- cgit v1.2.3