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_megas fill the eeprom block in even units */
28 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mega))] ;
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;
46 while (!ao_log_running)
47 ao_sleep(&ao_log_running);
50 log.type = AO_LOG_FLIGHT;
51 log.tick = ao_sample_tick;
53 log.u.flight.ground_accel = ao_ground_accel;
56 log.u.flight.ground_accel_along = ao_ground_accel_along;
57 log.u.flight.ground_accel_across = ao_ground_accel_across;
58 log.u.flight.ground_accel_through = ao_ground_accel_through;
59 log.u.flight.ground_roll = ao_ground_roll;
60 log.u.flight.ground_pitch = ao_ground_pitch;
61 log.u.flight.ground_yaw = ao_ground_yaw;
63 log.u.flight.ground_pres = ao_ground_pres;
64 log.u.flight.flight = ao_flight_number;
68 /* Write the whole contents of the ring to the log
71 ao_log_data_pos = ao_data_ring_next(ao_data_head);
72 next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
73 ao_log_state = ao_flight_startup;
75 /* Write samples to EEPROM */
76 while (ao_log_data_pos != ao_data_head) {
77 log.tick = ao_data_ring[ao_log_data_pos].tick;
78 if ((int16_t) (log.tick - next_sensor) >= 0) {
79 log.type = AO_LOG_SENSOR;
81 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607_raw.pres;
82 log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607_raw.temp;
85 log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu6000.accel_x;
86 log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu6000.accel_y;
87 log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu6000.accel_z;
88 log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu6000.gyro_x;
89 log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu6000.gyro_y;
90 log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu6000.gyro_z;
93 log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].hmc5883.x;
94 log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].hmc5883.z;
95 log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y;
98 log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu9250.accel_x;
99 log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu9250.accel_y;
100 log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu9250.accel_z;
101 log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu9250.gyro_x;
102 log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu9250.gyro_y;
103 log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu9250.gyro_z;
104 log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].mpu9250.mag_x;
105 log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].mpu9250.mag_z;
106 log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].mpu9250.mag_y;
108 log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
110 if (ao_log_state <= ao_flight_coast)
111 next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
113 next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
115 if ((int16_t) (log.tick - next_other) >= 0) {
116 log.type = AO_LOG_TEMP_VOLT;
117 log.u.volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
118 log.u.volt.v_pbatt = ao_data_ring[ao_log_data_pos].adc.v_pbatt;
119 log.u.volt.n_sense = AO_ADC_NUM_SENSE;
120 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
121 log.u.volt.sense[i] = ao_data_ring[ao_log_data_pos].adc.sense[i];
122 log.u.volt.pyro = ao_pyro_fired;
124 next_other = log.tick + AO_OTHER_INTERVAL;
126 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
129 /* Write state change to EEPROM */
130 if (ao_flight_state != ao_log_state) {
131 ao_log_state = ao_flight_state;
132 log.type = AO_LOG_STATE;
133 log.tick = ao_time();
134 log.u.state.state = ao_log_state;
135 log.u.state.reason = 0;
138 if (ao_log_state == ao_flight_landed)
145 /* Wait for a while */
146 ao_delay(AO_MS_TO_TICKS(100));
148 /* Stop logging when told to */
149 while (!ao_log_running)
150 ao_sleep(&ao_log_running);
153 #endif /* HAS_FLIGHT */