2 * Copyright © 2012 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; version 2 of the License.
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.
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.
19 #include <ao_micropeak.h>
20 #include <ao_ms5607.h>
21 #include <ao_log_micro.h>
24 static struct ao_ms5607_sample sample;
25 static struct ao_ms5607_value value;
31 alt_t ground_alt, max_alt;
34 static uint32_t pa_sum;
39 ao_ms5607_sample(&sample);
40 ao_ms5607_convert(&sample, &value);
45 ao_compute_height(void)
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;
56 for (i = 0; i < 10; i++) {
57 ao_led_toggle(AO_LED_REPORT);
58 ao_delay(AO_MS_TO_TICKS(80));
60 ao_delay(AO_MS_TO_TICKS(200));
63 #define NUM_PA_HIST 16
65 #define SKIP_PA_HIST(i,j) (((i) + (j)) & (NUM_PA_HIST - 1))
67 static uint32_t pa_hist[NUM_PA_HIST];
74 uint32_t pa_interval_min, pa_interval_max;
78 ao_led_init(LEDS_AVAILABLE);
81 /* Init external hardware */
86 /* Give the person a second to get their finger out of the way */
87 ao_delay(AO_MS_TO_TICKS(1000));
89 ao_log_micro_restore();
95 ao_delay(BOOST_DELAY);
96 /* Wait for motion, averaging values to get ground pressure */
99 pa_avg = pa_ground = pa << FILTER_SHIFT;
103 time += SAMPLE_SLEEP;
104 if (sample_count == 0)
105 ao_led_on(AO_LED_REPORT);
106 ao_delay_until(time);
108 if (sample_count == 0)
109 ao_led_off(AO_LED_REPORT);
111 h = SKIP_PA_HIST(h,1);
112 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
113 pa_diff = pa_ground - pa_avg;
115 /* Check for a significant pressure change */
116 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
119 if (sample_count < GROUND_AVG * 2) {
120 if (sample_count < GROUND_AVG)
124 pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
130 pa_ground >>= FILTER_SHIFT;
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)
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)) {
145 /* Now sit around until the pressure is stable again and record the max */
149 pa_interval_min = pa_avg;
150 pa_interval_max = pa_avg;
152 time += SAMPLE_SLEEP;
153 ao_delay_until(time);
154 if ((sample_count & 3) == 0)
155 ao_led_on(AO_LED_REPORT);
157 if ((sample_count & 3) == 0)
158 ao_led_off(AO_LED_REPORT);
159 if (sample_count & 1)
161 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
165 if (sample_count == (GROUND_AVG - 1)) {
166 pa_diff = pa_interval_max - pa_interval_min;
168 /* Check to see if the pressure is now stable */
169 if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
172 pa_interval_min = pa_avg;
173 pa_interval_max = pa_avg;
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;
182 pa_min >>= FILTER_SHIFT;
185 ao_report_altitude();
188 set_sleep_mode(SLEEP_MODE_PWR_DOWN);