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>
28 /* We record flight numbers in the first record of
29 * the log. Tasks may wait for this to be initialized
30 * by sleeping on this variable.
32 extern __xdata uint16_t ao_flight_number;
33 extern __xdata uint8_t ao_log_mutex;
34 extern __pdata uint32_t ao_log_current_pos;
35 extern __pdata uint32_t ao_log_end_pos;
36 extern __pdata uint32_t ao_log_start_pos;
37 extern __xdata uint8_t ao_log_running;
38 extern __pdata enum ao_flight_state ao_log_state;
40 /* required functions from the underlying log system */
42 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
43 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
44 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
45 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
46 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
47 #define AO_LOG_FORMAT_TELEMEGA_OLD 5 /* 32 byte typed telemega records */
48 #define AO_LOG_FORMAT_EASYMINI1 6 /* 16-byte MS5607 baro only, 3.0V supply */
49 #define AO_LOG_FORMAT_TELEMETRUM 7 /* 16-byte typed telemetrum records */
50 #define AO_LOG_FORMAT_TELEMINI2 8 /* 16-byte MS5607 baro only, 3.3V supply, cc1111 SoC */
51 #define AO_LOG_FORMAT_TELEGPS 9 /* 32 byte telegps records */
52 #define AO_LOG_FORMAT_TELEMEGA 10 /* 32 byte typed telemega records with 32 bit gyro cal */
53 #define AO_LOG_FORMAT_DETHERM 11 /* 16-byte MS5607 baro only, no ADC */
54 #define AO_LOG_FORMAT_TELEMINI3 12 /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
55 #define AO_LOG_FORMAT_TELEFIRETWO 13 /* 32-byte test stand data */
56 #define AO_LOG_FORMAT_EASYMINI2 14 /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
57 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
59 extern __code uint8_t ao_log_format;
60 extern __code uint8_t ao_log_size;
62 /* Return the flight number from the given log slot, 0 if none */
64 ao_log_flight(uint8_t slot);
66 /* Check if there is valid log data at the specified location */
68 ao_log_check(uint32_t pos);
74 /* Logging thread main routine */
78 /* functions provided in ao_log.c */
80 /* Figure out the current flight number */
82 ao_log_scan(void) __reentrant;
84 /* Return the position of the start of the given log slot */
86 ao_log_pos(uint8_t slot);
88 /* Start logging to eeprom */
96 /* Initialize the logging system */
100 /* Write out the current flight number to the erase log */
102 ao_log_write_erase(uint8_t pos);
104 /* Returns true if there are any logs stored in eeprom */
106 ao_log_present(void);
108 /* Returns true if there is no more storage space available */
117 * The data log is recorded in the eeprom as a sequence
120 * Each packet starts with a 4-byte header that has the
121 * packet type, the packet checksum and the tick count. Then
122 * they all contain 2 16 bit values which hold packet-specific
125 * For each flight, the first packet
126 * is FLIGHT packet, indicating the serial number of the
127 * device and a unique number marking the number of flights
128 * recorded by this device.
130 * During flight, data from the accelerometer and barometer
131 * are recorded in SENSOR packets, using the raw 16-bit values
132 * read from the A/D converter.
134 * Also during flight, but at a lower rate, the deployment
135 * sensors are recorded in DEPLOY packets. The goal here is to
136 * detect failure in the deployment circuits.
138 * STATE packets hold state transitions as the flight computer
139 * transitions through different stages of the flight.
141 #define AO_LOG_FLIGHT 'F'
142 #define AO_LOG_SENSOR 'A'
143 #define AO_LOG_TEMP_VOLT 'T'
144 #define AO_LOG_DEPLOY 'D'
145 #define AO_LOG_STATE 'S'
146 #define AO_LOG_GPS_TIME 'G'
147 #define AO_LOG_GPS_LAT 'N'
148 #define AO_LOG_GPS_LON 'W'
149 #define AO_LOG_GPS_ALT 'H'
150 #define AO_LOG_GPS_SAT 'V'
151 #define AO_LOG_GPS_DATE 'Y'
152 #define AO_LOG_GPS_POS 'P'
154 #define AO_LOG_POS_NONE (~0UL)
156 struct ao_log_record {
158 uint8_t csum; /* 1 */
159 uint16_t tick; /* 2 */
162 int16_t ground_accel; /* 4 */
163 uint16_t flight; /* 6 */
166 int16_t accel; /* 4 */
167 int16_t pres; /* 6 */
187 int32_t gps_latitude;
188 int32_t gps_longitude;
190 uint16_t altitude_low;
191 int16_t altitude_high;
213 uint8_t csum; /* 1 */
214 uint16_t tick; /* 2 */
218 uint16_t flight; /* 4 */
219 int16_t ground_accel; /* 6 */
220 uint32_t ground_pres; /* 8 */
221 int16_t ground_accel_along; /* 12 */
222 int16_t ground_accel_across; /* 14 */
223 int16_t ground_accel_through; /* 16 */
224 int16_t pad_18; /* 18 */
225 int32_t ground_roll; /* 20 */
226 int32_t ground_pitch; /* 24 */
227 int32_t ground_yaw; /* 28 */
230 uint16_t flight; /* 4 */
231 int16_t ground_accel; /* 6 */
232 uint32_t ground_pres; /* 8 */
233 int16_t ground_accel_along; /* 12 */
234 int16_t ground_accel_across; /* 14 */
235 int16_t ground_accel_through; /* 16 */
236 int16_t ground_roll; /* 18 */
237 int16_t ground_pitch; /* 20 */
238 int16_t ground_yaw; /* 22 */
239 } flight_old; /* 24 */
247 uint32_t pres; /* 4 */
248 uint32_t temp; /* 8 */
249 int16_t accel_x; /* 12 */
250 int16_t accel_y; /* 14 */
251 int16_t accel_z; /* 16 */
252 int16_t gyro_x; /* 18 */
253 int16_t gyro_y; /* 20 */
254 int16_t gyro_z; /* 22 */
255 int16_t mag_x; /* 24 */
256 int16_t mag_z; /* 26 */
257 int16_t mag_y; /* 28 */
258 int16_t accel; /* 30 */
260 /* AO_LOG_TEMP_VOLT */
262 int16_t v_batt; /* 4 */
263 int16_t v_pbatt; /* 6 */
264 int16_t n_sense; /* 8 */
265 int16_t sense[10]; /* 10 */
266 uint16_t pyro; /* 30 */
268 /* AO_LOG_GPS_TIME */
270 int32_t latitude; /* 4 */
271 int32_t longitude; /* 8 */
272 uint16_t altitude_low; /* 12 */
273 uint8_t hour; /* 14 */
274 uint8_t minute; /* 15 */
275 uint8_t second; /* 16 */
276 uint8_t flags; /* 17 */
277 uint8_t year; /* 18 */
278 uint8_t month; /* 19 */
279 uint8_t day; /* 20 */
280 uint8_t course; /* 21 */
281 uint16_t ground_speed; /* 22 */
282 int16_t climb_rate; /* 24 */
283 uint8_t pdop; /* 26 */
284 uint8_t hdop; /* 27 */
285 uint8_t vdop; /* 28 */
286 uint8_t mode; /* 29 */
287 int16_t altitude_high; /* 30 */
291 uint16_t channels; /* 4 */
300 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
301 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
302 ((l)->u.gps.altitude_high = (a) >> 16), \
303 (l)->u.gps.altitude_low = (a))
305 struct ao_log_firetwo {
307 uint8_t csum; /* 1 */
308 uint16_t tick; /* 2 */
312 uint16_t flight; /* 4 */
316 uint16_t state; /* 4 */
317 uint16_t reason; /* 6 */
321 uint16_t pressure; /* 4 */
322 uint16_t thrust; /* 6 */
323 uint16_t thermistor[4]; /* 8 */
325 uint8_t align[28]; /* 4 */
329 struct ao_log_metrum {
331 uint8_t csum; /* 1 */
332 uint16_t tick; /* 2 */
336 uint16_t flight; /* 4 */
337 int16_t ground_accel; /* 6 */
338 uint32_t ground_pres; /* 8 */
339 uint32_t ground_temp; /* 12 */
343 uint16_t state; /* 4 */
344 uint16_t reason; /* 6 */
348 uint32_t pres; /* 4 */
349 uint32_t temp; /* 8 */
350 int16_t accel; /* 12 */
352 /* AO_LOG_TEMP_VOLT */
354 int16_t v_batt; /* 4 */
355 int16_t sense_a; /* 6 */
356 int16_t sense_m; /* 8 */
360 int32_t latitude; /* 4 */
361 int32_t longitude; /* 8 */
362 uint16_t altitude_low; /* 12 */
363 int16_t altitude_high; /* 14 */
365 /* AO_LOG_GPS_TIME */
367 uint8_t hour; /* 4 */
368 uint8_t minute; /* 5 */
369 uint8_t second; /* 6 */
370 uint8_t flags; /* 7 */
371 uint8_t year; /* 8 */
372 uint8_t month; /* 9 */
373 uint8_t day; /* 10 */
374 uint8_t pdop; /* 11 */
376 /* AO_LOG_GPS_SAT (up to three packets) */
378 uint8_t channels; /* 4 */
379 uint8_t more; /* 5 */
385 uint8_t raw[12]; /* 4 */
391 uint8_t csum; /* 1 */
392 uint16_t tick; /* 2 */
396 uint16_t flight; /* 4 */
398 uint32_t ground_pres; /* 8 */
402 uint16_t state; /* 4 */
403 uint16_t reason; /* 6 */
407 uint8_t pres[3]; /* 4 */
408 uint8_t temp[3]; /* 7 */
409 int16_t sense_a; /* 10 */
410 int16_t sense_m; /* 12 */
411 int16_t v_batt; /* 14 */
416 #define ao_log_pack24(dst,value) do { \
417 (dst)[0] = (value); \
418 (dst)[1] = (value) >> 8; \
419 (dst)[2] = (value) >> 16; \
424 uint8_t csum; /* 1 */
425 uint16_t tick; /* 2 */
429 uint16_t flight; /* 4 */
430 int16_t start_altitude; /* 6 */
431 int32_t start_latitude; /* 8 */
432 int32_t start_longitude; /* 12 */
434 /* AO_LOG_GPS_TIME */
436 int32_t latitude; /* 4 */
437 int32_t longitude; /* 8 */
438 uint16_t altitude_low; /* 12 */
439 uint8_t hour; /* 14 */
440 uint8_t minute; /* 15 */
441 uint8_t second; /* 16 */
442 uint8_t flags; /* 17 */
443 uint8_t year; /* 18 */
444 uint8_t month; /* 19 */
445 uint8_t day; /* 20 */
446 uint8_t course; /* 21 */
447 uint16_t ground_speed; /* 22 */
448 int16_t climb_rate; /* 24 */
449 uint8_t pdop; /* 26 */
450 uint8_t hdop; /* 27 */
451 uint8_t vdop; /* 28 */
452 uint8_t mode; /* 29 */
453 int16_t altitude_high; /* 30 */
457 uint16_t channels; /* 4 */
466 /* Write a record to the eeprom log */
468 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
471 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
474 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
477 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
480 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
486 ao_gps_report_metrum_init(void);
488 #endif /* _AO_LOG_H_ */