altos: Log baro readings for MicroPeak
[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
23 static struct ao_ms5607_sample  sample;
24 static struct ao_ms5607_value   value;
25
26 uint32_t        pa;
27 uint32_t        pa_avg;
28 uint32_t        pa_ground;
29 uint32_t        pa_min;
30 alt_t           ground_alt, max_alt;
31 alt_t           ao_max_height;
32
33 static uint32_t pa_sum;
34
35 static void
36 ao_pa_get(void)
37 {
38         ao_ms5607_sample(&sample);
39         ao_ms5607_convert(&sample, &value);
40         pa = value.pres;
41 }
42
43 static void
44 ao_compute_height(void)
45 {
46         ground_alt = ao_pa_to_altitude(pa_ground);
47         max_alt = ao_pa_to_altitude(pa_min);
48         ao_max_height = max_alt - ground_alt;
49 }
50
51 int
52 main(void)
53 {
54         int16_t         sample_count;
55         uint16_t        time;
56         uint32_t        pa_interval_min, pa_interval_max;
57         int32_t         pa_diff;
58
59         ao_led_init(LEDS_AVAILABLE);
60         ao_timer_init();
61
62         /* Init external hardware */
63         ao_spi_init();
64         ao_ms5607_init();
65         ao_ms5607_setup();
66
67         /* Give the person a second to get their finger out of the way */
68         ao_delay(AO_MS_TO_TICKS(1000));
69
70         ao_log_micro_restore();
71         ao_compute_height();
72         ao_report_altitude();
73         ao_log_micro_dump();
74         
75         ao_delay(BOOST_DELAY);
76         /* Wait for motion, averaging values to get ground pressure */
77         time = ao_time();
78         ao_pa_get();
79         pa_avg = pa_ground = pa << FILTER_SHIFT;
80         sample_count = 0;
81         for (;;) {
82                 time += SAMPLE_SLEEP;
83                 if (sample_count == 0)
84                         ao_led_on(AO_LED_REPORT);
85                 ao_delay_until(time);
86                 ao_pa_get();
87                 if (sample_count == 0)
88                         ao_led_off(AO_LED_REPORT);
89                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
90                 pa_diff = pa_ground - pa_avg;
91
92                 /* Check for a significant pressure change */
93                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
94                         break;
95
96                 if (sample_count < GROUND_AVG * 2) {
97                         if (sample_count < GROUND_AVG)
98                                 pa_sum += pa;
99                         ++sample_count;
100                 } else {
101                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
102                         pa_sum = 0;
103                         sample_count = 0;
104                 }
105         }
106
107         pa_ground >>= FILTER_SHIFT;
108
109         /* Now sit around until the pressure is stable again and record the max */
110
111         sample_count = 0;
112         pa_min = pa_avg;
113         pa_interval_min = pa_avg;
114         pa_interval_max = pa_avg;
115         for (;;) {
116                 time += SAMPLE_SLEEP;
117                 ao_delay_until(time);
118                 if ((sample_count & 3) == 0)
119                         ao_led_on(AO_LED_REPORT);
120                 ao_pa_get();
121                 if ((sample_count & 3) == 0)
122                         ao_led_off(AO_LED_REPORT);
123                 if (sample_count & 1)
124                         ao_log_micro_data();
125                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
126                 if (pa_avg < pa_min)
127                         pa_min = pa_avg;
128
129                 if (sample_count == (GROUND_AVG - 1)) {
130                         pa_diff = pa_interval_max - pa_interval_min;
131
132                         /* Check to see if the pressure is now stable */
133                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
134                                 break;
135                         sample_count = 0;
136                         pa_interval_min = pa_avg;
137                         pa_interval_max = pa_avg;
138                 } else {
139                         if (pa_avg < pa_interval_min)
140                                 pa_interval_min = pa_avg;
141                         if (pa_avg > pa_interval_max)
142                                 pa_interval_max = pa_avg;
143                         ++sample_count;
144                 }
145         }
146         pa_min >>= FILTER_SHIFT;
147         ao_log_micro_save();
148         ao_compute_height();
149         ao_report_altitude();
150         for (;;) {
151                 cli();
152                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
153                 sleep_mode();
154         }
155 }