2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #ifndef AO_FLIGHT_TEST
22 #include <ao_micropeak.h>
26 #define to_fix_v(x) ((int16_t) ((x) * 65536.0 + 0.5))
27 #define to_fix_k(x) ((int32_t) ((x) * 65536.0 + 0.5))
28 #define from_fix8(x) ((x) >> 8)
29 #define from_fix(x) ((x) >> 16)
30 #define fix8_to_fix_v(x) ((x) << 8)
31 #define fix16_to_fix8(x) ((x) >> 8)
33 #include <ao_kalman.h>
35 #ifndef AO_MK_STEP_96MS
36 #define AO_MK_STEP_96MS 1
41 #define AO_MK_TIME_STEP 0.1
43 #define AO_K0_10 AO_MK2_BARO_K0_10
44 #define AO_K1_10 AO_MK2_BARO_K1_10
45 #define AO_K2_10 AO_MK2_BARO_K2_10
49 #define AO_MK_TIME_STEP 0.096
51 #define AO_K0_10 AO_MK_BARO_K0_10
52 #define AO_K1_10 AO_MK_BARO_K1_10
53 #define AO_K2_10 AO_MK_BARO_K2_10
56 /* Basic time step (96ms) */
57 #define AO_MK_STEP to_fix_v(AO_MK_TIME_STEP)
60 #define AO_MK_STEP_2_2 to_fix_v(AO_MK_TIME_STEP * AO_MK_TIME_STEP / 2.0)
62 uint32_t ao_k_pa; /* 24.8 fixed point */
63 int32_t ao_k_pa_speed; /* 16.16 fixed point */
64 int32_t ao_k_pa_accel; /* 16.16 fixed point */
66 uint32_t ao_pa; /* integer portion */
67 int16_t ao_pa_speed; /* integer portion */
68 int16_t ao_pa_accel; /* integer portion */
71 ao_microkalman_init(void)
78 ao_microkalman_predict(void)
80 ao_k_pa += fix16_to_fix8((int32_t) ao_pa_speed * AO_MK_STEP + (int32_t) ao_pa_accel * AO_MK_STEP_2_2);
81 ao_k_pa_speed += (int32_t) ao_pa_accel * AO_MK_STEP;
85 ao_microkalman_correct(void)
87 int16_t e; /* Height error in Pa */
89 e = pa - from_fix8(ao_k_pa);
91 ao_k_pa += fix16_to_fix8((int32_t) e * AO_K0_10);
92 ao_k_pa_speed += (int32_t) e * AO_K1_10;
93 ao_k_pa_accel += (int32_t) e * AO_K2_10;
94 ao_pa = from_fix8(ao_k_pa);
95 ao_pa_speed = from_fix(ao_k_pa_speed);
96 ao_pa_accel = from_fix(ao_k_pa_accel);