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_MICROPEAK2 18 /* 2-byte baro values with header */
61 #define AO_LOG_FORMAT_TELEMEGA_4 19 /* 32 byte typed telemega records with 32 bit gyro cal and Bmx160 */
62 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
64 /* Return the flight number from the given log slot, 0 if none, -slot on failure */
67 ao_log_flight(uint8_t slot);
69 /* Checksum the loaded log record */
71 ao_log_check_data(void);
73 /* Check to see if the loaded log record is empty */
75 ao_log_check_clear(void);
77 /* Check if there is valid log data at the specified location */
78 #define AO_LOG_VALID 1
79 #define AO_LOG_EMPTY 0
80 #define AO_LOG_INVALID -1
83 ao_log_check(uint32_t pos);
89 /* Logging thread main routine */
93 /* functions provided in ao_log.c */
95 /* Figure out the current flight number */
99 /* Return the position of the start of the given log slot */
101 ao_log_pos(uint8_t slot);
103 /* Start logging to eeprom */
111 /* Initialize the logging system */
115 /* Write out the current flight number to the erase log */
117 ao_log_write_erase(uint8_t pos);
119 /* Returns true if there are any logs stored in eeprom */
121 ao_log_present(void);
123 /* Returns true if there is no more storage space available */
132 * The data log is recorded in the eeprom as a sequence
135 * Each packet starts with a 4-byte header that has the
136 * packet type, the packet checksum and the tick count. Then
137 * they all contain 2 16 bit values which hold packet-specific
140 * For each flight, the first packet
141 * is FLIGHT packet, indicating the serial number of the
142 * device and a unique number marking the number of flights
143 * recorded by this device.
145 * During flight, data from the accelerometer and barometer
146 * are recorded in SENSOR packets, using the raw 16-bit values
147 * read from the A/D converter.
149 * Also during flight, but at a lower rate, the deployment
150 * sensors are recorded in DEPLOY packets. The goal here is to
151 * detect failure in the deployment circuits.
153 * STATE packets hold state transitions as the flight computer
154 * transitions through different stages of the flight.
156 #define AO_LOG_FLIGHT 'F'
157 #define AO_LOG_SENSOR 'A'
158 #define AO_LOG_TEMP_VOLT 'T'
159 #define AO_LOG_DEPLOY 'D'
160 #define AO_LOG_STATE 'S'
161 #define AO_LOG_GPS_TIME 'G'
162 #define AO_LOG_GPS_LAT 'N'
163 #define AO_LOG_GPS_LON 'W'
164 #define AO_LOG_GPS_ALT 'H'
165 #define AO_LOG_GPS_SAT 'V'
166 #define AO_LOG_GPS_DATE 'Y'
167 #define AO_LOG_GPS_POS 'P'
169 #define AO_LOG_POS_NONE (~0UL)
171 struct ao_log_record {
173 uint8_t csum; /* 1 */
174 uint16_t tick; /* 2 */
177 int16_t ground_accel; /* 4 */
178 uint16_t flight; /* 6 */
181 int16_t accel; /* 4 */
182 int16_t pres; /* 6 */
202 int32_t gps_latitude;
203 int32_t gps_longitude;
205 uint16_t altitude_low;
206 int16_t altitude_high;
228 uint8_t csum; /* 1 */
229 uint16_t tick; /* 2 */
233 uint16_t flight; /* 4 */
234 int16_t ground_accel; /* 6 */
235 uint32_t ground_pres; /* 8 */
236 int16_t ground_accel_along; /* 12 */
237 int16_t ground_accel_across; /* 14 */
238 int16_t ground_accel_through; /* 16 */
239 int16_t pad_18; /* 18 */
240 int32_t ground_roll; /* 20 */
241 int32_t ground_pitch; /* 24 */
242 int32_t ground_yaw; /* 28 */
245 uint16_t flight; /* 4 */
246 int16_t ground_accel; /* 6 */
247 uint32_t ground_pres; /* 8 */
248 int16_t ground_accel_along; /* 12 */
249 int16_t ground_accel_across; /* 14 */
250 int16_t ground_accel_through; /* 16 */
251 int16_t ground_roll; /* 18 */
252 int16_t ground_pitch; /* 20 */
253 int16_t ground_yaw; /* 22 */
254 } flight_old; /* 24 */
262 uint32_t pres; /* 4 */
263 uint32_t temp; /* 8 */
264 int16_t accel_x; /* 12 */
265 int16_t accel_y; /* 14 */
266 int16_t accel_z; /* 16 */
267 int16_t gyro_x; /* 18 */
268 int16_t gyro_y; /* 20 */
269 int16_t gyro_z; /* 22 */
270 int16_t mag_x; /* 24 */
271 int16_t mag_z; /* 26 */
272 int16_t mag_y; /* 28 */
273 int16_t accel; /* 30 */
275 /* AO_LOG_TEMP_VOLT */
277 int16_t v_batt; /* 4 */
278 int16_t v_pbatt; /* 6 */
279 int16_t n_sense; /* 8 */
280 int16_t sense[10]; /* 10 */
281 uint16_t pyro; /* 30 */
283 /* AO_LOG_GPS_TIME */
285 int32_t latitude; /* 4 */
286 int32_t longitude; /* 8 */
287 uint16_t altitude_low; /* 12 */
288 uint8_t hour; /* 14 */
289 uint8_t minute; /* 15 */
290 uint8_t second; /* 16 */
291 uint8_t flags; /* 17 */
292 uint8_t year; /* 18 */
293 uint8_t month; /* 19 */
294 uint8_t day; /* 20 */
295 uint8_t course; /* 21 */
296 uint16_t ground_speed; /* 22 */
297 int16_t climb_rate; /* 24 */
298 uint8_t pdop; /* 26 */
299 uint8_t hdop; /* 27 */
300 uint8_t vdop; /* 28 */
301 uint8_t mode; /* 29 */
302 int16_t altitude_high; /* 30 */
306 uint16_t channels; /* 4 */
315 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
316 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
317 ((l)->u.gps.altitude_high = (a) >> 16), \
318 (l)->u.gps.altitude_low = (a))
320 struct ao_log_firetwo {
322 uint8_t csum; /* 1 */
323 uint16_t tick; /* 2 */
327 uint16_t flight; /* 4 */
331 uint16_t state; /* 4 */
332 uint16_t reason; /* 6 */
336 uint16_t pressure; /* 4 */
337 uint16_t thrust; /* 6 */
338 uint16_t thermistor[4]; /* 8 */
340 uint8_t align[28]; /* 4 */
344 struct ao_log_telestatic {
346 uint8_t csum; /* 1 */
347 uint16_t tick; /* 2 */
351 uint16_t flight; /* 4 */
355 uint16_t state; /* 4 */
356 uint16_t reason; /* 6 */
360 uint32_t pressure; /* 4 */
361 uint32_t pressure2; /* 8 */
362 uint32_t thrust; /* 12 */
363 uint32_t mass; /* 16 */
364 uint16_t t_low; /* 20 */
365 uint16_t t_high[4]; /* 22 */
367 uint8_t align[28]; /* 4 */
371 struct ao_log_metrum {
373 uint8_t csum; /* 1 */
374 uint16_t tick; /* 2 */
378 uint16_t flight; /* 4 */
379 int16_t ground_accel; /* 6 */
380 uint32_t ground_pres; /* 8 */
381 uint32_t ground_temp; /* 12 */
385 uint16_t state; /* 4 */
386 uint16_t reason; /* 6 */
390 uint32_t pres; /* 4 */
391 uint32_t temp; /* 8 */
392 int16_t accel; /* 12 */
394 /* AO_LOG_TEMP_VOLT */
396 int16_t v_batt; /* 4 */
397 int16_t sense_a; /* 6 */
398 int16_t sense_m; /* 8 */
402 int32_t latitude; /* 4 */
403 int32_t longitude; /* 8 */
404 uint16_t altitude_low; /* 12 */
405 int16_t altitude_high; /* 14 */
407 /* AO_LOG_GPS_TIME */
409 uint8_t hour; /* 4 */
410 uint8_t minute; /* 5 */
411 uint8_t second; /* 6 */
412 uint8_t flags; /* 7 */
413 uint8_t year; /* 8 */
414 uint8_t month; /* 9 */
415 uint8_t day; /* 10 */
416 uint8_t pdop; /* 11 */
418 /* AO_LOG_GPS_SAT (up to three packets) */
420 uint8_t channels; /* 4 */
421 uint8_t more; /* 5 */
427 uint8_t raw[12]; /* 4 */
433 uint8_t csum; /* 1 */
434 uint16_t tick; /* 2 */
438 uint16_t flight; /* 4 */
440 uint32_t ground_pres; /* 8 */
444 uint16_t state; /* 4 */
445 uint16_t reason; /* 6 */
449 uint8_t pres[3]; /* 4 */
450 uint8_t temp[3]; /* 7 */
451 int16_t sense_a; /* 10 */
452 int16_t sense_m; /* 12 */
453 int16_t v_batt; /* 14 */
458 #define ao_log_pack24(dst,value) do { \
459 (dst)[0] = (value); \
460 (dst)[1] = (value) >> 8; \
461 (dst)[2] = (value) >> 16; \
466 uint8_t csum; /* 1 */
467 uint16_t tick; /* 2 */
471 uint16_t flight; /* 4 */
472 int16_t start_altitude; /* 6 */
473 int32_t start_latitude; /* 8 */
474 int32_t start_longitude; /* 12 */
476 /* AO_LOG_GPS_TIME */
478 int32_t latitude; /* 4 */
479 int32_t longitude; /* 8 */
480 uint16_t altitude_low; /* 12 */
481 uint8_t hour; /* 14 */
482 uint8_t minute; /* 15 */
483 uint8_t second; /* 16 */
484 uint8_t flags; /* 17 */
485 uint8_t year; /* 18 */
486 uint8_t month; /* 19 */
487 uint8_t day; /* 20 */
488 uint8_t course; /* 21 */
489 uint16_t ground_speed; /* 22 */
490 int16_t climb_rate; /* 24 */
491 uint8_t pdop; /* 26 */
492 uint8_t hdop; /* 27 */
493 uint8_t vdop; /* 28 */
494 uint8_t mode; /* 29 */
495 int16_t altitude_high; /* 30 */
499 uint16_t channels; /* 4 */
508 #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 || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_4
509 typedef struct ao_log_mega ao_log_type;
512 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRUM
513 typedef struct ao_log_metrum ao_log_type;
516 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEFIRETWO
517 typedef struct ao_log_firetwo ao_log_type;
520 #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
521 typedef struct ao_log_mini ao_log_type;
524 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEGPS
525 typedef struct ao_log_gps ao_log_type;
528 #if AO_LOG_FORMAT == AO_LOG_FORMAT_FULL
529 typedef struct ao_log_record ao_log_type;
532 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TINY
533 #define AO_LOG_UNCOMMON 1
536 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRY
537 #define AO_LOG_UNCOMMON 1
540 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELESCIENCE
541 #define AO_LOG_UNCOMMON 1
544 #if AO_LOG_FORMAT == AO_LOG_FORMAT_MICROPEAK2
545 #define AO_LOG_UNCOMMON 1
548 #ifndef AO_LOG_UNCOMMON
549 extern ao_log_type ao_log_data;
551 #define AO_LOG_SIZE sizeof(ao_log_type)
553 /* Write a record to the eeprom log */
556 ao_log_write(ao_log_type *log);
563 ao_gps_report_metrum_init(void);
565 #endif /* _AO_LOG_H_ */