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>
27 /* We record flight numbers in the first record of
28 * the log. Tasks may wait for this to be initialized
29 * by sleeping on this variable.
31 extern __xdata uint16_t ao_flight_number;
32 extern __xdata uint8_t ao_log_mutex;
33 extern __pdata uint32_t ao_log_current_pos;
34 extern __pdata uint32_t ao_log_end_pos;
35 extern __pdata uint32_t ao_log_start_pos;
36 extern __xdata uint8_t ao_log_running;
37 extern __pdata enum ao_flight_state ao_log_state;
39 /* required functions from the underlying log system */
41 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
42 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
43 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
44 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
45 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
46 #define AO_LOG_FORMAT_TELEMEGA_OLD 5 /* 32 byte typed telemega records */
47 #define AO_LOG_FORMAT_EASYMINI 6 /* 16-byte MS5607 baro only, 3.0V supply */
48 #define AO_LOG_FORMAT_TELEMETRUM 7 /* 16-byte typed telemetrum records */
49 #define AO_LOG_FORMAT_TELEMINI 8 /* 16-byte MS5607 baro only, 3.3V supply */
50 #define AO_LOG_FORMAT_TELEGPS 9 /* 32 byte telegps records */
51 #define AO_LOG_FORMAT_TELEMEGA 10 /* 32 byte typed telemega records with 32 bit gyro cal */
52 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
54 extern __code uint8_t ao_log_format;
55 extern __code uint8_t ao_log_size;
57 /* Return the flight number from the given log slot, 0 if none */
59 ao_log_flight(uint8_t slot);
61 /* Check if there is valid log data at the specified location */
63 ao_log_check(uint32_t pos);
69 /* Logging thread main routine */
73 /* functions provided in ao_log.c */
75 /* Figure out the current flight number */
77 ao_log_scan(void) __reentrant;
79 /* Return the position of the start of the given log slot */
81 ao_log_pos(uint8_t slot);
83 /* Start logging to eeprom */
91 /* Initialize the logging system */
95 /* Write out the current flight number to the erase log */
97 ao_log_write_erase(uint8_t pos);
99 /* Returns true if there are any logs stored in eeprom */
101 ao_log_present(void);
103 /* Returns true if there is no more storage space available */
112 * The data log is recorded in the eeprom as a sequence
115 * Each packet starts with a 4-byte header that has the
116 * packet type, the packet checksum and the tick count. Then
117 * they all contain 2 16 bit values which hold packet-specific
120 * For each flight, the first packet
121 * is FLIGHT packet, indicating the serial number of the
122 * device and a unique number marking the number of flights
123 * recorded by this device.
125 * During flight, data from the accelerometer and barometer
126 * are recorded in SENSOR packets, using the raw 16-bit values
127 * read from the A/D converter.
129 * Also during flight, but at a lower rate, the deployment
130 * sensors are recorded in DEPLOY packets. The goal here is to
131 * detect failure in the deployment circuits.
133 * STATE packets hold state transitions as the flight computer
134 * transitions through different stages of the flight.
136 #define AO_LOG_FLIGHT 'F'
137 #define AO_LOG_SENSOR 'A'
138 #define AO_LOG_TEMP_VOLT 'T'
139 #define AO_LOG_DEPLOY 'D'
140 #define AO_LOG_STATE 'S'
141 #define AO_LOG_GPS_TIME 'G'
142 #define AO_LOG_GPS_LAT 'N'
143 #define AO_LOG_GPS_LON 'W'
144 #define AO_LOG_GPS_ALT 'H'
145 #define AO_LOG_GPS_SAT 'V'
146 #define AO_LOG_GPS_DATE 'Y'
147 #define AO_LOG_GPS_POS 'P'
149 #define AO_LOG_POS_NONE (~0UL)
151 struct ao_log_record {
153 uint8_t csum; /* 1 */
154 uint16_t tick; /* 2 */
157 int16_t ground_accel; /* 4 */
158 uint16_t flight; /* 6 */
161 int16_t accel; /* 4 */
162 int16_t pres; /* 6 */
182 int32_t gps_latitude;
183 int32_t gps_longitude;
185 uint16_t altitude_low;
186 int16_t altitude_high;
208 uint8_t csum; /* 1 */
209 uint16_t tick; /* 2 */
213 uint16_t flight; /* 4 */
214 int16_t ground_accel; /* 6 */
215 uint32_t ground_pres; /* 8 */
216 int16_t ground_accel_along; /* 12 */
217 int16_t ground_accel_across; /* 14 */
218 int16_t ground_accel_through; /* 16 */
219 int16_t pad_18; /* 18 */
220 int32_t ground_roll; /* 20 */
221 int32_t ground_pitch; /* 24 */
222 int32_t ground_yaw; /* 28 */
225 uint16_t flight; /* 4 */
226 int16_t ground_accel; /* 6 */
227 uint32_t ground_pres; /* 8 */
228 int16_t ground_accel_along; /* 12 */
229 int16_t ground_accel_across; /* 14 */
230 int16_t ground_accel_through; /* 16 */
231 int16_t ground_roll; /* 18 */
232 int16_t ground_pitch; /* 20 */
233 int16_t ground_yaw; /* 22 */
234 } flight_old; /* 24 */
242 uint32_t pres; /* 4 */
243 uint32_t temp; /* 8 */
244 int16_t accel_x; /* 12 */
245 int16_t accel_y; /* 14 */
246 int16_t accel_z; /* 16 */
247 int16_t gyro_x; /* 18 */
248 int16_t gyro_y; /* 20 */
249 int16_t gyro_z; /* 22 */
250 int16_t mag_x; /* 24 */
251 int16_t mag_y; /* 26 */
252 int16_t mag_z; /* 28 */
253 int16_t accel; /* 30 */
255 /* AO_LOG_TEMP_VOLT */
257 int16_t v_batt; /* 4 */
258 int16_t v_pbatt; /* 6 */
259 int16_t n_sense; /* 8 */
260 int16_t sense[10]; /* 10 */
261 uint16_t pyro; /* 30 */
263 /* AO_LOG_GPS_TIME */
265 int32_t latitude; /* 4 */
266 int32_t longitude; /* 8 */
267 uint16_t altitude_low; /* 12 */
268 uint8_t hour; /* 14 */
269 uint8_t minute; /* 15 */
270 uint8_t second; /* 16 */
271 uint8_t flags; /* 17 */
272 uint8_t year; /* 18 */
273 uint8_t month; /* 19 */
274 uint8_t day; /* 20 */
275 uint8_t course; /* 21 */
276 uint16_t ground_speed; /* 22 */
277 int16_t climb_rate; /* 24 */
278 uint8_t pdop; /* 26 */
279 uint8_t hdop; /* 27 */
280 uint8_t vdop; /* 28 */
281 uint8_t mode; /* 29 */
282 int16_t altitude_high; /* 30 */
286 uint16_t channels; /* 4 */
295 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
296 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
297 ((l)->u.gps.altitude_high = (a) >> 16), \
298 (l)->u.gps.altitude_low = (a))
300 struct ao_log_metrum {
302 uint8_t csum; /* 1 */
303 uint16_t tick; /* 2 */
307 uint16_t flight; /* 4 */
308 int16_t ground_accel; /* 6 */
309 uint32_t ground_pres; /* 8 */
310 uint32_t ground_temp; /* 12 */
314 uint16_t state; /* 4 */
315 uint16_t reason; /* 6 */
319 uint32_t pres; /* 4 */
320 uint32_t temp; /* 8 */
321 int16_t accel; /* 12 */
323 /* AO_LOG_TEMP_VOLT */
325 int16_t v_batt; /* 4 */
326 int16_t sense_a; /* 6 */
327 int16_t sense_m; /* 8 */
331 int32_t latitude; /* 4 */
332 int32_t longitude; /* 8 */
333 uint16_t altitude_low; /* 12 */
334 int16_t altitude_high; /* 14 */
336 /* AO_LOG_GPS_TIME */
338 uint8_t hour; /* 4 */
339 uint8_t minute; /* 5 */
340 uint8_t second; /* 6 */
341 uint8_t flags; /* 7 */
342 uint8_t year; /* 8 */
343 uint8_t month; /* 9 */
344 uint8_t day; /* 10 */
345 uint8_t pdop; /* 11 */
347 /* AO_LOG_GPS_SAT (up to three packets) */
349 uint8_t channels; /* 4 */
350 uint8_t more; /* 5 */
356 uint8_t raw[12]; /* 4 */
362 uint8_t csum; /* 1 */
363 uint16_t tick; /* 2 */
367 uint16_t flight; /* 4 */
369 uint32_t ground_pres; /* 8 */
373 uint16_t state; /* 4 */
374 uint16_t reason; /* 6 */
378 uint8_t pres[3]; /* 4 */
379 uint8_t temp[3]; /* 7 */
380 int16_t sense_a; /* 10 */
381 int16_t sense_m; /* 12 */
382 int16_t v_batt; /* 14 */
387 #define ao_log_pack24(dst,value) do { \
388 (dst)[0] = (value); \
389 (dst)[1] = (value) >> 8; \
390 (dst)[2] = (value) >> 16; \
395 uint8_t csum; /* 1 */
396 uint16_t tick; /* 2 */
400 uint16_t flight; /* 4 */
401 int16_t start_altitude; /* 6 */
402 int32_t start_latitude; /* 8 */
403 int32_t start_longitude; /* 12 */
405 /* AO_LOG_GPS_TIME */
407 int32_t latitude; /* 4 */
408 int32_t longitude; /* 8 */
409 uint16_t altitude_low; /* 12 */
410 uint8_t hour; /* 14 */
411 uint8_t minute; /* 15 */
412 uint8_t second; /* 16 */
413 uint8_t flags; /* 17 */
414 uint8_t year; /* 18 */
415 uint8_t month; /* 19 */
416 uint8_t day; /* 20 */
417 uint8_t course; /* 21 */
418 uint16_t ground_speed; /* 22 */
419 int16_t climb_rate; /* 24 */
420 uint8_t pdop; /* 26 */
421 uint8_t hdop; /* 27 */
422 uint8_t vdop; /* 28 */
423 uint8_t mode; /* 29 */
424 int16_t altitude_high; /* 30 */
428 uint16_t channels; /* 4 */
437 /* Write a record to the eeprom log */
439 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
442 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
445 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
448 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
451 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
457 ao_gps_report_metrum_init(void);
459 #endif /* _AO_LOG_H_ */