X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fkernel%2Fao_kalman.c;h=4f4ffe8f1b6a813b532cd4fe751ed6d684f34b6b;hb=188f9efadd480de872f86a8eb741e8738db84c6b;hp=7b0f8145d807ce74bd5db66c0d23408251a8d745;hpb=013e9ccfbe76dc46e8c69ea314950bed83d9a39f;p=fw%2Faltos diff --git a/src/kernel/ao_kalman.c b/src/kernel/ao_kalman.c index 7b0f8145..4f4ffe8f 100644 --- a/src/kernel/ao_kalman.c +++ b/src/kernel/ao_kalman.c @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -23,9 +24,9 @@ #include "ao_sample.h" #include "ao_kalman.h" -static __pdata ao_k_t ao_k_height; -static __pdata ao_k_t ao_k_speed; -static __pdata ao_k_t ao_k_accel; +static ao_k_t ao_k_height; +static ao_k_t ao_k_speed; +static ao_k_t ao_k_accel; #define AO_K_STEP_100 to_fix_v(0.01) #define AO_K_STEP_2_2_100 to_fix_v(0.00005) @@ -36,32 +37,38 @@ static __pdata ao_k_t ao_k_accel; #define AO_K_STEP_1 to_fix_v(1) #define AO_K_STEP_2_2_1 to_fix_v(0.5) -__pdata ao_v_t ao_height; -__pdata ao_v_t ao_speed; -__pdata ao_v_t ao_accel; -__xdata ao_v_t ao_max_height; -static __pdata ao_k_t ao_avg_height_scaled; -__xdata ao_v_t ao_avg_height; +ao_v_t ao_height; +ao_v_t ao_speed; +ao_v_t ao_accel; +ao_v_t ao_max_height; +static ao_k_t ao_avg_height_scaled; +ao_v_t ao_avg_height; -__pdata ao_v_t ao_error_h; -__pdata ao_v_t ao_error_h_sq_avg; +ao_v_t ao_error_h; +#if !HAS_ACCEL || AO_FLIGHT_TEST +#define AO_ERROR_H_SQ_AVG 1 +#endif + +#if AO_ERROR_H_SQ_AVG +ao_v_t ao_error_h_sq_avg; +#endif #if HAS_ACCEL -__pdata ao_v_t ao_error_a; +ao_v_t ao_error_a; #endif 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; @@ -82,7 +89,9 @@ ao_kalman_predict(void) static void ao_kalman_err_height(void) { +#if AO_ERROR_H_SQ_AVG ao_v_t e; +#endif ao_v_t height_distrust; #if HAS_ACCEL ao_v_t speed_distrust; @@ -90,15 +99,12 @@ ao_kalman_err_height(void) ao_error_h = ao_sample_height - (ao_v_t) (ao_k_height >> 16); +#if AO_ERROR_H_SQ_AVG e = ao_error_h; if (e < 0) 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 @@ -107,13 +113,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) { @@ -140,13 +146,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; @@ -179,7 +185,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), @@ -200,7 +206,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), @@ -249,7 +255,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; @@ -284,9 +290,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