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 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_NONE 127 /* No log at all */
53 extern __code uint8_t ao_log_format;
54 extern __code uint8_t ao_log_size;
56 /* Return the flight number from the given log slot, 0 if none */
58 ao_log_flight(uint8_t slot);
60 /* Check if there is valid log data at the specified location */
62 ao_log_check(uint32_t pos);
68 /* Logging thread main routine */
72 /* functions provided in ao_log.c */
74 /* Figure out the current flight number */
76 ao_log_scan(void) __reentrant;
78 /* Return the position of the start of the given log slot */
80 ao_log_pos(uint8_t slot);
82 /* Start logging to eeprom */
90 /* Initialize the logging system */
94 /* Write out the current flight number to the erase log */
96 ao_log_write_erase(uint8_t pos);
98 /* Returns true if there are any logs stored in eeprom */
100 ao_log_present(void);
102 /* Returns true if there is no more storage space available */
111 * The data log is recorded in the eeprom as a sequence
114 * Each packet starts with a 4-byte header that has the
115 * packet type, the packet checksum and the tick count. Then
116 * they all contain 2 16 bit values which hold packet-specific
119 * For each flight, the first packet
120 * is FLIGHT packet, indicating the serial number of the
121 * device and a unique number marking the number of flights
122 * recorded by this device.
124 * During flight, data from the accelerometer and barometer
125 * are recorded in SENSOR packets, using the raw 16-bit values
126 * read from the A/D converter.
128 * Also during flight, but at a lower rate, the deployment
129 * sensors are recorded in DEPLOY packets. The goal here is to
130 * detect failure in the deployment circuits.
132 * STATE packets hold state transitions as the flight computer
133 * transitions through different stages of the flight.
135 #define AO_LOG_FLIGHT 'F'
136 #define AO_LOG_SENSOR 'A'
137 #define AO_LOG_TEMP_VOLT 'T'
138 #define AO_LOG_DEPLOY 'D'
139 #define AO_LOG_STATE 'S'
140 #define AO_LOG_GPS_TIME 'G'
141 #define AO_LOG_GPS_LAT 'N'
142 #define AO_LOG_GPS_LON 'W'
143 #define AO_LOG_GPS_ALT 'H'
144 #define AO_LOG_GPS_SAT 'V'
145 #define AO_LOG_GPS_DATE 'Y'
146 #define AO_LOG_GPS_POS 'P'
148 #define AO_LOG_POS_NONE (~0UL)
150 struct ao_log_record {
152 uint8_t csum; /* 1 */
153 uint16_t tick; /* 2 */
156 int16_t ground_accel; /* 4 */
157 uint16_t flight; /* 6 */
160 int16_t accel; /* 4 */
161 int16_t pres; /* 6 */
181 int32_t gps_latitude;
182 int32_t gps_longitude;
184 uint16_t altitude_low;
185 int16_t altitude_high;
207 uint8_t csum; /* 1 */
208 uint16_t tick; /* 2 */
212 uint16_t flight; /* 4 */
213 int16_t ground_accel; /* 6 */
214 uint32_t ground_pres; /* 8 */
215 int16_t ground_accel_along; /* 12 */
216 int16_t ground_accel_across; /* 14 */
217 int16_t ground_accel_through; /* 16 */
218 int16_t ground_roll; /* 18 */
219 int16_t ground_pitch; /* 20 */
220 int16_t ground_yaw; /* 22 */
229 uint32_t pres; /* 4 */
230 uint32_t temp; /* 8 */
231 int16_t accel_x; /* 12 */
232 int16_t accel_y; /* 14 */
233 int16_t accel_z; /* 16 */
234 int16_t gyro_x; /* 18 */
235 int16_t gyro_y; /* 20 */
236 int16_t gyro_z; /* 22 */
237 int16_t mag_x; /* 24 */
238 int16_t mag_y; /* 26 */
239 int16_t mag_z; /* 28 */
240 int16_t accel; /* 30 */
242 /* AO_LOG_TEMP_VOLT */
244 int16_t v_batt; /* 4 */
245 int16_t v_pbatt; /* 6 */
246 int16_t n_sense; /* 8 */
247 int16_t sense[10]; /* 10 */
248 uint16_t pyro; /* 30 */
250 /* AO_LOG_GPS_TIME */
252 int32_t latitude; /* 4 */
253 int32_t longitude; /* 8 */
254 uint16_t altitude_low; /* 12 */
255 uint8_t hour; /* 14 */
256 uint8_t minute; /* 15 */
257 uint8_t second; /* 16 */
258 uint8_t flags; /* 17 */
259 uint8_t year; /* 18 */
260 uint8_t month; /* 19 */
261 uint8_t day; /* 20 */
262 uint8_t course; /* 21 */
263 uint16_t ground_speed; /* 22 */
264 int16_t climb_rate; /* 24 */
265 uint8_t pdop; /* 26 */
266 uint8_t hdop; /* 27 */
267 uint8_t vdop; /* 28 */
268 uint8_t mode; /* 29 */
269 int16_t altitude_high; /* 30 */
273 uint16_t channels; /* 4 */
282 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
283 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
284 ((l)->u.gps.altitude_high = (a) >> 16), \
285 (l)->u.gps.altitude_low = (a))
287 struct ao_log_metrum {
289 uint8_t csum; /* 1 */
290 uint16_t tick; /* 2 */
294 uint16_t flight; /* 4 */
295 int16_t ground_accel; /* 6 */
296 uint32_t ground_pres; /* 8 */
297 uint32_t ground_temp; /* 12 */
301 uint16_t state; /* 4 */
302 uint16_t reason; /* 6 */
306 uint32_t pres; /* 4 */
307 uint32_t temp; /* 8 */
308 int16_t accel; /* 12 */
310 /* AO_LOG_TEMP_VOLT */
312 int16_t v_batt; /* 4 */
313 int16_t sense_a; /* 6 */
314 int16_t sense_m; /* 8 */
318 int32_t latitude; /* 4 */
319 int32_t longitude; /* 8 */
320 uint16_t altitude_low; /* 12 */
321 int16_t altitude_high; /* 14 */
323 /* AO_LOG_GPS_TIME */
325 uint8_t hour; /* 4 */
326 uint8_t minute; /* 5 */
327 uint8_t second; /* 6 */
328 uint8_t flags; /* 7 */
329 uint8_t year; /* 8 */
330 uint8_t month; /* 9 */
331 uint8_t day; /* 10 */
332 uint8_t pdop; /* 11 */
334 /* AO_LOG_GPS_SAT (up to three packets) */
336 uint8_t channels; /* 4 */
337 uint8_t more; /* 5 */
343 uint8_t raw[12]; /* 4 */
349 uint8_t csum; /* 1 */
350 uint16_t tick; /* 2 */
354 uint16_t flight; /* 4 */
356 uint32_t ground_pres; /* 8 */
360 uint16_t state; /* 4 */
361 uint16_t reason; /* 6 */
365 uint8_t pres[3]; /* 4 */
366 uint8_t temp[3]; /* 7 */
367 int16_t sense_a; /* 10 */
368 int16_t sense_m; /* 12 */
369 int16_t v_batt; /* 14 */
374 #define ao_log_pack24(dst,value) do { \
375 (dst)[0] = (value); \
376 (dst)[1] = (value) >> 8; \
377 (dst)[2] = (value) >> 16; \
382 uint8_t csum; /* 1 */
383 uint16_t tick; /* 2 */
387 uint16_t flight; /* 4 */
388 int16_t start_altitude; /* 6 */
389 int32_t start_latitude; /* 8 */
390 int32_t start_longitude; /* 12 */
392 /* AO_LOG_GPS_TIME */
394 int32_t latitude; /* 4 */
395 int32_t longitude; /* 8 */
396 uint16_t altitude_low; /* 12 */
397 uint8_t hour; /* 14 */
398 uint8_t minute; /* 15 */
399 uint8_t second; /* 16 */
400 uint8_t flags; /* 17 */
401 uint8_t year; /* 18 */
402 uint8_t month; /* 19 */
403 uint8_t day; /* 20 */
404 uint8_t course; /* 21 */
405 uint16_t ground_speed; /* 22 */
406 int16_t climb_rate; /* 24 */
407 uint8_t pdop; /* 26 */
408 uint8_t hdop; /* 27 */
409 uint8_t vdop; /* 28 */
410 uint8_t mode; /* 29 */
411 int16_t altitude_high; /* 30 */
415 uint16_t channels; /* 4 */
424 /* Write a record to the eeprom log */
426 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
429 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
432 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
435 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
438 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
444 ao_gps_report_metrum_init(void);
446 #endif /* _AO_LOG_H_ */