2 * Copyright © 2024 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 uint8_t ao_log_data_pos;
27 /* a hack to make sure that ao_log_timers fill the eeprom block in even units */
28 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_timer))] ;
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 AO_TICK_TYPE next_sensor;
46 while (!ao_log_running)
47 ao_sleep(&ao_log_running);
50 ao_log_data.type = AO_LOG_FLIGHT;
51 ao_log_data.tick = (uint16_t) ao_sample_tick;
53 ao_log_data.u.flight.ground_accel = ao_ground_accel;
56 ao_log_data.u.flight.ground_accel_along = ao_ground_accel_along;
57 ao_log_data.u.flight.ground_accel_across = ao_ground_accel_across;
58 ao_log_data.u.flight.ground_accel_through = ao_ground_accel_through;
59 ao_log_data.u.flight.ground_roll = ao_ground_roll;
60 ao_log_data.u.flight.ground_pitch = ao_ground_pitch;
61 ao_log_data.u.flight.ground_yaw = ao_ground_yaw;
63 ao_log_data.u.flight.flight = ao_flight_number;
64 ao_log_write(&ao_log_data);
67 /* Write the whole contents of the ring to the log
70 ao_log_data_pos = ao_data_ring_next(ao_data_head);
71 next_sensor = ao_data_ring[ao_log_data_pos].tick;
72 ao_log_state = ao_flight_startup;
74 /* Write samples to EEPROM */
75 while (ao_log_data_pos != ao_data_head) {
76 AO_TICK_TYPE tick = ao_data_ring[ao_log_data_pos].tick;
77 ao_log_data.tick = (uint16_t) tick;
78 volatile struct ao_data *d = &ao_data_ring[ao_log_data_pos];
79 if ((AO_TICK_SIGNED) (tick - next_sensor) >= 0) {
80 ao_log_data.type = AO_LOG_SENSOR;
83 ao_log_data.u.sensor.accel_along = ao_data_along(d);
84 ao_log_data.u.sensor.accel_across = ao_data_across(d);
85 ao_log_data.u.sensor.accel_through = ao_data_through(d);
86 ao_log_data.u.sensor.gyro_roll = ao_data_roll(d);
87 ao_log_data.u.sensor.gyro_pitch = ao_data_pitch(d);
88 ao_log_data.u.sensor.gyro_yaw = ao_data_yaw(d);
91 ao_log_data.u.sensor.mag_along = ao_data_mag_along(d);
92 ao_log_data.u.sensor.mag_across = ao_data_mag_across(d);
93 ao_log_data.u.sensor.mag_through = ao_data_mag_through(d);
95 ao_log_data.u.sensor.v_batt = d->adc.v_batt;
96 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
97 ao_log_data.u.sensor.sense[i] = d->adc.sense[i];
98 ao_log_data.u.sensor.pyro = ao_pyro_fired;
100 ao_log_write(&ao_log_data);
101 if (ao_log_state <= ao_flight_coast)
102 next_sensor = tick + AO_SENSOR_INTERVAL_ASCENT;
104 next_sensor = tick + AO_SENSOR_INTERVAL_DESCENT;
106 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
109 /* Write state change to EEPROM */
110 if (ao_flight_state != ao_log_state) {
111 ao_log_state = ao_flight_state;
112 ao_log_data.type = AO_LOG_STATE;
113 ao_log_data.tick = (uint16_t) ao_time();
114 ao_log_data.u.state.state = ao_log_state;
115 ao_log_data.u.state.reason = 0;
116 ao_log_write(&ao_log_data);
118 if (ao_log_state == ao_flight_landed)
125 /* Wait for a while */
126 ao_delay(AO_MS_TO_TICKS(100));
128 /* Stop logging when told to */
129 while (!ao_log_running)
130 ao_sleep(&ao_log_running);
133 #endif /* HAS_FLIGHT */