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.
21 #include <ao_flight.h>
23 static __xdata uint8_t ao_log_mutex;
24 static __xdata struct ao_log_mega log;
26 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMEGA;
29 ao_log_csum(__xdata uint8_t *b) __reentrant
34 for (i = 0; i < sizeof (struct ao_log_mega); i++)
40 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant
45 log->csum = ao_log_csum((__xdata uint8_t *) log);
46 ao_mutex_get(&ao_log_mutex); {
47 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
51 ao_storage_write(ao_log_current_pos,
53 sizeof (struct ao_log_mega));
54 ao_log_current_pos += sizeof (struct ao_log_mega);
56 } ao_mutex_put(&ao_log_mutex);
61 ao_log_dump_check_data(void)
63 if (ao_log_csum((uint8_t *) &log) != 0)
69 static __data uint8_t ao_log_data_pos;
71 /* a hack to make sure that ao_log_megas fill the eeprom block in even units */
72 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mega))] ;
74 #ifndef AO_SENSOR_INTERVAL_ASCENT
75 #define AO_SENSOR_INTERVAL_ASCENT 1
76 #define AO_SENSOR_INTERVAL_DESCENT 10
77 #define AO_OTHER_INTERVAL 32
83 __pdata uint16_t next_sensor, next_other;
90 while (!ao_log_running)
91 ao_sleep(&ao_log_running);
94 log.type = AO_LOG_FLIGHT;
95 log.tick = ao_sample_tick;
97 log.u.flight.ground_accel = ao_ground_accel;
100 log.u.flight.ground_accel_along = ao_ground_accel_along;
101 log.u.flight.ground_accel_across = ao_ground_accel_across;
102 log.u.flight.ground_accel_through = ao_ground_accel_through;
103 log.u.flight.ground_pitch = ao_ground_pitch;
104 log.u.flight.ground_yaw = ao_ground_yaw;
105 log.u.flight.ground_roll = ao_ground_roll;
107 log.u.flight.ground_pres = ao_ground_pres;
108 log.u.flight.flight = ao_flight_number;
112 /* Write the whole contents of the ring to the log
115 ao_log_data_pos = ao_data_ring_next(ao_data_head);
116 next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
117 ao_log_state = ao_flight_startup;
119 /* Write samples to EEPROM */
120 while (ao_log_data_pos != ao_data_head) {
121 log.tick = ao_data_ring[ao_log_data_pos].tick;
122 if ((int16_t) (log.tick - next_sensor) >= 0) {
123 log.type = AO_LOG_SENSOR;
125 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607_raw.pres;
126 log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607_raw.temp;
129 log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu6000.accel_x;
130 log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu6000.accel_y;
131 log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu6000.accel_z;
132 log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu6000.gyro_x;
133 log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu6000.gyro_y;
134 log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu6000.gyro_z;
137 log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].hmc5883.x;
138 log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y;
139 log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].hmc5883.z;
141 log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
143 if (ao_log_state <= ao_flight_coast)
144 next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
146 next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
148 if ((int16_t) (log.tick - next_other) >= 0) {
149 log.type = AO_LOG_TEMP_VOLT;
150 log.u.volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
151 log.u.volt.v_pbatt = ao_data_ring[ao_log_data_pos].adc.v_pbatt;
152 log.u.volt.n_sense = AO_ADC_NUM_SENSE;
153 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
154 log.u.volt.sense[i] = ao_data_ring[ao_log_data_pos].adc.sense[i];
155 log.u.volt.pyro = ao_pyro_fired;
157 next_other = log.tick + AO_OTHER_INTERVAL;
159 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
162 /* Write state change to EEPROM */
163 if (ao_flight_state != ao_log_state) {
164 ao_log_state = ao_flight_state;
165 log.type = AO_LOG_STATE;
166 log.tick = ao_time();
167 log.u.state.state = ao_log_state;
168 log.u.state.reason = 0;
171 if (ao_log_state == ao_flight_landed)
178 /* Wait for a while */
179 ao_delay(AO_MS_TO_TICKS(100));
181 /* Stop logging when told to */
182 while (!ao_log_running)
183 ao_sleep(&ao_log_running);
189 ao_log_flight(uint8_t slot)
191 if (!ao_storage_read(ao_log_pos(slot),
193 sizeof (struct ao_log_mega)))
196 if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
197 return log.u.flight.flight;