altos: Add bit-bang i2c driver
[fw/altos] / src / kernel / ao_microflight.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 #include <ao_log_micro.h>
24
25 uint32_t        pa;
26 uint32_t        pa_ground;
27 uint32_t        pa_min;
28
29 static void
30 ao_microsample(void)
31 {
32         ao_pa_get();
33         ao_microkalman_predict();
34         ao_microkalman_correct();
35 }
36
37 #define NUM_PA_HIST     (GROUND_AVG)
38
39 #define SKIP_PA_HIST(i,j)       (((i) + (j)) & (NUM_PA_HIST - 1))
40
41 static uint32_t pa_hist[NUM_PA_HIST];
42
43 void
44 ao_microflight(void)
45 {
46         int16_t         sample_count;
47         int16_t         log_count;
48         uint16_t        time;
49         uint32_t        pa_interval_min, pa_interval_max;
50         int32_t         pa_diff;
51         uint8_t         h;
52         uint8_t         i;
53         uint8_t         accel_lock = 0;
54         uint32_t        pa_sum = 0;
55
56         /* Wait for motion, averaging values to get ground pressure */
57
58         time = ao_time();
59         ao_pa_get();
60         ao_microkalman_init();
61         pa_ground = pa;
62         sample_count = 0;
63         h = 0;
64         for (;;) {
65                 time += SAMPLE_SLEEP;
66 #if BOOST_DETECT
67                 if ((sample_count & 0x1f) == 0)
68                         ao_led_on(AO_LED_REPORT);
69 #endif
70                 ao_delay_until(time);
71                 ao_microsample();
72 #if BOOST_DETECT
73                 if ((sample_count & 0x1f) == 0)
74                         ao_led_off(AO_LED_REPORT);
75 #endif
76                 pa_hist[h] = pa;
77                 h = SKIP_PA_HIST(h,1);
78                 pa_diff = pa_ground - ao_pa;
79
80 #if BOOST_DETECT
81                 /* Check for a significant pressure change */
82                 if (pa_diff > BOOST_DETECT)
83                         break;
84 #endif
85
86                 if (sample_count < GROUND_AVG * 2) {
87                         if (sample_count < GROUND_AVG)
88                                 pa_sum += pa;
89                         ++sample_count;
90                 } else {
91                         pa_ground = pa_sum >> GROUND_AVG_SHIFT;
92                         pa_sum = 0;
93                         sample_count = 0;
94 #if !BOOST_DETECT
95                         break;
96 #endif
97                 }
98         }
99
100         /* Go back and find the last sample close to the ground */
101         pa_min = pa_ground - LAND_DETECT;
102         for (i = SKIP_PA_HIST(h,-2); i != SKIP_PA_HIST(h,2); i = SKIP_PA_HIST(i,-2)) {
103                 if (pa_hist[i] >= pa_min)
104                         break;
105         }
106
107         /* Log the remaining samples so we get a complete history since leaving the ground */
108 #if LOG_INTERVAL < NUM_PA_HIST
109         for (; i != h; i = SKIP_PA_HIST(i,2))
110 #endif
111         {
112                 pa = pa_hist[i];
113                 ao_log_micro_data();
114         }
115
116         /* Now sit around until the pressure is stable again and record the max */
117
118         sample_count = 0;
119         log_count = 0;
120         pa_min = ao_pa;
121         pa_interval_min = ao_pa;
122         pa_interval_max = ao_pa;
123         for (;;) {
124                 time += SAMPLE_SLEEP;
125                 ao_delay_until(time);
126                 if ((sample_count & 3) == 0)
127                         ao_led_on(AO_LED_REPORT);
128                 ao_microsample();
129                 if ((sample_count & 3) == 0)
130                         ao_led_off(AO_LED_REPORT);
131                 if (log_count == LOG_INTERVAL - 1) {
132                         ao_log_micro_data();
133                         log_count = 0;
134                 } else
135                         log_count++;
136
137                 /* If accelerating upwards, don't look for min pressure */
138                 if (ao_pa_accel < ACCEL_LOCK_PA)
139                         accel_lock = ACCEL_LOCK_TIME;
140                 else if (accel_lock)
141                         --accel_lock;
142                 else if (ao_pa < pa_min)
143                         pa_min = ao_pa;
144
145                 if (sample_count == (GROUND_AVG - 1)) {
146                         pa_diff = pa_interval_max - pa_interval_min;
147
148                         /* Check to see if the pressure is now stable */
149                         if (pa_diff < LAND_DETECT)
150                                 break;
151                         sample_count = 0;
152                         pa_interval_min = ao_pa;
153                         pa_interval_max = ao_pa;
154                 } else {
155                         if (ao_pa < pa_interval_min)
156                                 pa_interval_min = ao_pa;
157                         if (ao_pa > pa_interval_max)
158                                 pa_interval_max = ao_pa;
159                         ++sample_count;
160                 }
161         }
162 }