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_ms5607.h>
20 #include <ao_log_micro.h>
22 static struct ao_ms5607_sample sample;
23 static struct ao_ms5607_value value;
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;
38 ao_ms5607_sample(&sample);
39 ao_ms5607_convert(&sample, &value);
43 #define FILTER_SHIFT 3
44 #define SAMPLE_SLEEP AO_MS_TO_TICKS(96)
46 /* 16 sample, or about two seconds worth */
47 #define GROUND_AVG_SHIFT 4
48 #define GROUND_AVG (1 << GROUND_AVG_SHIFT)
51 ao_compute_height(void)
53 ground_alt = ao_pa_to_altitude(pa_ground);
54 max_alt = ao_pa_to_altitude(pa_min);
55 ao_max_height = max_alt - ground_alt;
62 ao_eeprom_write(0, &pa_ground, sizeof (pa_ground));
63 ao_eeprom_write(sizeof (pa_ground), &pa_min, sizeof (pa_min));
67 ao_restore_flight(void)
69 ao_eeprom_read(0, &pa_ground, sizeof (pa_ground));
70 ao_eeprom_read(sizeof (pa_ground), &pa_min, sizeof (pa_min));
80 uint8_t dump_eeprom = 0;
82 ao_led_init(LEDS_AVAILABLE);
87 /* Set MOSI and CLK as inputs with pull-ups */
88 DDRB &= ~(1 << 0) | (1 << 2);
89 PORTB |= (1 << 0) | (1 << 2);
91 /* Check to see if either MOSI or CLK are pulled low by the
92 * user shorting them to ground. If so, dump the eeprom out
93 * via the LED. Wait for the shorting wire to go away before
96 while ((PINB & ((1 << 0) | (1 << 2))) != ((1 << 0) | (1 << 2)))
98 PORTB &= ~(1 << 0) | (1 << 2);
104 ao_report_altitude();
113 /* Check to see if there's a flight recorded in memory */
114 if (dump_eeprom && ao_log_micro_scan())
118 /* Wait for motion, averaging values to get ground pressure */
121 pa_avg = pa_ground = pa << FILTER_SHIFT;
124 time += SAMPLE_SLEEP;
125 ao_delay_until(time);
126 if (sample_count == 0)
127 ao_led_on(AO_LED_BLUE);
129 if (sample_count == 0)
130 ao_led_off(AO_LED_BLUE);
131 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
132 pa_diff = pa_ground - pa_avg;
136 /* about 2 meters at sea level, more if you're higher */
137 if (pa_diff > (24 << FILTER_SHIFT))
140 if (sample_count < GROUND_AVG * 2) {
141 ao_led_off(AO_LED_BLUE);
142 if (sample_count < GROUND_AVG)
146 pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
152 pa_ground >>= FILTER_SHIFT;
155 ao_log_micro_data(AO_LOG_MICRO_GROUND | pa_ground);
158 /* Now sit around until the pressure is stable again and record the max */
162 pa_interval_min = pa_avg;
163 pa_interval_max = pa_avg;
165 time += SAMPLE_SLEEP;
166 ao_delay_until(time);
167 if ((sample_count & 3) == 0)
168 ao_led_on(AO_LED_BLUE);
170 if ((sample_count & 3) == 0)
171 ao_led_off(AO_LED_BLUE);
173 ao_log_micro_data(AO_LOG_MICRO_DATA | pa);
175 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
179 if (sample_count == (GROUND_AVG - 1)) {
180 pa_diff = pa_interval_max - pa_interval_min;
181 /* About 1m at sea level */
182 if (pa_diff < (12 << FILTER_SHIFT))
185 pa_interval_min = pa_avg;
186 pa_interval_max = pa_avg;
188 if (pa_avg < pa_interval_min)
189 pa_interval_min = pa_avg;
190 if (pa_avg > pa_interval_max)
191 pa_interval_max = pa_avg;
195 pa_min >>= FILTER_SHIFT;
197 ao_log_micro_data(AO_LOG_MICRO_DONE | pa_min);
201 ao_report_altitude();
204 set_sleep_mode(SLEEP_MODE_PWR_DOWN);