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_DETHERM 11 /* 16-byte MS5607 baro only, no ADC */
53 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
55 extern __code uint8_t ao_log_format;
56 extern __code uint8_t ao_log_size;
58 /* Return the flight number from the given log slot, 0 if none */
60 ao_log_flight(uint8_t slot);
62 /* Check if there is valid log data at the specified location */
64 ao_log_check(uint32_t pos);
70 /* Logging thread main routine */
74 /* functions provided in ao_log.c */
76 /* Figure out the current flight number */
78 ao_log_scan(void) __reentrant;
80 /* Return the position of the start of the given log slot */
82 ao_log_pos(uint8_t slot);
84 /* Start logging to eeprom */
92 /* Initialize the logging system */
96 /* Write out the current flight number to the erase log */
98 ao_log_write_erase(uint8_t pos);
100 /* Returns true if there are any logs stored in eeprom */
102 ao_log_present(void);
104 /* Returns true if there is no more storage space available */
113 * The data log is recorded in the eeprom as a sequence
116 * Each packet starts with a 4-byte header that has the
117 * packet type, the packet checksum and the tick count. Then
118 * they all contain 2 16 bit values which hold packet-specific
121 * For each flight, the first packet
122 * is FLIGHT packet, indicating the serial number of the
123 * device and a unique number marking the number of flights
124 * recorded by this device.
126 * During flight, data from the accelerometer and barometer
127 * are recorded in SENSOR packets, using the raw 16-bit values
128 * read from the A/D converter.
130 * Also during flight, but at a lower rate, the deployment
131 * sensors are recorded in DEPLOY packets. The goal here is to
132 * detect failure in the deployment circuits.
134 * STATE packets hold state transitions as the flight computer
135 * transitions through different stages of the flight.
137 #define AO_LOG_FLIGHT 'F'
138 #define AO_LOG_SENSOR 'A'
139 #define AO_LOG_TEMP_VOLT 'T'
140 #define AO_LOG_DEPLOY 'D'
141 #define AO_LOG_STATE 'S'
142 #define AO_LOG_GPS_TIME 'G'
143 #define AO_LOG_GPS_LAT 'N'
144 #define AO_LOG_GPS_LON 'W'
145 #define AO_LOG_GPS_ALT 'H'
146 #define AO_LOG_GPS_SAT 'V'
147 #define AO_LOG_GPS_DATE 'Y'
148 #define AO_LOG_GPS_POS 'P'
150 #define AO_LOG_POS_NONE (~0UL)
152 struct ao_log_record {
154 uint8_t csum; /* 1 */
155 uint16_t tick; /* 2 */
158 int16_t ground_accel; /* 4 */
159 uint16_t flight; /* 6 */
162 int16_t accel; /* 4 */
163 int16_t pres; /* 6 */
183 int32_t gps_latitude;
184 int32_t gps_longitude;
186 uint16_t altitude_low;
187 int16_t altitude_high;
209 uint8_t csum; /* 1 */
210 uint16_t tick; /* 2 */
214 uint16_t flight; /* 4 */
215 int16_t ground_accel; /* 6 */
216 uint32_t ground_pres; /* 8 */
217 int16_t ground_accel_along; /* 12 */
218 int16_t ground_accel_across; /* 14 */
219 int16_t ground_accel_through; /* 16 */
220 int16_t pad_18; /* 18 */
221 int32_t ground_roll; /* 20 */
222 int32_t ground_pitch; /* 24 */
223 int32_t ground_yaw; /* 28 */
226 uint16_t flight; /* 4 */
227 int16_t ground_accel; /* 6 */
228 uint32_t ground_pres; /* 8 */
229 int16_t ground_accel_along; /* 12 */
230 int16_t ground_accel_across; /* 14 */
231 int16_t ground_accel_through; /* 16 */
232 int16_t ground_roll; /* 18 */
233 int16_t ground_pitch; /* 20 */
234 int16_t ground_yaw; /* 22 */
235 } flight_old; /* 24 */
243 uint32_t pres; /* 4 */
244 uint32_t temp; /* 8 */
245 int16_t accel_x; /* 12 */
246 int16_t accel_y; /* 14 */
247 int16_t accel_z; /* 16 */
248 int16_t gyro_x; /* 18 */
249 int16_t gyro_y; /* 20 */
250 int16_t gyro_z; /* 22 */
251 int16_t mag_x; /* 24 */
252 int16_t mag_y; /* 26 */
253 int16_t mag_z; /* 28 */
254 int16_t accel; /* 30 */
256 /* AO_LOG_TEMP_VOLT */
258 int16_t v_batt; /* 4 */
259 int16_t v_pbatt; /* 6 */
260 int16_t n_sense; /* 8 */
261 int16_t sense[10]; /* 10 */
262 uint16_t pyro; /* 30 */
264 /* AO_LOG_GPS_TIME */
266 int32_t latitude; /* 4 */
267 int32_t longitude; /* 8 */
268 uint16_t altitude_low; /* 12 */
269 uint8_t hour; /* 14 */
270 uint8_t minute; /* 15 */
271 uint8_t second; /* 16 */
272 uint8_t flags; /* 17 */
273 uint8_t year; /* 18 */
274 uint8_t month; /* 19 */
275 uint8_t day; /* 20 */
276 uint8_t course; /* 21 */
277 uint16_t ground_speed; /* 22 */
278 int16_t climb_rate; /* 24 */
279 uint8_t pdop; /* 26 */
280 uint8_t hdop; /* 27 */
281 uint8_t vdop; /* 28 */
282 uint8_t mode; /* 29 */
283 int16_t altitude_high; /* 30 */
287 uint16_t channels; /* 4 */
296 #define AO_LOG_MEGA_GPS_ALTITUDE(l) ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
297 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a) (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
298 ((l)->u.gps.altitude_high = (a) >> 16), \
299 (l)->u.gps.altitude_low = (a))
301 struct ao_log_metrum {
303 uint8_t csum; /* 1 */
304 uint16_t tick; /* 2 */
308 uint16_t flight; /* 4 */
309 int16_t ground_accel; /* 6 */
310 uint32_t ground_pres; /* 8 */
311 uint32_t ground_temp; /* 12 */
315 uint16_t state; /* 4 */
316 uint16_t reason; /* 6 */
320 uint32_t pres; /* 4 */
321 uint32_t temp; /* 8 */
322 int16_t accel; /* 12 */
324 /* AO_LOG_TEMP_VOLT */
326 int16_t v_batt; /* 4 */
327 int16_t sense_a; /* 6 */
328 int16_t sense_m; /* 8 */
332 int32_t latitude; /* 4 */
333 int32_t longitude; /* 8 */
334 uint16_t altitude_low; /* 12 */
335 int16_t altitude_high; /* 14 */
337 /* AO_LOG_GPS_TIME */
339 uint8_t hour; /* 4 */
340 uint8_t minute; /* 5 */
341 uint8_t second; /* 6 */
342 uint8_t flags; /* 7 */
343 uint8_t year; /* 8 */
344 uint8_t month; /* 9 */
345 uint8_t day; /* 10 */
346 uint8_t pdop; /* 11 */
348 /* AO_LOG_GPS_SAT (up to three packets) */
350 uint8_t channels; /* 4 */
351 uint8_t more; /* 5 */
357 uint8_t raw[12]; /* 4 */
363 uint8_t csum; /* 1 */
364 uint16_t tick; /* 2 */
368 uint16_t flight; /* 4 */
370 uint32_t ground_pres; /* 8 */
374 uint16_t state; /* 4 */
375 uint16_t reason; /* 6 */
379 uint8_t pres[3]; /* 4 */
380 uint8_t temp[3]; /* 7 */
381 int16_t sense_a; /* 10 */
382 int16_t sense_m; /* 12 */
383 int16_t v_batt; /* 14 */
388 #define ao_log_pack24(dst,value) do { \
389 (dst)[0] = (value); \
390 (dst)[1] = (value) >> 8; \
391 (dst)[2] = (value) >> 16; \
396 uint8_t csum; /* 1 */
397 uint16_t tick; /* 2 */
401 uint16_t flight; /* 4 */
402 int16_t start_altitude; /* 6 */
403 int32_t start_latitude; /* 8 */
404 int32_t start_longitude; /* 12 */
406 /* AO_LOG_GPS_TIME */
408 int32_t latitude; /* 4 */
409 int32_t longitude; /* 8 */
410 uint16_t altitude_low; /* 12 */
411 uint8_t hour; /* 14 */
412 uint8_t minute; /* 15 */
413 uint8_t second; /* 16 */
414 uint8_t flags; /* 17 */
415 uint8_t year; /* 18 */
416 uint8_t month; /* 19 */
417 uint8_t day; /* 20 */
418 uint8_t course; /* 21 */
419 uint16_t ground_speed; /* 22 */
420 int16_t climb_rate; /* 24 */
421 uint8_t pdop; /* 26 */
422 uint8_t hdop; /* 27 */
423 uint8_t vdop; /* 28 */
424 uint8_t mode; /* 29 */
425 int16_t altitude_high; /* 30 */
429 uint16_t channels; /* 4 */
438 /* Write a record to the eeprom log */
440 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
443 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
446 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
449 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
452 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
458 ao_gps_report_metrum_init(void);
460 #endif /* _AO_LOG_H_ */