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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <ao_flight.h>
25 static __data uint8_t ao_log_data_pos;
27 /* a hack to make sure that ao_log_metrums fill the eeprom block in even units */
28 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_metrum))] ;
30 #ifndef AO_SENSOR_INTERVAL_ASCENT
31 #define AO_SENSOR_INTERVAL_ASCENT 1
32 #define AO_SENSOR_INTERVAL_DESCENT 10
33 #define AO_OTHER_INTERVAL 32
39 __pdata uint16_t next_sensor, next_other;
45 while (!ao_log_running)
46 ao_sleep(&ao_log_running);
49 log.type = AO_LOG_FLIGHT;
50 log.tick = ao_sample_tick;
52 log.u.flight.ground_accel = ao_ground_accel;
54 log.u.flight.ground_pres = ao_ground_pres;
55 log.u.flight.flight = ao_flight_number;
59 /* Write the whole contents of the ring to the log
62 ao_log_data_pos = ao_data_ring_next(ao_data_head);
63 next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
64 ao_log_state = ao_flight_startup;
66 /* Write samples to EEPROM */
67 while (ao_log_data_pos != ao_data_head) {
68 log.tick = ao_data_ring[ao_log_data_pos].tick;
69 if ((int16_t) (log.tick - next_sensor) >= 0) {
70 log.type = AO_LOG_SENSOR;
72 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607_raw.pres;
73 log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607_raw.temp;
76 log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
79 if (ao_log_state <= ao_flight_coast)
80 next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
82 next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
84 if ((int16_t) (log.tick - next_other) >= 0) {
85 log.type = AO_LOG_TEMP_VOLT;
86 log.u.volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
87 log.u.volt.sense_a = ao_data_ring[ao_log_data_pos].adc.sense_a;
88 log.u.volt.sense_m = ao_data_ring[ao_log_data_pos].adc.sense_m;
90 next_other = log.tick + AO_OTHER_INTERVAL;
92 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
95 /* Write state change to EEPROM */
96 if (ao_flight_state != ao_log_state) {
97 ao_log_state = ao_flight_state;
98 log.type = AO_LOG_STATE;
100 log.u.state.state = ao_log_state;
101 log.u.state.reason = 0;
104 if (ao_log_state == ao_flight_landed)
111 /* Wait for a while */
112 ao_delay(AO_MS_TO_TICKS(100));
114 /* Stop logging when told to */
115 while (!ao_log_running)
116 ao_sleep(&ao_log_running);