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_EASYMINI 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_NONE 127 /* No log at all */
57 extern __code uint8_t ao_log_format;
58 extern __code uint8_t ao_log_size;
60 /* Return the flight number from the given log slot, 0 if none */
62 ao_log_flight(uint8_t slot);
64 /* Check if there is valid log data at the specified location */
66 ao_log_check(uint32_t pos);
72 /* Logging thread main routine */
76 /* functions provided in ao_log.c */
78 /* Figure out the current flight number */
80 ao_log_scan(void) __reentrant;
82 /* Return the position of the start of the given log slot */
84 ao_log_pos(uint8_t slot);
86 /* Start logging to eeprom */
94 /* Initialize the logging system */
98 /* Write out the current flight number to the erase log */
100 ao_log_write_erase(uint8_t pos);
102 /* Returns true if there are any logs stored in eeprom */
104 ao_log_present(void);
106 /* Returns true if there is no more storage space available */
115 * The data log is recorded in the eeprom as a sequence
118 * Each packet starts with a 4-byte header that has the
119 * packet type, the packet checksum and the tick count. Then
120 * they all contain 2 16 bit values which hold packet-specific
123 * For each flight, the first packet
124 * is FLIGHT packet, indicating the serial number of the
125 * device and a unique number marking the number of flights
126 * recorded by this device.
128 * During flight, data from the accelerometer and barometer
129 * are recorded in SENSOR packets, using the raw 16-bit values
130 * read from the A/D converter.
132 * Also during flight, but at a lower rate, the deployment
133 * sensors are recorded in DEPLOY packets. The goal here is to
134 * detect failure in the deployment circuits.
136 * STATE packets hold state transitions as the flight computer
137 * transitions through different stages of the flight.
139 #define AO_LOG_FLIGHT 'F'
140 #define AO_LOG_SENSOR 'A'
141 #define AO_LOG_TEMP_VOLT 'T'
142 #define AO_LOG_DEPLOY 'D'
143 #define AO_LOG_STATE 'S'
144 #define AO_LOG_GPS_TIME 'G'
145 #define AO_LOG_GPS_LAT 'N'
146 #define AO_LOG_GPS_LON 'W'
147 #define AO_LOG_GPS_ALT 'H'
148 #define AO_LOG_GPS_SAT 'V'
149 #define AO_LOG_GPS_DATE 'Y'
150 #define AO_LOG_GPS_POS 'P'
152 #define AO_LOG_POS_NONE (~0UL)
154 struct ao_log_record {
156 uint8_t csum; /* 1 */
157 uint16_t tick; /* 2 */
160 int16_t ground_accel; /* 4 */
161 uint16_t flight; /* 6 */
164 int16_t accel; /* 4 */
165 int16_t pres; /* 6 */
185 int32_t gps_latitude;
186 int32_t gps_longitude;
188 uint16_t altitude_low;
189 int16_t altitude_high;
211 uint8_t csum; /* 1 */
212 uint16_t tick; /* 2 */
216 uint16_t flight; /* 4 */
217 int16_t ground_accel; /* 6 */
218 uint32_t ground_pres; /* 8 */
219 int16_t ground_accel_along; /* 12 */
220 int16_t ground_accel_across; /* 14 */
221 int16_t ground_accel_through; /* 16 */
222 int16_t pad_18; /* 18 */
223 int32_t ground_roll; /* 20 */
224 int32_t ground_pitch; /* 24 */
225 int32_t ground_yaw; /* 28 */
228 uint16_t flight; /* 4 */
229 int16_t ground_accel; /* 6 */
230 uint32_t ground_pres; /* 8 */
231 int16_t ground_accel_along; /* 12 */
232 int16_t ground_accel_across; /* 14 */
233 int16_t ground_accel_through; /* 16 */
234 int16_t ground_roll; /* 18 */
235 int16_t ground_pitch; /* 20 */
236 int16_t ground_yaw; /* 22 */
237 } flight_old; /* 24 */
245 uint32_t pres; /* 4 */
246 uint32_t temp; /* 8 */
247 int16_t accel_x; /* 12 */
248 int16_t accel_y; /* 14 */
249 int16_t accel_z; /* 16 */
250 int16_t gyro_x; /* 18 */
251 int16_t gyro_y; /* 20 */
252 int16_t gyro_z; /* 22 */
253 int16_t mag_x; /* 24 */
254 int16_t mag_y; /* 26 */
255 int16_t mag_z; /* 28 */
256 int16_t accel; /* 30 */
258 /* AO_LOG_TEMP_VOLT */
260 int16_t v_batt; /* 4 */
261 int16_t v_pbatt; /* 6 */
262 int16_t n_sense; /* 8 */
263 int16_t sense[10]; /* 10 */
264 uint16_t pyro; /* 30 */
266 /* AO_LOG_GPS_TIME */
268 int32_t latitude; /* 4 */
269 int32_t longitude; /* 8 */
270 uint16_t altitude_low; /* 12 */
271 uint8_t hour; /* 14 */
272 uint8_t minute; /* 15 */
273 uint8_t second; /* 16 */
274 uint8_t flags; /* 17 */
275 uint8_t year; /* 18 */
276 uint8_t month; /* 19 */
277 uint8_t day; /* 20 */
278 uint8_t course; /* 21 */
279 uint16_t ground_speed; /* 22 */
280 int16_t climb_rate; /* 24 */
281 uint8_t pdop; /* 26 */
282 uint8_t hdop; /* 27 */
283 uint8_t vdop; /* 28 */
284 uint8_t mode; /* 29 */
285 int16_t altitude_high; /* 30 */
289 uint16_t channels; /* 4 */
298 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
299 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
300 ((l)->u.gps.altitude_high = (a) >> 16), \
301 (l)->u.gps.altitude_low = (a))
303 struct ao_log_metrum {
305 uint8_t csum; /* 1 */
306 uint16_t tick; /* 2 */
310 uint16_t flight; /* 4 */
311 int16_t ground_accel; /* 6 */
312 uint32_t ground_pres; /* 8 */
313 uint32_t ground_temp; /* 12 */
317 uint16_t state; /* 4 */
318 uint16_t reason; /* 6 */
322 uint32_t pres; /* 4 */
323 uint32_t temp; /* 8 */
324 int16_t accel; /* 12 */
326 /* AO_LOG_TEMP_VOLT */
328 int16_t v_batt; /* 4 */
329 int16_t sense_a; /* 6 */
330 int16_t sense_m; /* 8 */
334 int32_t latitude; /* 4 */
335 int32_t longitude; /* 8 */
336 uint16_t altitude_low; /* 12 */
337 int16_t altitude_high; /* 14 */
339 /* AO_LOG_GPS_TIME */
341 uint8_t hour; /* 4 */
342 uint8_t minute; /* 5 */
343 uint8_t second; /* 6 */
344 uint8_t flags; /* 7 */
345 uint8_t year; /* 8 */
346 uint8_t month; /* 9 */
347 uint8_t day; /* 10 */
348 uint8_t pdop; /* 11 */
350 /* AO_LOG_GPS_SAT (up to three packets) */
352 uint8_t channels; /* 4 */
353 uint8_t more; /* 5 */
359 uint8_t raw[12]; /* 4 */
365 uint8_t csum; /* 1 */
366 uint16_t tick; /* 2 */
370 uint16_t flight; /* 4 */
372 uint32_t ground_pres; /* 8 */
376 uint16_t state; /* 4 */
377 uint16_t reason; /* 6 */
381 uint8_t pres[3]; /* 4 */
382 uint8_t temp[3]; /* 7 */
383 int16_t sense_a; /* 10 */
384 int16_t sense_m; /* 12 */
385 int16_t v_batt; /* 14 */
390 #define ao_log_pack24(dst,value) do { \
391 (dst)[0] = (value); \
392 (dst)[1] = (value) >> 8; \
393 (dst)[2] = (value) >> 16; \
398 uint8_t csum; /* 1 */
399 uint16_t tick; /* 2 */
403 uint16_t flight; /* 4 */
404 int16_t start_altitude; /* 6 */
405 int32_t start_latitude; /* 8 */
406 int32_t start_longitude; /* 12 */
408 /* AO_LOG_GPS_TIME */
410 int32_t latitude; /* 4 */
411 int32_t longitude; /* 8 */
412 uint16_t altitude_low; /* 12 */
413 uint8_t hour; /* 14 */
414 uint8_t minute; /* 15 */
415 uint8_t second; /* 16 */
416 uint8_t flags; /* 17 */
417 uint8_t year; /* 18 */
418 uint8_t month; /* 19 */
419 uint8_t day; /* 20 */
420 uint8_t course; /* 21 */
421 uint16_t ground_speed; /* 22 */
422 int16_t climb_rate; /* 24 */
423 uint8_t pdop; /* 26 */
424 uint8_t hdop; /* 27 */
425 uint8_t vdop; /* 28 */
426 uint8_t mode; /* 29 */
427 int16_t altitude_high; /* 30 */
431 uint16_t channels; /* 4 */
440 /* Write a record to the eeprom log */
442 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
445 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
448 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
451 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
454 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
460 ao_gps_report_metrum_init(void);
462 #endif /* _AO_LOG_H_ */