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