altos: Log in-flight data 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_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            120     /* 10m at sea level, 12m at 2000m */
52
53 /* Wait after power on before doing anything to give the user time to assemble the rocket */
54 #define BOOST_DELAY             AO_SEC_TO_TICKS(30)
55
56 /* Pressure change (in Pa) to detect landing */
57 #define LAND_DETECT             12      /* 1m at sea level, 1.2m at 2000m */
58
59 static void
60 ao_compute_height(void)
61 {
62         ground_alt = ao_pa_to_altitude(pa_ground);
63         max_alt = ao_pa_to_altitude(pa_min);
64         ao_max_height = max_alt - ground_alt;
65 }
66
67 #if !HAS_EEPROM
68
69 #define PA_GROUND_OFFSET        0
70 #define PA_MIN_OFFSET           4
71 #define N_SAMPLES_OFFSET        8
72 #define STARTING_LOG_OFFSET     10
73 #define MAX_LOG_OFFSET          512
74
75 static uint16_t ao_log_offset = STARTING_LOG_OFFSET;
76
77 void
78 ao_save_flight(void)
79 {
80         uint16_t        n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t);
81         ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
82         ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
83         ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
84 }
85
86 void
87 ao_restore_flight(void)
88 {
89         ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
90         ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
91 }
92
93 void
94 ao_log_micro(void)
95 {
96         uint16_t        low_bits = pa;
97
98         if (ao_log_offset < MAX_LOG_OFFSET) {
99                 ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits));
100                 ao_log_offset += sizeof (low_bits);
101         }
102 }
103 #endif
104
105 int
106 main(void)
107 {
108         int16_t         sample_count;
109         uint16_t        time;
110 #if HAS_EEPROM
111         uint8_t dump_eeprom = 0;
112 #endif
113         ao_led_init(LEDS_AVAILABLE);
114         ao_timer_init();
115
116 #if HAS_EEPROM
117
118         /* Set MOSI and CLK as inputs with pull-ups */
119         DDRB &= ~(1 << 0) | (1 << 2);
120         PORTB |= (1 << 0) | (1 << 2);
121
122         /* Check to see if either MOSI or CLK are pulled low by the
123          * user shorting them to ground. If so, dump the eeprom out
124          * via the LED. Wait for the shorting wire to go away before
125          * continuing.
126          */
127         while ((PINB & ((1 << 0) | (1 << 2))) != ((1 << 0) | (1 << 2)))
128                 dump_eeprom = 1;
129         PORTB &= ~(1 << 0) | (1 << 2);
130
131         ao_i2c_init();
132 #endif
133         ao_restore_flight();
134         ao_compute_height();
135         /* Give the person a second to get their finger out of the way */
136         ao_delay(AO_MS_TO_TICKS(1000));
137         ao_report_altitude();
138         
139         ao_spi_init();
140         ao_ms5607_init();
141         ao_ms5607_setup();
142
143 #if HAS_EEPROM
144         ao_storage_init();
145
146         /* Check to see if there's a flight recorded in memory */
147         if (dump_eeprom && ao_log_micro_scan())
148                 ao_log_micro_dump();
149 #endif  
150
151         ao_delay(BOOST_DELAY);
152         /* Wait for motion, averaging values to get ground pressure */
153         time = ao_time();
154         ao_pa_get();
155         pa_avg = pa_ground = pa << FILTER_SHIFT;
156         sample_count = 0;
157         for (;;) {
158                 time += SAMPLE_SLEEP;
159                 if (sample_count == 0)
160                         ao_led_on(AO_LED_REPORT);
161                 ao_delay_until(time);
162                 ao_pa_get();
163                 if (sample_count == 0)
164                         ao_led_off(AO_LED_REPORT);
165                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
166                 pa_diff = pa_ground - pa_avg;
167
168                 /* Check for a significant pressure change */
169                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
170                         break;
171
172                 if (sample_count < GROUND_AVG * 2) {
173                         if (sample_count < GROUND_AVG)
174                                 pa_sum += pa;
175                         ++sample_count;
176                 } else {
177                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
178                         pa_sum = 0;
179                         sample_count = 0;
180                 }
181         }
182
183         pa_ground >>= FILTER_SHIFT;
184
185 #if HAS_EEPROM
186         ao_log_micro_data(AO_LOG_MICRO_GROUND | pa_ground);
187 #endif
188
189         /* Now sit around until the pressure is stable again and record the max */
190
191         sample_count = 0;
192         pa_min = pa_avg;
193         pa_interval_min = pa_avg;
194         pa_interval_max = pa_avg;
195         for (;;) {
196                 time += SAMPLE_SLEEP;
197                 ao_delay_until(time);
198                 if ((sample_count & 3) == 0)
199                         ao_led_on(AO_LED_REPORT);
200                 ao_pa_get();
201                 if ((sample_count & 3) == 0)
202                         ao_led_off(AO_LED_REPORT);
203 #if HAS_EEPROM
204                 ao_log_micro_data(AO_LOG_MICRO_DATA | pa);
205 #else
206                 if (sample_count & 1)
207                         ao_log_micro();
208 #endif
209                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
210                 if (pa_avg < pa_min)
211                         pa_min = pa_avg;
212
213                 if (sample_count == (GROUND_AVG - 1)) {
214                         pa_diff = pa_interval_max - pa_interval_min;
215
216                         /* Check to see if the pressure is now stable */
217                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
218                                 break;
219                         sample_count = 0;
220                         pa_interval_min = pa_avg;
221                         pa_interval_max = pa_avg;
222                 } else {
223                         if (pa_avg < pa_interval_min)
224                                 pa_interval_min = pa_avg;
225                         if (pa_avg > pa_interval_max)
226                                 pa_interval_max = pa_avg;
227                         ++sample_count;
228                 }
229         }
230         pa_min >>= FILTER_SHIFT;
231 #if HAS_EEPROM
232         ao_log_micro_data(AO_LOG_MICRO_DONE | pa_min);
233 #endif
234         ao_save_flight();
235         ao_compute_height();
236         ao_report_altitude();
237         for (;;) {
238                 cli();
239                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
240                 sleep_mode();
241         }
242 }