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;
55 /* Return the flight number from the given log slot, 0 if none */
57 ao_log_flight(uint8_t slot);
63 /* Logging thread main routine */
67 /* functions provided in ao_log.c */
69 /* Figure out the current flight number */
71 ao_log_scan(void) __reentrant;
73 /* Return the position of the start of the given log slot */
75 ao_log_pos(uint8_t slot);
77 /* Start logging to eeprom */
85 /* Initialize the logging system */
89 /* Write out the current flight number to the erase log */
91 ao_log_write_erase(uint8_t pos);
93 /* Returns true if there are any logs stored in eeprom */
97 /* Returns true if there is no more storage space available */
106 * The data log is recorded in the eeprom as a sequence
109 * Each packet starts with a 4-byte header that has the
110 * packet type, the packet checksum and the tick count. Then
111 * they all contain 2 16 bit values which hold packet-specific
114 * For each flight, the first packet
115 * is FLIGHT packet, indicating the serial number of the
116 * device and a unique number marking the number of flights
117 * recorded by this device.
119 * During flight, data from the accelerometer and barometer
120 * are recorded in SENSOR packets, using the raw 16-bit values
121 * read from the A/D converter.
123 * Also during flight, but at a lower rate, the deployment
124 * sensors are recorded in DEPLOY packets. The goal here is to
125 * detect failure in the deployment circuits.
127 * STATE packets hold state transitions as the flight computer
128 * transitions through different stages of the flight.
130 #define AO_LOG_FLIGHT 'F'
131 #define AO_LOG_SENSOR 'A'
132 #define AO_LOG_TEMP_VOLT 'T'
133 #define AO_LOG_DEPLOY 'D'
134 #define AO_LOG_STATE 'S'
135 #define AO_LOG_GPS_TIME 'G'
136 #define AO_LOG_GPS_LAT 'N'
137 #define AO_LOG_GPS_LON 'W'
138 #define AO_LOG_GPS_ALT 'H'
139 #define AO_LOG_GPS_SAT 'V'
140 #define AO_LOG_GPS_DATE 'Y'
141 #define AO_LOG_GPS_POS 'P'
143 #define AO_LOG_POS_NONE (~0UL)
145 struct ao_log_record {
147 uint8_t csum; /* 1 */
148 uint16_t tick; /* 2 */
151 int16_t ground_accel; /* 4 */
152 uint16_t flight; /* 6 */
155 int16_t accel; /* 4 */
156 int16_t pres; /* 6 */
176 int32_t gps_latitude;
177 int32_t gps_longitude;
179 uint16_t altitude_low;
180 int16_t altitude_high;
202 uint8_t csum; /* 1 */
203 uint16_t tick; /* 2 */
207 uint16_t flight; /* 4 */
208 int16_t ground_accel; /* 6 */
209 uint32_t ground_pres; /* 8 */
210 int16_t ground_accel_along; /* 12 */
211 int16_t ground_accel_across; /* 14 */
212 int16_t ground_accel_through; /* 16 */
213 int16_t ground_roll; /* 18 */
214 int16_t ground_pitch; /* 20 */
215 int16_t ground_yaw; /* 22 */
224 uint32_t pres; /* 4 */
225 uint32_t temp; /* 8 */
226 int16_t accel_x; /* 12 */
227 int16_t accel_y; /* 14 */
228 int16_t accel_z; /* 16 */
229 int16_t gyro_x; /* 18 */
230 int16_t gyro_y; /* 20 */
231 int16_t gyro_z; /* 22 */
232 int16_t mag_x; /* 24 */
233 int16_t mag_y; /* 26 */
234 int16_t mag_z; /* 28 */
235 int16_t accel; /* 30 */
237 /* AO_LOG_TEMP_VOLT */
239 int16_t v_batt; /* 4 */
240 int16_t v_pbatt; /* 6 */
241 int16_t n_sense; /* 8 */
242 int16_t sense[10]; /* 10 */
243 uint16_t pyro; /* 30 */
245 /* AO_LOG_GPS_TIME */
247 int32_t latitude; /* 4 */
248 int32_t longitude; /* 8 */
249 uint16_t altitude_low; /* 12 */
250 uint8_t hour; /* 14 */
251 uint8_t minute; /* 15 */
252 uint8_t second; /* 16 */
253 uint8_t flags; /* 17 */
254 uint8_t year; /* 18 */
255 uint8_t month; /* 19 */
256 uint8_t day; /* 20 */
257 uint8_t course; /* 21 */
258 uint16_t ground_speed; /* 22 */
259 int16_t climb_rate; /* 24 */
260 uint8_t pdop; /* 26 */
261 uint8_t hdop; /* 27 */
262 uint8_t vdop; /* 28 */
263 uint8_t mode; /* 29 */
264 int16_t altitude_high; /* 30 */
268 uint16_t channels; /* 4 */
277 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
278 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
279 ((l)->u.gps.altitude_high = (a) >> 16), \
280 (l)->u.gps.altitude_low = (a))
282 struct ao_log_metrum {
284 uint8_t csum; /* 1 */
285 uint16_t tick; /* 2 */
289 uint16_t flight; /* 4 */
290 int16_t ground_accel; /* 6 */
291 uint32_t ground_pres; /* 8 */
292 uint32_t ground_temp; /* 12 */
296 uint16_t state; /* 4 */
297 uint16_t reason; /* 6 */
301 uint32_t pres; /* 4 */
302 uint32_t temp; /* 8 */
303 int16_t accel; /* 12 */
305 /* AO_LOG_TEMP_VOLT */
307 int16_t v_batt; /* 4 */
308 int16_t sense_a; /* 6 */
309 int16_t sense_m; /* 8 */
313 int32_t latitude; /* 4 */
314 int32_t longitude; /* 8 */
315 uint16_t altitude_low; /* 12 */
316 int16_t altitude_high; /* 14 */
318 /* AO_LOG_GPS_TIME */
320 uint8_t hour; /* 4 */
321 uint8_t minute; /* 5 */
322 uint8_t second; /* 6 */
323 uint8_t flags; /* 7 */
324 uint8_t year; /* 8 */
325 uint8_t month; /* 9 */
326 uint8_t day; /* 10 */
327 uint8_t pdop; /* 11 */
329 /* AO_LOG_GPS_SAT (up to three packets) */
331 uint8_t channels; /* 4 */
332 uint8_t more; /* 5 */
338 uint8_t raw[12]; /* 4 */
344 uint8_t csum; /* 1 */
345 uint16_t tick; /* 2 */
349 uint16_t flight; /* 4 */
351 uint32_t ground_pres; /* 8 */
355 uint16_t state; /* 4 */
356 uint16_t reason; /* 6 */
360 uint8_t pres[3]; /* 4 */
361 uint8_t temp[3]; /* 7 */
362 int16_t sense_a; /* 10 */
363 int16_t sense_m; /* 12 */
364 int16_t v_batt; /* 14 */
369 #define ao_log_pack24(dst,value) do { \
370 (dst)[0] = (value); \
371 (dst)[1] = (value) >> 8; \
372 (dst)[2] = (value) >> 16; \
377 uint8_t csum; /* 1 */
378 uint16_t tick; /* 2 */
382 uint16_t flight; /* 4 */
383 int16_t start_altitude; /* 6 */
384 int32_t start_latitude; /* 8 */
385 int32_t start_longitude; /* 12 */
387 /* AO_LOG_GPS_TIME */
389 int32_t latitude; /* 4 */
390 int32_t longitude; /* 8 */
391 uint16_t altitude_low; /* 12 */
392 uint8_t hour; /* 14 */
393 uint8_t minute; /* 15 */
394 uint8_t second; /* 16 */
395 uint8_t flags; /* 17 */
396 uint8_t year; /* 18 */
397 uint8_t month; /* 19 */
398 uint8_t day; /* 20 */
399 uint8_t course; /* 21 */
400 uint16_t ground_speed; /* 22 */
401 int16_t climb_rate; /* 24 */
402 uint8_t pdop; /* 26 */
403 uint8_t hdop; /* 27 */
404 uint8_t vdop; /* 28 */
405 uint8_t mode; /* 29 */
406 int16_t altitude_high; /* 30 */
410 uint16_t channels; /* 4 */
419 /* Write a record to the eeprom log */
421 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
424 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
427 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
430 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
433 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
439 ao_gps_report_metrum_init(void);
441 #endif /* _AO_LOG_H_ */