doc: Add micropeak manual
[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_ms5607.h>
20 #include <ao_log_micro.h>
21
22 static struct ao_ms5607_sample  sample;
23 static struct ao_ms5607_value   value;
24
25 static uint32_t pa;
26 static uint32_t pa_sum;
27 static uint32_t pa_avg;
28 static int32_t  pa_diff;
29 static uint32_t pa_ground;
30 static uint32_t pa_min;
31 static uint32_t pa_interval_min, pa_interval_max;
32 static alt_t    ground_alt, max_alt;
33 alt_t           ao_max_height;
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 #define FILTER_SHIFT            3
44 #define SAMPLE_SLEEP            AO_MS_TO_TICKS(96)
45
46 /* 16 sample, or about two seconds worth */
47 #define GROUND_AVG_SHIFT        4
48 #define GROUND_AVG              (1 << GROUND_AVG_SHIFT)
49
50 /* Pressure change (in Pa) to detect boost */
51 #define BOOST_DETECT            48      /* 4m at sea level, 4.8m at 2000m */
52
53 /* Pressure change (in Pa) to detect landing */
54 #define LAND_DETECT             12      /* 1m at sea level, 1.2m at 2000m */
55
56 static void
57 ao_compute_height(void)
58 {
59         ground_alt = ao_pa_to_altitude(pa_ground);
60         max_alt = ao_pa_to_altitude(pa_min);
61         ao_max_height = max_alt - ground_alt;
62 }
63
64 #if !HAS_EEPROM
65 void
66 ao_save_flight(void)
67 {
68         ao_eeprom_write(0, &pa_ground, sizeof (pa_ground));
69         ao_eeprom_write(sizeof (pa_ground), &pa_min, sizeof (pa_min));
70 }
71
72 void
73 ao_restore_flight(void)
74 {
75         ao_eeprom_read(0, &pa_ground, sizeof (pa_ground));
76         ao_eeprom_read(sizeof (pa_ground), &pa_min, sizeof (pa_min));
77 }
78 #endif
79
80 int
81 main(void)
82 {
83         int16_t         sample_count;
84         uint16_t        time;
85 #if HAS_EEPROM
86         uint8_t dump_eeprom = 0;
87 #endif
88         ao_led_init(LEDS_AVAILABLE);
89         ao_timer_init();
90
91 #if HAS_EEPROM
92
93         /* Set MOSI and CLK as inputs with pull-ups */
94         DDRB &= ~(1 << 0) | (1 << 2);
95         PORTB |= (1 << 0) | (1 << 2);
96
97         /* Check to see if either MOSI or CLK are pulled low by the
98          * user shorting them to ground. If so, dump the eeprom out
99          * via the LED. Wait for the shorting wire to go away before
100          * continuing.
101          */
102         while ((PINB & ((1 << 0) | (1 << 2))) != ((1 << 0) | (1 << 2)))
103                 dump_eeprom = 1;
104         PORTB &= ~(1 << 0) | (1 << 2);
105
106         ao_i2c_init();
107 #endif
108         ao_restore_flight();
109         ao_compute_height();
110         ao_report_altitude();
111         
112         ao_spi_init();
113         ao_ms5607_init();
114         ao_ms5607_setup();
115
116 #if HAS_EEPROM
117         ao_storage_init();
118
119         /* Check to see if there's a flight recorded in memory */
120         if (dump_eeprom && ao_log_micro_scan())
121                 ao_log_micro_dump();
122 #endif  
123
124         /* Wait for motion, averaging values to get ground pressure */
125         time = ao_time();
126         ao_pa_get();
127         pa_avg = pa_ground = pa << FILTER_SHIFT;
128         sample_count = 0;
129         for (;;) {
130                 time += SAMPLE_SLEEP;
131                 if (sample_count == 0)
132                         ao_led_on(AO_LED_BLUE);
133                 ao_delay_until(time);
134                 ao_pa_get();
135                 if (sample_count == 0)
136                         ao_led_off(AO_LED_BLUE);
137                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
138                 pa_diff = pa_ground - pa_avg;
139                 if (pa_diff < 0)
140                         pa_diff = -pa_diff;
141
142                 /* Check for a significant pressure change */
143                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
144                         break;
145
146                 if (sample_count < GROUND_AVG * 2) {
147                         if (sample_count < GROUND_AVG)
148                                 pa_sum += pa;
149                         ++sample_count;
150                 } else {
151                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
152                         pa_sum = 0;
153                         sample_count = 0;
154                 }
155         }
156
157         pa_ground >>= FILTER_SHIFT;
158
159 #if HAS_EEPROM
160         ao_log_micro_data(AO_LOG_MICRO_GROUND | pa_ground);
161 #endif
162
163         /* Now sit around until the pressure is stable again and record the max */
164
165         sample_count = 0;
166         pa_min = pa_avg;
167         pa_interval_min = pa_avg;
168         pa_interval_max = pa_avg;
169         for (;;) {
170                 time += SAMPLE_SLEEP;
171                 ao_delay_until(time);
172                 if ((sample_count & 3) == 0)
173                         ao_led_on(AO_LED_BLUE);
174                 ao_pa_get();
175                 if ((sample_count & 3) == 0)
176                         ao_led_off(AO_LED_BLUE);
177 #if HAS_EEPROM
178                 ao_log_micro_data(AO_LOG_MICRO_DATA | pa);
179 #endif
180                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
181                 if (pa_avg < pa_min)
182                         pa_min = pa_avg;
183
184                 if (sample_count == (GROUND_AVG - 1)) {
185                         pa_diff = pa_interval_max - pa_interval_min;
186
187                         /* Check to see if the pressure is now stable */
188                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
189                                 break;
190                         sample_count = 0;
191                         pa_interval_min = pa_avg;
192                         pa_interval_max = pa_avg;
193                 } else {
194                         if (pa_avg < pa_interval_min)
195                                 pa_interval_min = pa_avg;
196                         if (pa_avg > pa_interval_max)
197                                 pa_interval_max = pa_avg;
198                         ++sample_count;
199                 }
200         }
201         pa_min >>= FILTER_SHIFT;
202 #if HAS_EEPROM
203         ao_log_micro_data(AO_LOG_MICRO_DONE | pa_min);
204 #endif
205         ao_save_flight();
206         ao_compute_height();
207         ao_report_altitude();
208         for (;;) {
209                 cli();
210                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
211                 sleep_mode();
212         }
213 }