altoslib: Compute tilt angle from eeprom data
[fw/altos] / src / kernel / ao_log.h
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #ifndef _AO_LOG_H_
19 #define _AO_LOG_H_
20
21 #include <ao_flight.h>
22
23 /*
24  * ao_log.c
25  */
26
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.
30  */
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;
38
39 /* required functions from the underlying log system */
40
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 */
53
54 extern __code uint8_t ao_log_format;
55 extern __code uint8_t ao_log_size;
56
57 /* Return the flight number from the given log slot, 0 if none */
58 uint16_t
59 ao_log_flight(uint8_t slot);
60
61 /* Check if there is valid log data at the specified location */
62 uint8_t
63 ao_log_check(uint32_t pos);
64
65 /* Flush the log */
66 void
67 ao_log_flush(void);
68
69 /* Logging thread main routine */
70 void
71 ao_log(void);
72
73 /* functions provided in ao_log.c */
74
75 /* Figure out the current flight number */
76 uint8_t
77 ao_log_scan(void) __reentrant;
78
79 /* Return the position of the start of the given log slot */
80 uint32_t
81 ao_log_pos(uint8_t slot);
82
83 /* Start logging to eeprom */
84 void
85 ao_log_start(void);
86
87 /* Stop logging */
88 void
89 ao_log_stop(void);
90
91 /* Initialize the logging system */
92 void
93 ao_log_init(void);
94
95 /* Write out the current flight number to the erase log */
96 void
97 ao_log_write_erase(uint8_t pos);
98
99 /* Returns true if there are any logs stored in eeprom */
100 uint8_t
101 ao_log_present(void);
102
103 /* Returns true if there is no more storage space available */
104 uint8_t
105 ao_log_full(void);
106
107 /*
108  * ao_log_big.c
109  */
110
111 /*
112  * The data log is recorded in the eeprom as a sequence
113  * of data packets.
114  *
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
118  * data.
119  *
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.
124  *
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.
128  *
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.
132  *
133  * STATE packets hold state transitions as the flight computer
134  * transitions through different stages of the flight.
135  */
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'
148
149 #define AO_LOG_POS_NONE         (~0UL)
150
151 struct ao_log_record {
152         char                    type;                           /* 0 */
153         uint8_t                 csum;                           /* 1 */
154         uint16_t                tick;                           /* 2 */
155         union {
156                 struct {
157                         int16_t         ground_accel;           /* 4 */
158                         uint16_t        flight;                 /* 6 */
159                 } flight;
160                 struct {
161                         int16_t         accel;                  /* 4 */
162                         int16_t         pres;                   /* 6 */
163                 } sensor;
164                 struct {
165                         int16_t         temp;
166                         int16_t         v_batt;
167                 } temp_volt;
168                 struct {
169                         int16_t         drogue;
170                         int16_t         main;
171                 } deploy;
172                 struct {
173                         uint16_t        state;
174                         uint16_t        reason;
175                 } state;
176                 struct {
177                         uint8_t         hour;
178                         uint8_t         minute;
179                         uint8_t         second;
180                         uint8_t         flags;
181                 } gps_time;
182                 int32_t         gps_latitude;
183                 int32_t         gps_longitude;
184                 struct {
185                         uint16_t        altitude_low;
186                         int16_t         altitude_high;
187                 } gps_altitude;
188                 struct {
189                         uint16_t        svid;
190                         uint8_t         unused;
191                         uint8_t         c_n;
192                 } gps_sat;
193                 struct {
194                         uint8_t         year;
195                         uint8_t         month;
196                         uint8_t         day;
197                         uint8_t         extra;
198                 } gps_date;
199                 struct {
200                         uint16_t        d0;
201                         uint16_t        d1;
202                 } anon;
203         } u;
204 };
205
206 struct ao_log_mega {
207         char                    type;                   /* 0 */
208         uint8_t                 csum;                   /* 1 */
209         uint16_t                tick;                   /* 2 */
210         union {                                         /* 4 */
211                 /* AO_LOG_FLIGHT */
212                 struct {
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 */
220                         int32_t         ground_roll;            /* 20 */
221                         int32_t         ground_pitch;           /* 24 */
222                         int32_t         ground_yaw;             /* 28 */
223                 } flight;                                       /* 32 */
224                 struct {
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 */
235                 /* AO_LOG_STATE */
236                 struct {
237                         uint16_t        state;
238                         uint16_t        reason;
239                 } state;
240                 /* AO_LOG_SENSOR */
241                 struct {
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 */
254                 } sensor;       /* 32 */
255                 /* AO_LOG_TEMP_VOLT */
256                 struct {
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 */
262                 } volt;                                 /* 32 */
263                 /* AO_LOG_GPS_TIME */
264                 struct {
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 */
283                 } gps;  /* 32 */
284                 /* AO_LOG_GPS_SAT */
285                 struct {
286                         uint16_t        channels;       /* 4 */
287                         struct {
288                                 uint8_t svid;
289                                 uint8_t c_n;
290                         } sats[12];                     /* 6 */
291                 } gps_sat;                              /* 30 */
292         } u;
293 };
294
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))
299
300 struct ao_log_metrum {
301         char                    type;                   /* 0 */
302         uint8_t                 csum;                   /* 1 */
303         uint16_t                tick;                   /* 2 */
304         union {                                         /* 4 */
305                 /* AO_LOG_FLIGHT */
306                 struct {
307                         uint16_t        flight;         /* 4 */
308                         int16_t         ground_accel;   /* 6 */
309                         uint32_t        ground_pres;    /* 8 */
310                         uint32_t        ground_temp;    /* 12 */
311                 } flight;       /* 16 */
312                 /* AO_LOG_STATE */
313                 struct {
314                         uint16_t        state;          /* 4 */
315                         uint16_t        reason;         /* 6 */
316                 } state;        /* 8 */
317                 /* AO_LOG_SENSOR */
318                 struct {
319                         uint32_t        pres;           /* 4 */
320                         uint32_t        temp;           /* 8 */
321                         int16_t         accel;          /* 12 */
322                 } sensor;       /* 14 */
323                 /* AO_LOG_TEMP_VOLT */
324                 struct {
325                         int16_t         v_batt;         /* 4 */
326                         int16_t         sense_a;        /* 6 */
327                         int16_t         sense_m;        /* 8 */
328                 } volt;         /* 10 */
329                 /* AO_LOG_GPS_POS */
330                 struct {
331                         int32_t         latitude;       /* 4 */
332                         int32_t         longitude;      /* 8 */
333                         uint16_t        altitude_low;   /* 12 */
334                         int16_t         altitude_high;  /* 14 */
335                 } gps;          /* 16 */
336                 /* AO_LOG_GPS_TIME */
337                 struct {
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 */
346                 } gps_time;     /* 12 */
347                 /* AO_LOG_GPS_SAT (up to three packets) */
348                 struct {
349                         uint8_t channels;               /* 4 */
350                         uint8_t more;                   /* 5 */
351                         struct {
352                                 uint8_t svid;
353                                 uint8_t c_n;
354                         } sats[4];                      /* 6 */
355                 } gps_sat;                              /* 14 */
356                 uint8_t         raw[12];                /* 4 */
357         } u;    /* 16 */
358 };
359
360 struct ao_log_mini {
361         char            type;                           /* 0 */
362         uint8_t         csum;                           /* 1 */
363         uint16_t        tick;                           /* 2 */
364         union {                                         /* 4 */
365                 /* AO_LOG_FLIGHT */
366                 struct {
367                         uint16_t        flight;         /* 4 */
368                         uint16_t        r6;
369                         uint32_t        ground_pres;    /* 8 */
370                 } flight;
371                 /* AO_LOG_STATE */
372                 struct {
373                         uint16_t        state;          /* 4 */
374                         uint16_t        reason;         /* 6 */
375                 } state;
376                 /* AO_LOG_SENSOR */
377                 struct {
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 */
383                 } sensor;                               /* 16 */
384         } u;                                            /* 16 */
385 };                                                      /* 16 */
386
387 #define ao_log_pack24(dst,value) do {           \
388                 (dst)[0] = (value);             \
389                 (dst)[1] = (value) >> 8;        \
390                 (dst)[2] = (value) >> 16;       \
391         } while (0)
392
393 struct ao_log_gps {
394         char                    type;                   /* 0 */
395         uint8_t                 csum;                   /* 1 */
396         uint16_t                tick;                   /* 2 */
397         union {                                         /* 4 */
398                 /* AO_LOG_FLIGHT */
399                 struct {
400                         uint16_t        flight;                 /* 4 */
401                         int16_t         start_altitude;         /* 6 */
402                         int32_t         start_latitude;         /* 8 */
403                         int32_t         start_longitude;        /* 12 */
404                 } flight;                                       /* 16 */
405                 /* AO_LOG_GPS_TIME */
406                 struct {
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 */
425                 } gps;  /* 31 */
426                 /* AO_LOG_GPS_SAT */
427                 struct {
428                         uint16_t        channels;       /* 4 */
429                         struct {
430                                 uint8_t svid;
431                                 uint8_t c_n;
432                         } sats[12];                     /* 6 */
433                 } gps_sat;                              /* 30 */
434         } u;
435 };
436
437 /* Write a record to the eeprom log */
438 uint8_t
439 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
440
441 uint8_t
442 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
443
444 uint8_t
445 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
446
447 uint8_t
448 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
449
450 uint8_t
451 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
452
453 void
454 ao_log_flush(void);
455
456 void
457 ao_gps_report_metrum_init(void);
458
459 #endif /* _AO_LOG_H_ */