Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / micropeak / ao_micropeak.c
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_micropeak.h>
20 #include <ao_ms5607.h>
21 #include <ao_log_micro.h>
22 #include <ao_async.h>
23
24 static struct ao_ms5607_sample  sample;
25 static struct ao_ms5607_value   value;
26
27 uint32_t        pa;
28 uint32_t        pa_avg;
29 uint32_t        pa_ground;
30 uint32_t        pa_min;
31 alt_t           ground_alt, max_alt;
32 alt_t           ao_max_height;
33
34 static uint32_t pa_sum;
35
36 static void
37 ao_pa_get(void)
38 {
39         ao_ms5607_sample(&sample);
40         ao_ms5607_convert(&sample, &value);
41         pa = value.pres;
42 }
43
44 static void
45 ao_compute_height(void)
46 {
47         ground_alt = ao_pa_to_altitude(pa_ground);
48         max_alt = ao_pa_to_altitude(pa_min);
49         ao_max_height = max_alt - ground_alt;
50 }
51
52 static void
53 ao_pips(void)
54 {
55         uint8_t i;
56         for (i = 0; i < 10; i++) {
57                 ao_led_toggle(AO_LED_REPORT);
58                 ao_delay(AO_MS_TO_TICKS(80));
59         }
60         ao_delay(AO_MS_TO_TICKS(200));
61 }
62
63 #define NUM_PA_HIST     16
64
65 #define SKIP_PA_HIST(i,j)       (((i) + (j)) & (NUM_PA_HIST - 1))
66
67 static uint32_t pa_hist[NUM_PA_HIST];
68
69 int
70 main(void)
71 {
72         int16_t         sample_count;
73         uint16_t        time;
74         uint32_t        pa_interval_min, pa_interval_max;
75         int32_t         pa_diff;
76         uint8_t         h, i;
77
78         ao_led_init(LEDS_AVAILABLE);
79         ao_timer_init();
80
81         /* Init external hardware */
82         ao_spi_init();
83         ao_ms5607_init();
84         ao_ms5607_setup();
85
86         /* Give the person a second to get their finger out of the way */
87         ao_delay(AO_MS_TO_TICKS(1000));
88
89         ao_log_micro_restore();
90         ao_compute_height();
91         ao_report_altitude();
92         ao_pips();
93         ao_log_micro_dump();
94         
95         ao_delay(BOOST_DELAY);
96         /* Wait for motion, averaging values to get ground pressure */
97         time = ao_time();
98         ao_pa_get();
99         pa_avg = pa_ground = pa << FILTER_SHIFT;
100         sample_count = 0;
101         h = 0;
102         for (;;) {
103                 time += SAMPLE_SLEEP;
104                 if (sample_count == 0)
105                         ao_led_on(AO_LED_REPORT);
106                 ao_delay_until(time);
107                 ao_pa_get();
108                 if (sample_count == 0)
109                         ao_led_off(AO_LED_REPORT);
110                 pa_hist[h] = pa;
111                 h = SKIP_PA_HIST(h,1);
112                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
113                 pa_diff = pa_ground - pa_avg;
114
115                 /* Check for a significant pressure change */
116                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
117                         break;
118
119                 if (sample_count < GROUND_AVG * 2) {
120                         if (sample_count < GROUND_AVG)
121                                 pa_sum += pa;
122                         ++sample_count;
123                 } else {
124                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
125                         pa_sum = 0;
126                         sample_count = 0;
127                 }
128         }
129
130         pa_ground >>= FILTER_SHIFT;
131
132         /* Go back and find the first sample a decent interval above the ground */
133         pa_min = pa_ground - LAND_DETECT;
134         for (i = SKIP_PA_HIST(h,2); i != h; i = SKIP_PA_HIST(i,2)) {
135                 if (pa_hist[i] < pa_min)
136                         break;
137         }
138
139         /* Log the remaining samples so we get a complete history since leaving the ground */
140         for (; i != h; i = SKIP_PA_HIST(i,2)) {
141                 pa = pa_hist[i];
142                 ao_log_micro_data();
143         }
144
145         /* Now sit around until the pressure is stable again and record the max */
146
147         sample_count = 0;
148         pa_min = pa_avg;
149         pa_interval_min = pa_avg;
150         pa_interval_max = pa_avg;
151         for (;;) {
152                 time += SAMPLE_SLEEP;
153                 ao_delay_until(time);
154                 if ((sample_count & 3) == 0)
155                         ao_led_on(AO_LED_REPORT);
156                 ao_pa_get();
157                 if ((sample_count & 3) == 0)
158                         ao_led_off(AO_LED_REPORT);
159                 if (sample_count & 1)
160                         ao_log_micro_data();
161                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
162                 if (pa_avg < pa_min)
163                         pa_min = pa_avg;
164
165                 if (sample_count == (GROUND_AVG - 1)) {
166                         pa_diff = pa_interval_max - pa_interval_min;
167
168                         /* Check to see if the pressure is now stable */
169                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
170                                 break;
171                         sample_count = 0;
172                         pa_interval_min = pa_avg;
173                         pa_interval_max = pa_avg;
174                 } else {
175                         if (pa_avg < pa_interval_min)
176                                 pa_interval_min = pa_avg;
177                         if (pa_avg > pa_interval_max)
178                                 pa_interval_max = pa_avg;
179                         ++sample_count;
180                 }
181         }
182         pa_min >>= FILTER_SHIFT;
183         ao_log_micro_save();
184         ao_compute_height();
185         ao_report_altitude();
186         for (;;) {
187                 cli();
188                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
189                 sleep_mode();
190         }
191 }