altos: Add bit-bang i2c driver
[fw/altos] / src / kernel / ao_microkalman.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #ifndef AO_FLIGHT_TEST
20 #include <ao.h>
21 #endif
22 #include <ao_micropeak.h>
23
24 #define FIX_BITS        16
25
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)
32
33 #include <ao_kalman.h>
34
35 #ifndef AO_MK_STEP_96MS
36 #define AO_MK_STEP_96MS 1
37
38 #endif
39
40 #if AO_MK_STEP_100MS
41 #define AO_MK_TIME_STEP 0.1
42
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
46 #endif
47
48 #if AO_MK_STEP_96MS
49 #define AO_MK_TIME_STEP 0.096
50
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
54 #endif
55
56 /* Basic time step (96ms) */
57 #define AO_MK_STEP      to_fix_v(AO_MK_TIME_STEP)
58
59 /* step ** 2 / 2 */
60 #define AO_MK_STEP_2_2  to_fix_v(AO_MK_TIME_STEP * AO_MK_TIME_STEP / 2.0)
61
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 */
65
66 uint32_t        ao_pa;                  /* integer portion */
67 int16_t         ao_pa_speed;            /* integer portion */
68 int16_t         ao_pa_accel;            /* integer portion */
69
70 void
71 ao_microkalman_init(void)
72 {
73         ao_pa = pa;
74         ao_k_pa = pa << 8;
75 }
76
77 void
78 ao_microkalman_predict(void)
79 {
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;
82 }
83
84 void
85 ao_microkalman_correct(void)
86 {
87         int16_t e;      /* Height error in Pa */
88
89         e = pa - from_fix8(ao_k_pa);
90
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);
97 }