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 int16_t ao_flight_number;
33 extern uint8_t ao_log_mutex;
34 extern uint32_t ao_log_current_pos;
35 extern uint32_t ao_log_end_pos;
36 extern uint32_t ao_log_start_pos;
37 extern uint8_t ao_log_running;
38 extern 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_TELEMEGA_3 15 /* 32 byte typed telemega records with 32 bit gyro cal and mpu9250 */
58 #define AO_LOG_FORMAT_EASYMEGA_2 16 /* 32 byte typed telemega records with 32 bit gyro cal, mpu9250 rotated 90° and adxl375 */
59 #define AO_LOG_FORMAT_TELESTATIC 17 /* 32 byte typed telestatic records */
60 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
62 /* Return the flight number from the given log slot, 0 if none, -slot on failure */
65 ao_log_flight(uint8_t slot);
67 /* Checksum the loaded log record */
69 ao_log_check_data(void);
71 /* Check to see if the loaded log record is empty */
73 ao_log_check_clear(void);
75 /* Check if there is valid log data at the specified location */
76 #define AO_LOG_VALID 1
77 #define AO_LOG_EMPTY 0
78 #define AO_LOG_INVALID -1
81 ao_log_check(uint32_t pos);
87 /* Logging thread main routine */
91 /* functions provided in ao_log.c */
93 /* Figure out the current flight number */
97 /* Return the position of the start of the given log slot */
99 ao_log_pos(uint8_t slot);
101 /* Start logging to eeprom */
109 /* Initialize the logging system */
113 /* Write out the current flight number to the erase log */
115 ao_log_write_erase(uint8_t pos);
117 /* Returns true if there are any logs stored in eeprom */
119 ao_log_present(void);
121 /* Returns true if there is no more storage space available */
130 * The data log is recorded in the eeprom as a sequence
133 * Each packet starts with a 4-byte header that has the
134 * packet type, the packet checksum and the tick count. Then
135 * they all contain 2 16 bit values which hold packet-specific
138 * For each flight, the first packet
139 * is FLIGHT packet, indicating the serial number of the
140 * device and a unique number marking the number of flights
141 * recorded by this device.
143 * During flight, data from the accelerometer and barometer
144 * are recorded in SENSOR packets, using the raw 16-bit values
145 * read from the A/D converter.
147 * Also during flight, but at a lower rate, the deployment
148 * sensors are recorded in DEPLOY packets. The goal here is to
149 * detect failure in the deployment circuits.
151 * STATE packets hold state transitions as the flight computer
152 * transitions through different stages of the flight.
154 #define AO_LOG_FLIGHT 'F'
155 #define AO_LOG_SENSOR 'A'
156 #define AO_LOG_TEMP_VOLT 'T'
157 #define AO_LOG_DEPLOY 'D'
158 #define AO_LOG_STATE 'S'
159 #define AO_LOG_GPS_TIME 'G'
160 #define AO_LOG_GPS_LAT 'N'
161 #define AO_LOG_GPS_LON 'W'
162 #define AO_LOG_GPS_ALT 'H'
163 #define AO_LOG_GPS_SAT 'V'
164 #define AO_LOG_GPS_DATE 'Y'
165 #define AO_LOG_GPS_POS 'P'
167 #define AO_LOG_POS_NONE (~0UL)
169 struct ao_log_record {
171 uint8_t csum; /* 1 */
172 uint16_t tick; /* 2 */
175 int16_t ground_accel; /* 4 */
176 uint16_t flight; /* 6 */
179 int16_t accel; /* 4 */
180 int16_t pres; /* 6 */
200 int32_t gps_latitude;
201 int32_t gps_longitude;
203 uint16_t altitude_low;
204 int16_t altitude_high;
226 uint8_t csum; /* 1 */
227 uint16_t tick; /* 2 */
231 uint16_t flight; /* 4 */
232 int16_t ground_accel; /* 6 */
233 uint32_t ground_pres; /* 8 */
234 int16_t ground_accel_along; /* 12 */
235 int16_t ground_accel_across; /* 14 */
236 int16_t ground_accel_through; /* 16 */
237 int16_t pad_18; /* 18 */
238 int32_t ground_roll; /* 20 */
239 int32_t ground_pitch; /* 24 */
240 int32_t ground_yaw; /* 28 */
243 uint16_t flight; /* 4 */
244 int16_t ground_accel; /* 6 */
245 uint32_t ground_pres; /* 8 */
246 int16_t ground_accel_along; /* 12 */
247 int16_t ground_accel_across; /* 14 */
248 int16_t ground_accel_through; /* 16 */
249 int16_t ground_roll; /* 18 */
250 int16_t ground_pitch; /* 20 */
251 int16_t ground_yaw; /* 22 */
252 } flight_old; /* 24 */
260 uint32_t pres; /* 4 */
261 uint32_t temp; /* 8 */
262 int16_t accel_x; /* 12 */
263 int16_t accel_y; /* 14 */
264 int16_t accel_z; /* 16 */
265 int16_t gyro_x; /* 18 */
266 int16_t gyro_y; /* 20 */
267 int16_t gyro_z; /* 22 */
268 int16_t mag_x; /* 24 */
269 int16_t mag_z; /* 26 */
270 int16_t mag_y; /* 28 */
271 int16_t accel; /* 30 */
273 /* AO_LOG_TEMP_VOLT */
275 int16_t v_batt; /* 4 */
276 int16_t v_pbatt; /* 6 */
277 int16_t n_sense; /* 8 */
278 int16_t sense[10]; /* 10 */
279 uint16_t pyro; /* 30 */
281 /* AO_LOG_GPS_TIME */
283 int32_t latitude; /* 4 */
284 int32_t longitude; /* 8 */
285 uint16_t altitude_low; /* 12 */
286 uint8_t hour; /* 14 */
287 uint8_t minute; /* 15 */
288 uint8_t second; /* 16 */
289 uint8_t flags; /* 17 */
290 uint8_t year; /* 18 */
291 uint8_t month; /* 19 */
292 uint8_t day; /* 20 */
293 uint8_t course; /* 21 */
294 uint16_t ground_speed; /* 22 */
295 int16_t climb_rate; /* 24 */
296 uint8_t pdop; /* 26 */
297 uint8_t hdop; /* 27 */
298 uint8_t vdop; /* 28 */
299 uint8_t mode; /* 29 */
300 int16_t altitude_high; /* 30 */
304 uint16_t channels; /* 4 */
313 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
314 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
315 ((l)->u.gps.altitude_high = (a) >> 16), \
316 (l)->u.gps.altitude_low = (a))
318 struct ao_log_firetwo {
320 uint8_t csum; /* 1 */
321 uint16_t tick; /* 2 */
325 uint16_t flight; /* 4 */
329 uint16_t state; /* 4 */
330 uint16_t reason; /* 6 */
334 uint16_t pressure; /* 4 */
335 uint16_t thrust; /* 6 */
336 uint16_t thermistor[4]; /* 8 */
338 uint8_t align[28]; /* 4 */
342 struct ao_log_telestatic {
344 uint8_t csum; /* 1 */
345 uint16_t tick; /* 2 */
349 uint16_t flight; /* 4 */
353 uint16_t state; /* 4 */
354 uint16_t reason; /* 6 */
358 uint32_t pressure; /* 4 */
359 uint32_t pressure2; /* 8 */
360 uint32_t thrust; /* 12 */
361 uint32_t mass; /* 16 */
362 uint16_t t_low; /* 20 */
363 uint16_t t_high[4]; /* 22 */
365 uint8_t align[28]; /* 4 */
369 struct ao_log_metrum {
371 uint8_t csum; /* 1 */
372 uint16_t tick; /* 2 */
376 uint16_t flight; /* 4 */
377 int16_t ground_accel; /* 6 */
378 uint32_t ground_pres; /* 8 */
379 uint32_t ground_temp; /* 12 */
383 uint16_t state; /* 4 */
384 uint16_t reason; /* 6 */
388 uint32_t pres; /* 4 */
389 uint32_t temp; /* 8 */
390 int16_t accel; /* 12 */
392 /* AO_LOG_TEMP_VOLT */
394 int16_t v_batt; /* 4 */
395 int16_t sense_a; /* 6 */
396 int16_t sense_m; /* 8 */
400 int32_t latitude; /* 4 */
401 int32_t longitude; /* 8 */
402 uint16_t altitude_low; /* 12 */
403 int16_t altitude_high; /* 14 */
405 /* AO_LOG_GPS_TIME */
407 uint8_t hour; /* 4 */
408 uint8_t minute; /* 5 */
409 uint8_t second; /* 6 */
410 uint8_t flags; /* 7 */
411 uint8_t year; /* 8 */
412 uint8_t month; /* 9 */
413 uint8_t day; /* 10 */
414 uint8_t pdop; /* 11 */
416 /* AO_LOG_GPS_SAT (up to three packets) */
418 uint8_t channels; /* 4 */
419 uint8_t more; /* 5 */
425 uint8_t raw[12]; /* 4 */
431 uint8_t csum; /* 1 */
432 uint16_t tick; /* 2 */
436 uint16_t flight; /* 4 */
438 uint32_t ground_pres; /* 8 */
442 uint16_t state; /* 4 */
443 uint16_t reason; /* 6 */
447 uint8_t pres[3]; /* 4 */
448 uint8_t temp[3]; /* 7 */
449 int16_t sense_a; /* 10 */
450 int16_t sense_m; /* 12 */
451 int16_t v_batt; /* 14 */
456 #define ao_log_pack24(dst,value) do { \
457 (dst)[0] = (value); \
458 (dst)[1] = (value) >> 8; \
459 (dst)[2] = (value) >> 16; \
464 uint8_t csum; /* 1 */
465 uint16_t tick; /* 2 */
469 uint16_t flight; /* 4 */
470 int16_t start_altitude; /* 6 */
471 int32_t start_latitude; /* 8 */
472 int32_t start_longitude; /* 12 */
474 /* AO_LOG_GPS_TIME */
476 int32_t latitude; /* 4 */
477 int32_t longitude; /* 8 */
478 uint16_t altitude_low; /* 12 */
479 uint8_t hour; /* 14 */
480 uint8_t minute; /* 15 */
481 uint8_t second; /* 16 */
482 uint8_t flags; /* 17 */
483 uint8_t year; /* 18 */
484 uint8_t month; /* 19 */
485 uint8_t day; /* 20 */
486 uint8_t course; /* 21 */
487 uint16_t ground_speed; /* 22 */
488 int16_t climb_rate; /* 24 */
489 uint8_t pdop; /* 26 */
490 uint8_t hdop; /* 27 */
491 uint8_t vdop; /* 28 */
492 uint8_t mode; /* 29 */
493 int16_t altitude_high; /* 30 */
497 uint16_t channels; /* 4 */
506 #if AO_LOG_FORMAT == AO_LOG_FOMAT_TELEMEGA_OLD || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_3 || AO_LOG_FORMAT == AO_LOG_FORMAT_EASYMEGA_2
507 typedef struct ao_log_mega ao_log_type;
510 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRUM
511 typedef struct ao_log_metrum ao_log_type;
514 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEFIRETWO
515 typedef struct ao_log_firetwo ao_log_type;
518 #if AO_LOG_FORMAT == AO_LOG_FORMAT_EASYMINI1 || AO_LOG_FORMAT == AO_LOG_FORMAT_EASYMINI2 || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMINI2 || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMINI3
519 typedef struct ao_log_mini ao_log_type;
522 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEGPS
523 typedef struct ao_log_gps ao_log_type;
526 #if AO_LOG_FORMAT == AO_LOG_FORMAT_FULL
527 typedef struct ao_log_record ao_log_type;
530 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TINY
531 #define AO_LOG_UNCOMMON 1
534 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRY
535 #define AO_LOG_UNCOMMON 1
538 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELESCIENCE
539 #define AO_LOG_UNCOMMON 1
542 #ifndef AO_LOG_UNCOMMON
543 extern ao_log_type ao_log_data;
545 #define AO_LOG_SIZE sizeof(ao_log_type)
547 /* Write a record to the eeprom log */
550 ao_log_write(ao_log_type *log);
557 ao_gps_report_metrum_init(void);
559 #endif /* _AO_LOG_H_ */