Merge remote-tracking branch 'origin/master' into micropeak-logging
[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 int
64 main(void)
65 {
66         int16_t         sample_count;
67         uint16_t        time;
68         uint32_t        pa_interval_min, pa_interval_max;
69         int32_t         pa_diff;
70
71         ao_led_init(LEDS_AVAILABLE);
72         ao_timer_init();
73
74         /* Init external hardware */
75         ao_spi_init();
76         ao_ms5607_init();
77         ao_ms5607_setup();
78
79         /* Give the person a second to get their finger out of the way */
80         ao_delay(AO_MS_TO_TICKS(1000));
81
82         ao_log_micro_restore();
83         ao_compute_height();
84         ao_report_altitude();
85         ao_pips();
86         ao_log_micro_dump();
87         
88         ao_delay(BOOST_DELAY);
89         /* Wait for motion, averaging values to get ground pressure */
90         time = ao_time();
91         ao_pa_get();
92         pa_avg = pa_ground = pa << FILTER_SHIFT;
93         sample_count = 0;
94         for (;;) {
95                 time += SAMPLE_SLEEP;
96                 if (sample_count == 0)
97                         ao_led_on(AO_LED_REPORT);
98                 ao_delay_until(time);
99                 ao_pa_get();
100                 if (sample_count == 0)
101                         ao_led_off(AO_LED_REPORT);
102                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
103                 pa_diff = pa_ground - pa_avg;
104
105                 /* Check for a significant pressure change */
106                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
107                         break;
108
109                 if (sample_count < GROUND_AVG * 2) {
110                         if (sample_count < GROUND_AVG)
111                                 pa_sum += pa;
112                         ++sample_count;
113                 } else {
114                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
115                         pa_sum = 0;
116                         sample_count = 0;
117                 }
118         }
119
120         pa_ground >>= FILTER_SHIFT;
121
122         /* Now sit around until the pressure is stable again and record the max */
123
124         sample_count = 0;
125         pa_min = pa_avg;
126         pa_interval_min = pa_avg;
127         pa_interval_max = pa_avg;
128         for (;;) {
129                 time += SAMPLE_SLEEP;
130                 ao_delay_until(time);
131                 if ((sample_count & 3) == 0)
132                         ao_led_on(AO_LED_REPORT);
133                 ao_pa_get();
134                 if ((sample_count & 3) == 0)
135                         ao_led_off(AO_LED_REPORT);
136                 if (sample_count & 1)
137                         ao_log_micro_data();
138                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
139                 if (pa_avg < pa_min)
140                         pa_min = pa_avg;
141
142                 if (sample_count == (GROUND_AVG - 1)) {
143                         pa_diff = pa_interval_max - pa_interval_min;
144
145                         /* Check to see if the pressure is now stable */
146                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
147                                 break;
148                         sample_count = 0;
149                         pa_interval_min = pa_avg;
150                         pa_interval_max = pa_avg;
151                 } else {
152                         if (pa_avg < pa_interval_min)
153                                 pa_interval_min = pa_avg;
154                         if (pa_avg > pa_interval_max)
155                                 pa_interval_max = pa_avg;
156                         ++sample_count;
157                 }
158         }
159         pa_min >>= FILTER_SHIFT;
160         ao_log_micro_save();
161         ao_compute_height();
162         ao_report_altitude();
163         for (;;) {
164                 cli();
165                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
166                 sleep_mode();
167         }
168 }