altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / lib / cc-telemetry.h
1 /*
2  * Copyright © 2011 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _CC_TELEMETRY_H_
20 #define _CC_TELEMETRY_H_
21 /*
22  * ao_telemetry.c
23  */
24 #define AO_MAX_CALLSIGN                 8
25 #define AO_MAX_VERSION                  8
26 #define AO_MAX_TELEMETRY                128
27
28 struct ao_telemetry_generic {
29         uint16_t        serial;         /* 0 */
30         uint16_t        tick;           /* 2 */
31         uint8_t         type;           /* 4 */
32         uint8_t         payload[27];    /* 5 */
33         uint8_t         rssi;           /* 32 */
34         uint8_t         status;         /* 33 */
35         /* 34 */
36 };
37
38 #define AO_TELEMETRY_SENSOR_TELEMETRUM  0x01
39 #define AO_TELEMETRY_SENSOR_TELEMINI    0x02
40 #define AO_TELEMETRY_SENSOR_TELENANO    0x03
41
42 struct ao_telemetry_sensor {
43         uint16_t        serial;         /*  0 */
44         uint16_t        tick;           /*  2 */
45         uint8_t         type;           /*  4 */
46
47         uint8_t         state;          /*  5 flight state */
48         int16_t         accel;          /*  6 accelerometer (TM only) */
49         int16_t         pres;           /*  8 pressure sensor */
50         int16_t         temp;           /* 10 temperature sensor */
51         int16_t         v_batt;         /* 12 battery voltage */
52         int16_t         sense_d;        /* 14 drogue continuity sense (TM/Tm) */
53         int16_t         sense_m;        /* 16 main continuity sense (TM/Tm) */
54
55         int16_t         acceleration;   /* 18 m/s² * 16 */
56         int16_t         speed;          /* 20 m/s * 16 */
57         int16_t         height;         /* 22 m */
58
59         int16_t         ground_pres;    /* 24 average pres on pad */
60         int16_t         ground_accel;   /* 26 average accel on pad */
61         int16_t         accel_plus_g;   /* 28 accel calibration at +1g */
62         int16_t         accel_minus_g;  /* 30 accel calibration at -1g */
63         /* 32 */
64 };
65
66 #define AO_TELEMETRY_CONFIGURATION      0x04
67
68 struct ao_telemetry_configuration {
69         uint16_t        serial;                         /*  0 */
70         uint16_t        tick;                           /*  2 */
71         uint8_t         type;                           /*  4 */
72
73         uint8_t         device;                         /*  5 device type */
74         uint16_t        flight;                         /*  6 flight number */
75         uint8_t         config_major;                   /*  8 Config major version */
76         uint8_t         config_minor;                   /*  9 Config minor version */
77         uint16_t        apogee_delay;                   /* 10 Apogee deploy delay in seconds */
78         uint16_t        main_deploy;                    /* 12 Main deploy alt in meters */
79         uint16_t        flight_log_max;                 /* 14 Maximum flight log size in kB */
80         char            callsign[AO_MAX_CALLSIGN];      /* 16 Radio operator identity */
81         char            version[AO_MAX_VERSION];        /* 24 Software version */
82         /* 32 */
83 };
84
85 #define AO_TELEMETRY_LOCATION           0x05
86
87 #define AO_GPS_MODE_NOT_VALID           'N'
88 #define AO_GPS_MODE_AUTONOMOUS          'A'
89 #define AO_GPS_MODE_DIFFERENTIAL        'D'
90 #define AO_GPS_MODE_ESTIMATED           'E'
91 #define AO_GPS_MODE_MANUAL              'M'
92 #define AO_GPS_MODE_SIMULATED           'S'
93
94 #define AO_GPS_MODE_ALTITUDE_24         (1 << 0)        /* reports 24-bits of altitude */
95
96 struct ao_telemetry_location {
97         uint16_t        serial;         /*  0 */
98         uint16_t        tick;           /*  2 */
99         uint8_t         type;           /*  4 */
100
101         uint8_t         flags;          /*  5 Number of sats and other flags */
102         int16_t         altitude_low;   /*  6 GPS reported altitude (m) */
103         int32_t         latitude;       /*  8 latitude (degrees * 10⁷) */
104         int32_t         longitude;      /* 12 longitude (degrees * 10⁷) */
105         uint8_t         year;           /* 16 (- 2000) */
106         uint8_t         month;          /* 17 (1-12) */
107         uint8_t         day;            /* 18 (1-31) */
108         uint8_t         hour;           /* 19 (0-23) */
109         uint8_t         minute;         /* 20 (0-59) */
110         uint8_t         second;         /* 21 (0-59) */
111         uint8_t         pdop;           /* 22 (m * 5) */
112         uint8_t         hdop;           /* 23 (m * 5) */
113         uint8_t         vdop;           /* 24 (m * 5) */
114         uint8_t         mode;           /* 25 */
115         uint16_t        ground_speed;   /* 26 cm/s */
116         int16_t         climb_rate;     /* 28 cm/s */
117         uint8_t         course;         /* 30 degrees / 2 */
118         int8_t          altitude_high;  /* 31 */
119         /* 32 */
120 };
121
122 typedef int32_t gps_alt_t;
123 #define AO_TELEMETRY_LOCATION_ALTITUDE(l) \
124         ((l)->altitude_low | (((l)->mode & AO_GPS_MODE_ALTITUDE_24) ? \
125                               ((gps_alt_t) (l)->altitude_high << 16) : 0))
126
127 #define AO_TELEMETRY_SATELLITE          0x06
128
129 struct ao_telemetry_satellite_info {
130         uint8_t         svid;
131         uint8_t         c_n_1;
132 };
133
134 #define AO_TELEMETRY_SATELLITE_MAX_SAT  12
135
136
137 struct ao_telemetry_satellite {
138         uint16_t                                serial;         /*  0 */
139         uint16_t                                tick;           /*  2 */
140         uint8_t                                 type;           /*  4 */
141         uint8_t                                 channels;       /*  5 number of reported sats */
142
143         struct ao_telemetry_satellite_info      sats[AO_TELEMETRY_SATELLITE_MAX_SAT];   /* 6 */
144         uint8_t                                 unused[2];      /* 30 */
145         /* 32 */
146 };
147
148 #define AO_TELEMETRY_COMPANION          0x07
149
150 #define AO_COMPANION_MAX_CHANNELS       12
151
152 struct ao_telemetry_companion {
153         uint16_t                                serial;         /*  0 */
154         uint16_t                                tick;           /*  2 */
155         uint8_t                                 type;           /*  4 */
156         uint8_t                                 board_id;       /*  5 */
157
158         uint8_t                                 update_period;  /*  6 */
159         uint8_t                                 channels;       /*  7 */
160         uint16_t                                companion_data[AO_COMPANION_MAX_CHANNELS];      /*  8 */
161         /* 32 */
162 };
163
164 #define AO_TELEMETRY_MEGA_SENSOR_MPU            0x08    /* Invensense IMU */
165 #define AO_TELEMETRY_MEGA_SENSOR_BMX160         0x12    /* BMX160 IMU */
166
167 struct ao_telemetry_mega_sensor {
168         uint16_t        serial;         /*  0 */
169         uint16_t        tick;           /*  2 */
170         uint8_t         type;           /*  4 */
171
172         uint8_t         orient;         /*  5 angle from vertical */
173         int16_t         accel;          /*  6 Z axis */
174
175         int32_t         pres;           /*  8 Pa * 10 */
176         int16_t         temp;           /* 12 °C * 100 */
177
178         int16_t         accel_x;        /* 14 */
179         int16_t         accel_y;        /* 16 */
180         int16_t         accel_z;        /* 18 */
181
182         int16_t         gyro_x;         /* 20 */
183         int16_t         gyro_y;         /* 22 */
184         int16_t         gyro_z;         /* 24 */
185
186         int16_t         mag_x;          /* 26 */
187         int16_t         mag_z;          /* 30 */
188         int16_t         mag_y;          /* 28 */
189         /* 32 */
190 };
191         
192 #define AO_TELEMETRY_MEGA_DATA          0x09
193
194 struct ao_telemetry_mega_data {
195         uint16_t        serial;         /*  0 */
196         uint16_t        tick;           /*  2 */
197         uint8_t         type;           /*  4 */
198
199         uint8_t         state;          /*  5 flight state */
200
201         int16_t         v_batt;         /*  6 battery voltage */
202         int16_t         v_pyro;         /*  8 pyro battery voltage */
203         int8_t          sense[6];       /* 10 continuity sense */
204
205         int32_t         ground_pres;    /* 16 average pres on pad */
206         int16_t         ground_accel;   /* 20 average accel on pad */
207         int16_t         accel_plus_g;   /* 22 accel calibration at +1g */
208         int16_t         accel_minus_g;  /* 24 accel calibration at -1g */
209
210         int16_t         acceleration;   /* 26 m/s² * 16 */
211         int16_t         speed;          /* 28 m/s * 16 */
212         int16_t         height;         /* 30 m */
213         /* 32 */
214 };
215
216 #define AO_TELEMETRY_METRUM_SENSOR      0x0A
217
218 struct ao_telemetry_metrum_sensor {
219         uint16_t        serial;         /*  0 */
220         uint16_t        tick;           /*  2 */
221         uint8_t         type;           /*  4 */
222
223         uint8_t         state;          /*  5 flight state */
224         int16_t         accel;          /*  6 Z axis */
225
226         int32_t         pres;           /*  8 Pa * 10 */
227         int16_t         temp;           /* 12 °C * 100 */
228
229         int16_t         acceleration;   /* 14 m/s² * 16 */
230         int16_t         speed;          /* 16 m/s * 16 */
231         int16_t         height;         /* 18 m */
232
233         int16_t         v_batt;         /* 20 battery voltage */
234         int16_t         sense_a;        /* 22 apogee continuity sense */
235         int16_t         sense_m;        /* 24 main continuity sense */
236
237         uint8_t         pad[6];         /* 26 */
238         /* 32 */
239 };
240         
241 #define AO_TELEMETRY_METRUM_DATA        0x0B
242
243 struct ao_telemetry_metrum_data {
244         uint16_t        serial;         /*  0 */
245         uint16_t        tick;           /*  2 */
246         uint8_t         type;           /*  4 */
247         uint8_t         pad5[3];        /*  5 */
248
249         int32_t         ground_pres;    /* 8 average pres on pad */
250         int16_t         ground_accel;   /* 12 average accel on pad */
251         int16_t         accel_plus_g;   /* 14 accel calibration at +1g */
252         int16_t         accel_minus_g;  /* 16 accel calibration at -1g */
253
254         uint8_t         pad18[14];      /* 18 */
255         /* 32 */
256 };
257
258 #define AO_TELEMETRY_MINI               0x10
259 #define AO_TELEMETRY_MINI3              0x11
260
261 struct ao_telemetry_mini {
262         uint16_t        serial;         /*  0 */
263         uint16_t        tick;           /*  2 */
264         uint8_t         type;           /*  4 */
265
266         uint8_t         state;          /*  5 flight state */
267         int16_t         v_batt;         /*  6 battery voltage */
268         int16_t         sense_a;        /*  8 apogee continuity */
269         int16_t         sense_m;        /* 10 main continuity */
270
271         int32_t         pres;           /* 12 Pa * 10 */
272         int16_t         temp;           /* 16 °C * 100 */
273
274         int16_t         acceleration;   /* 18 m/s² * 16 */
275         int16_t         speed;          /* 20 m/s * 16 */
276         int16_t         height;         /* 22 m */
277
278         int32_t         ground_pres;    /* 24 average pres on pad */
279
280         int32_t         pad28;          /* 28 */
281         /* 32 */
282 };
283
284 /* #define AO_SEND_ALL_BARO */
285
286 #define AO_TELEMETRY_BARO               0x80
287
288 /*
289  * This packet allows the full sampling rate baro
290  * data to be captured over the RF link so that the
291  * flight software can be tested using 'real' data.
292  *
293  * Along with this telemetry packet, the flight
294  * code is modified to send full-rate telemetry all the time
295  * and never send an RDF tone; this ensure that the full radio
296  * link is available.
297  */
298 struct ao_telemetry_baro {
299         uint16_t                                serial;         /*  0 */
300         uint16_t                                tick;           /*  2 */
301         uint8_t                                 type;           /*  4 */
302         uint8_t                                 samples;        /*  5 number samples */
303
304         int16_t                                 baro[12];       /* 6 samples */
305         /* 32 */
306 };
307
308 union ao_telemetry_all {
309         struct ao_telemetry_generic             generic;
310         struct ao_telemetry_sensor              sensor;
311         struct ao_telemetry_configuration       configuration;
312         struct ao_telemetry_location            location;
313         struct ao_telemetry_satellite           satellite;
314         struct ao_telemetry_companion           companion;
315         struct ao_telemetry_mega_sensor         mega_sensor;
316         struct ao_telemetry_mega_data           mega_data;
317         struct ao_telemetry_metrum_sensor       metrum_sensor;
318         struct ao_telemetry_metrum_data         metrum_data;
319         struct ao_telemetry_mini                mini;
320         struct ao_telemetry_baro                baro;
321 };
322
323 #define CC_TELEMETRY_HEADER     "TELEM"
324
325 /* "TELEM " 1 byte length 32 data bytes 1 rssi 1 status 1 checksum 1 null */
326
327 #define CC_TELEMETRY_BUFSIZE    (6 + (1 + 32 + 3) * 2 + 1)
328
329 int
330 cc_telemetry_parse(const char *input_line, union ao_telemetry_all *telemetry);
331
332 uint8_t
333 cc_telemetry_cksum(const union ao_telemetry_all *telemetry);
334
335 void
336 cc_telemetry_unparse(const union ao_telemetry_all *telemetry, char output_line[CC_TELEMETRY_BUFSIZE]);
337
338 #endif