altosuilib: Wait for product data while scanning
[fw/altos] / altoslib / AltosTelemetryLegacy.java
1 /*
2  * Copyright © 2010 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 package org.altusmetrum.altoslib_5;
19
20 import java.text.*;
21
22 /*
23  * Telemetry data contents
24  */
25
26
27 /*
28  * The packet format is a simple hex dump of the raw telemetry frame.
29  * It starts with 'TELEM', then contains hex digits with a checksum as the last
30  * byte on the line.
31  *
32  * Version 4 is a replacement with consistent syntax. Each telemetry line
33  * contains a sequence of space-separated names and values, the values are
34  * either integers or strings. The names are all unique. All values are
35  * optional
36  *
37  * VERSION 4 c KD7SQG n 236 f 18 r -25 s pad t 513 r_a 15756 r_b 26444 r_t 20944
38  *   r_v 26640 r_d 512 r_m 208 c_a 15775 c_b 26439 c_p 15749 c_m 16281 a_a 15764
39  *   a_s 0 a_b 26439 g_s u g_n 0 s_n 0
40  *
41  * VERSION 4 c KD7SQG n 19 f 0 r -23 s pad t 513 r_b 26372 r_t 21292 r_v 26788
42  *   r_d 136 r_m 140 c_b 26370 k_h 0 k_s 0 k_a 0
43  *
44  * General header fields
45  *
46  *      Name            Value
47  *
48  *      VERSION         Telemetry version number (4 or more). Must be first.
49  *      c               Callsign (string, no spaces allowed)
50  *      n               Flight unit serial number (integer)
51  *      f               Flight number (integer)
52  *      r               Packet RSSI value (integer)
53  *      s               Flight computer state (string, no spaces allowed)
54  *      t               Flight computer clock (integer in centiseconds)
55  *
56  * Version 3 is Version 2 with fixed RSSI numbers -- the radio reports
57  * in 1/2dB increments while this protocol provides only integers. So,
58  * the syntax didn't change just the interpretation of the RSSI
59  * values.
60  *
61  * Version 2 of the telemetry data stream is a bit of a mess, with no
62  * consistent formatting. In particular, the GPS data is formatted for
63  * viewing instead of parsing.  However, the key feature is that every
64  * telemetry line contains all of the information necessary to
65  * describe the current rocket state, including the calibration values
66  * for accelerometer and barometer.
67  *
68  * GPS unlocked:
69  *
70  * VERSION 2 CALL KB0G SERIAL  51 FLIGHT     2 RSSI  -68 STATUS ff STATE     pad  1001 \
71  *    a: 16032 p: 21232 t: 20284 v: 25160 d:   204 m:   204 fa: 16038 ga: 16032 fv:       0 \
72  *    fp: 21232 gp: 21230 a+: 16049 a-: 16304 GPS  0 sat unlocked SAT 1   15  30
73  *
74  * GPS locked:
75  *
76  * VERSION 2 CALL KB0G SERIAL  51 FLIGHT     2 RSSI  -71 STATUS ff STATE     pad  2504 \
77  *     a: 16028 p: 21220 t: 20360 v: 25004 d:   208 m:   200 fa: 16031 ga: 16032 fv:     330 \
78  *     fp: 21231 gp: 21230 a+: 16049 a-: 16304 \
79  *     GPS  9 sat 2010-02-13 17:16:51 35°20.0803'N 106°45.2235'W  1790m  \
80  *     0.00m/s(H) 0°     0.00m/s(V) 1.0(hdop)     0(herr)     0(verr) \
81  *     SAT 10   29  30  24  28   5  25  21  20  15  33   1  23  30  24  18  26  10  29   2  26
82  *
83  */
84
85 public class AltosTelemetryLegacy extends AltosTelemetry {
86         /*
87          * General header fields
88          *
89          *      Name            Value
90          *
91          *      VERSION         Telemetry version number (4 or more). Must be first.
92          *      c               Callsign (string, no spaces allowed)
93          *      n               Flight unit serial number (integer)
94          *      f               Flight number (integer)
95          *      r               Packet RSSI value (integer)
96          *      s               Flight computer state (string, no spaces allowed)
97          *      t               Flight computer clock (integer in centiseconds)
98          */
99
100         final static String AO_TELEM_VERSION    = "VERSION";
101         final static String AO_TELEM_CALL       = "c";
102         final static String AO_TELEM_SERIAL     = "n";
103         final static String AO_TELEM_FLIGHT     = "f";
104         final static String AO_TELEM_RSSI       = "r";
105         final static String AO_TELEM_STATE      = "s";
106         final static String AO_TELEM_TICK       = "t";
107
108         /*
109          * Raw sensor values
110          *
111          *      Name            Value
112          *      r_a             Accelerometer reading (integer)
113          *      r_b             Barometer reading (integer)
114          *      r_t             Thermometer reading (integer)
115          *      r_v             Battery reading (integer)
116          *      r_d             Drogue continuity (integer)
117          *      r_m             Main continuity (integer)
118          */
119
120         final static String AO_TELEM_RAW_ACCEL  = "r_a";
121         final static String AO_TELEM_RAW_BARO   = "r_b";
122         final static String AO_TELEM_RAW_THERMO = "r_t";
123         final static String AO_TELEM_RAW_BATT   = "r_v";
124         final static String AO_TELEM_RAW_DROGUE = "r_d";
125         final static String AO_TELEM_RAW_MAIN   = "r_m";
126
127         /*
128          * Sensor calibration values
129          *
130          *      Name            Value
131          *      c_a             Ground accelerometer reading (integer)
132          *      c_b             Ground barometer reading (integer)
133          *      c_p             Accelerometer reading for +1g
134          *      c_m             Accelerometer reading for -1g
135          */
136
137         final static String AO_TELEM_CAL_ACCEL_GROUND   = "c_a";
138         final static String AO_TELEM_CAL_BARO_GROUND    = "c_b";
139         final static String AO_TELEM_CAL_ACCEL_PLUS     = "c_p";
140         final static String AO_TELEM_CAL_ACCEL_MINUS    = "c_m";
141
142         /*
143          * Kalman state values
144          *
145          *      Name            Value
146          *      k_h             Height above pad (integer, meters)
147          *      k_s             Vertical speeed (integer, m/s * 16)
148          *      k_a             Vertical acceleration (integer, m/s² * 16)
149          */
150
151         final static String AO_TELEM_KALMAN_HEIGHT      = "k_h";
152         final static String AO_TELEM_KALMAN_SPEED       = "k_s";
153         final static String AO_TELEM_KALMAN_ACCEL       = "k_a";
154
155         /*
156          * Ad-hoc flight values
157          *
158          *      Name            Value
159          *      a_a             Acceleration (integer, sensor units)
160          *      a_s             Speed (integer, integrated acceleration value)
161          *      a_b             Barometer reading (integer, sensor units)
162          */
163
164         final static String AO_TELEM_ADHOC_ACCEL        = "a_a";
165         final static String AO_TELEM_ADHOC_SPEED        = "a_s";
166         final static String AO_TELEM_ADHOC_BARO         = "a_b";
167
168         /*
169          * GPS values
170          *
171          *      Name            Value
172          *      g_s             GPS state (string):
173          *                              l       locked
174          *                              u       unlocked
175          *                              e       error (missing or broken)
176          *      g_n             Number of sats used in solution
177          *      g_ns            Latitude (degrees * 10e7)
178          *      g_ew            Longitude (degrees * 10e7)
179          *      g_a             Altitude (integer meters)
180          *      g_Y             GPS year (integer)
181          *      g_M             GPS month (integer - 1-12)
182          *      g_D             GPS day (integer - 1-31)
183          *      g_h             GPS hour (integer - 0-23)
184          *      g_m             GPS minute (integer - 0-59)
185          *      g_s             GPS second (integer - 0-59)
186          *      g_v             GPS vertical speed (integer, cm/sec)
187          *      g_s             GPS horizontal speed (integer, cm/sec)
188          *      g_c             GPS course (integer, 0-359)
189          *      g_pd            GPS pdop (integer * 10)
190          *      g_hd            GPS hdop (integer * 10)
191          *      g_vd            GPS vdop (integer * 10)
192          *      g_he            GPS h error (integer)
193          *      g_ve            GPS v error (integer)
194          */
195
196         final static String AO_TELEM_GPS_STATE                  = "g";
197         final static String AO_TELEM_GPS_STATE_LOCKED           = "l";
198         final static String AO_TELEM_GPS_STATE_UNLOCKED         = "u";
199         final static String AO_TELEM_GPS_STATE_ERROR            = "e";
200         final static String AO_TELEM_GPS_NUM_SAT                = "g_n";
201         final static String AO_TELEM_GPS_LATITUDE               = "g_ns";
202         final static String AO_TELEM_GPS_LONGITUDE              = "g_ew";
203         final static String AO_TELEM_GPS_ALTITUDE               = "g_a";
204         final static String AO_TELEM_GPS_YEAR                   = "g_Y";
205         final static String AO_TELEM_GPS_MONTH                  = "g_M";
206         final static String AO_TELEM_GPS_DAY                    = "g_D";
207         final static String AO_TELEM_GPS_HOUR                   = "g_h";
208         final static String AO_TELEM_GPS_MINUTE                 = "g_m";
209         final static String AO_TELEM_GPS_SECOND                 = "g_s";
210         final static String AO_TELEM_GPS_VERTICAL_SPEED         = "g_v";
211         final static String AO_TELEM_GPS_HORIZONTAL_SPEED       = "g_g";
212         final static String AO_TELEM_GPS_COURSE                 = "g_c";
213         final static String AO_TELEM_GPS_PDOP                   = "g_pd";
214         final static String AO_TELEM_GPS_HDOP                   = "g_hd";
215         final static String AO_TELEM_GPS_VDOP                   = "g_vd";
216         final static String AO_TELEM_GPS_HERROR                 = "g_he";
217         final static String AO_TELEM_GPS_VERROR                 = "g_ve";
218
219         /*
220          * GPS satellite values
221          *
222          *      Name            Value
223          *      s_n             Number of satellites reported (integer)
224          *      s_v0            Space vehicle ID (integer) for report 0
225          *      s_c0            C/N0 number (integer) for report 0
226          *      s_v1            Space vehicle ID (integer) for report 1
227          *      s_c1            C/N0 number (integer) for report 1
228          *      ...
229          */
230
231         final static String AO_TELEM_SAT_NUM    = "s_n";
232         final static String AO_TELEM_SAT_SVID   = "s_v";
233         final static String AO_TELEM_SAT_C_N_0  = "s_c";
234
235         public int      version;
236         public String   callsign;
237         public int      flight;
238         public int      state;
239
240         public AltosGPS gps;
241         public int      gps_sequence;
242
243         /* Telemetry sources have these values recorded from the flight computer */
244         public double   kalman_height;
245         public double   kalman_speed;
246         public double   kalman_acceleration;
247
248         /* Sensor values */
249         public int      accel;
250         public int      pres;
251         public int      temp;
252         public int      batt;
253         public int      apogee;
254         public int      main;
255
256         public int      ground_accel;
257         public int      ground_pres;
258         public int      accel_plus_g;
259         public int      accel_minus_g;
260
261         public int      flight_accel;
262         public int      flight_vel;
263         public int      flight_pres;
264
265         private void parse_v4(String[] words, int i) throws ParseException {
266                 AltosTelemetryMap       map = new AltosTelemetryMap(words, i);
267
268                 callsign = map.get_string(AO_TELEM_CALL, "N0CALL");
269                 serial = map.get_int(AO_TELEM_SERIAL, AltosLib.MISSING);
270                 flight = map.get_int(AO_TELEM_FLIGHT, AltosLib.MISSING);
271                 rssi = map.get_int(AO_TELEM_RSSI, AltosLib.MISSING);
272                 state = AltosLib.state(map.get_string(AO_TELEM_STATE, "invalid"));
273                 tick = map.get_int(AO_TELEM_TICK, 0);
274
275                 /* raw sensor values */
276                 accel = map.get_int(AO_TELEM_RAW_ACCEL, AltosLib.MISSING);
277                 pres = map.get_int(AO_TELEM_RAW_BARO, AltosLib.MISSING);
278                 temp = map.get_int(AO_TELEM_RAW_THERMO, AltosLib.MISSING);
279                 batt = map.get_int(AO_TELEM_RAW_BATT, AltosLib.MISSING);
280                 apogee = map.get_int(AO_TELEM_RAW_DROGUE, AltosLib.MISSING);
281                 main = map.get_int(AO_TELEM_RAW_MAIN, AltosLib.MISSING);
282
283                 /* sensor calibration information */
284                 ground_accel = map.get_int(AO_TELEM_CAL_ACCEL_GROUND, AltosLib.MISSING);
285                 ground_pres = map.get_int(AO_TELEM_CAL_BARO_GROUND, AltosLib.MISSING);
286                 accel_plus_g = map.get_int(AO_TELEM_CAL_ACCEL_PLUS, AltosLib.MISSING);
287                 accel_minus_g = map.get_int(AO_TELEM_CAL_ACCEL_MINUS, AltosLib.MISSING);
288
289                 /* flight computer values */
290                 kalman_acceleration = map.get_double(AO_TELEM_KALMAN_ACCEL, AltosLib.MISSING, 1/16.0);
291                 kalman_speed = map.get_double(AO_TELEM_KALMAN_SPEED, AltosLib.MISSING, 1/16.0);
292                 kalman_height = map.get_int(AO_TELEM_KALMAN_HEIGHT, AltosLib.MISSING);
293
294                 flight_accel = map.get_int(AO_TELEM_ADHOC_ACCEL, AltosLib.MISSING);
295                 flight_vel = map.get_int(AO_TELEM_ADHOC_SPEED, AltosLib.MISSING);
296                 flight_pres = map.get_int(AO_TELEM_ADHOC_BARO, AltosLib.MISSING);
297
298                 if (map.has(AO_TELEM_GPS_STATE))
299                         gps = new AltosGPS(map);
300                 else
301                         gps = null;
302         }
303
304         private void parse_legacy(String[] words, int i) throws ParseException {
305
306                 AltosParse.word (words[i++], "CALL");
307                 callsign = words[i++];
308
309                 AltosParse.word (words[i++], "SERIAL");
310                 serial = AltosParse.parse_int(words[i++]);
311
312                 if (version >= 2) {
313                         AltosParse.word (words[i++], "FLIGHT");
314                         flight = AltosParse.parse_int(words[i++]);
315                 } else
316                         flight = 0;
317
318                 AltosParse.word(words[i++], "RSSI");
319                 rssi = AltosParse.parse_int(words[i++]);
320
321                 /* Older telemetry data had mis-computed RSSI value */
322                 if (version <= 2)
323                         rssi = (rssi + 74) / 2 - 74;
324
325                 AltosParse.word(words[i++], "STATUS");
326                 status = AltosParse.parse_hex(words[i++]);
327
328                 AltosParse.word(words[i++], "STATE");
329                 state = AltosLib.state(words[i++]);
330
331                 tick = AltosParse.parse_int(words[i++]);
332
333                 AltosParse.word(words[i++], "a:");
334                 accel = AltosParse.parse_int(words[i++]);
335
336                 AltosParse.word(words[i++], "p:");
337                 pres = AltosParse.parse_int(words[i++]);
338
339                 AltosParse.word(words[i++], "t:");
340                 temp = AltosParse.parse_int(words[i++]);
341
342                 AltosParse.word(words[i++], "v:");
343                 batt = AltosParse.parse_int(words[i++]);
344
345                 AltosParse.word(words[i++], "d:");
346                 apogee = AltosParse.parse_int(words[i++]);
347
348                 AltosParse.word(words[i++], "m:");
349                 main = AltosParse.parse_int(words[i++]);
350
351                 AltosParse.word(words[i++], "fa:");
352                 flight_accel = AltosParse.parse_int(words[i++]);
353
354                 AltosParse.word(words[i++], "ga:");
355                 ground_accel = AltosParse.parse_int(words[i++]);
356
357                 AltosParse.word(words[i++], "fv:");
358                 flight_vel = AltosParse.parse_int(words[i++]);
359
360                 AltosParse.word(words[i++], "fp:");
361                 flight_pres = AltosParse.parse_int(words[i++]);
362
363                 /* Old TeleDongle code with kalman-reporting TeleMetrum code */
364                 if ((flight_vel & 0xffff0000) == 0x80000000) {
365                         kalman_speed = ((short) flight_vel) / 16.0;
366                         kalman_acceleration = flight_accel / 16.0;
367                         kalman_height = flight_pres;
368                         flight_vel = AltosLib.MISSING;
369                         flight_pres = AltosLib.MISSING;
370                         flight_accel = AltosLib.MISSING;
371                 } else {
372                         kalman_speed = AltosLib.MISSING;
373                         kalman_acceleration = AltosLib.MISSING;
374                         kalman_height = AltosLib.MISSING;
375                 }
376
377                 AltosParse.word(words[i++], "gp:");
378                 ground_pres = AltosParse.parse_int(words[i++]);
379
380                 if (version >= 1) {
381                         AltosParse.word(words[i++], "a+:");
382                         accel_plus_g = AltosParse.parse_int(words[i++]);
383
384                         AltosParse.word(words[i++], "a-:");
385                         accel_minus_g = AltosParse.parse_int(words[i++]);
386                 } else {
387                         accel_plus_g = ground_accel;
388                         accel_minus_g = ground_accel + 530;
389                 }
390
391                 gps = new AltosGPS(words, i, version);
392                 gps_sequence++;
393         }
394
395         public AltosTelemetryLegacy(String line) throws ParseException, AltosCRCException {
396                 String[] words = line.split("\\s+");
397                 int     i = 0;
398
399                 if (words[i].equals("CRC") && words[i+1].equals("INVALID")) {
400                         i += 2;
401                         AltosParse.word(words[i++], "RSSI");
402                         rssi = AltosParse.parse_int(words[i++]);
403                         throw new AltosCRCException(rssi);
404                 }
405                 if (words[i].equals("CALL")) {
406                         version = 0;
407                 } else {
408                         AltosParse.word (words[i++], "VERSION");
409                         version = AltosParse.parse_int(words[i++]);
410                 }
411
412                 if (version < 4)
413                         parse_legacy(words, i);
414                 else
415                         parse_v4(words, i);
416         }
417
418         /*
419          * Given a hex dump of a legacy telemetry line, construct an AltosRecordTM from that
420          */
421
422         int[]   bytes;
423         int     adjust;
424
425         /*
426         private int int8(int i) {
427                 return AltosLib.int8(bytes, i + 1 + adjust);
428         }
429         */
430         private int uint8(int i) {
431                 return AltosLib.uint8(bytes, i + 1 + adjust);
432         }
433         private int int16(int i) {
434                 return AltosLib.int16(bytes, i + 1 + adjust);
435         }
436         private int uint16(int i) {
437                 return AltosLib.uint16(bytes, i + 1 + adjust);
438         }
439         private int uint32(int i) {
440                 return AltosLib.uint32(bytes, i + 1 + adjust);
441         }
442         private String string(int i, int l) {
443                 return AltosLib.string(bytes, i + 1 + adjust, l);
444         }
445
446         static final int AO_GPS_NUM_SAT_MASK    = (0xf << 0);
447         static final int AO_GPS_NUM_SAT_SHIFT   = (0);
448
449         static final int AO_GPS_VALID           = (1 << 4);
450         static final int AO_GPS_RUNNING         = (1 << 5);
451         static final int AO_GPS_DATE_VALID      = (1 << 6);
452         static final int AO_GPS_COURSE_VALID    = (1 << 7);
453
454         public AltosTelemetryLegacy(int[] in_bytes) {
455                 bytes = in_bytes;
456                 version = 4;
457                 adjust = 0;
458
459                 if (bytes.length == AltosLib.ao_telemetry_0_8_len + 4) {
460                         serial = uint8(0);
461                         adjust = -1;
462                 } else
463                         serial = uint16(0);
464
465                 callsign = string(62, 8);
466                 flight = uint16(2);
467                 state = uint8(4);
468                 tick = uint16(21);
469                 accel = int16(23);
470                 pres = int16(25);
471                 temp = int16(27);
472                 batt = int16(29);
473                 apogee = int16(31);
474                 main = int16(33);
475
476                 ground_accel = int16(7);
477                 ground_pres = int16(15);
478                 accel_plus_g = int16(17);
479                 accel_minus_g = int16(19);
480
481                 if (uint16(11) == 0x8000) {
482                         kalman_acceleration = int16(5);
483                         kalman_speed = int16(9);
484                         kalman_height = int16(13);
485                         flight_accel = AltosLib.MISSING;
486                         flight_vel = AltosLib.MISSING;
487                         flight_pres = AltosLib.MISSING;
488                 } else {
489                         flight_accel = int16(5);
490                         flight_vel = uint32(9);
491                         flight_pres = int16(13);
492                         kalman_acceleration = AltosLib.MISSING;
493                         kalman_speed = AltosLib.MISSING;
494                         kalman_height = AltosLib.MISSING;
495                 }
496
497                 gps = null;
498
499                 int gps_flags = uint8(41);
500
501                 if ((gps_flags & (AO_GPS_VALID|AO_GPS_RUNNING)) != 0) {
502                         gps = new AltosGPS();
503                         gps_sequence++;
504
505                         gps.nsat = (gps_flags & AO_GPS_NUM_SAT_MASK);
506                         gps.locked = (gps_flags & AO_GPS_VALID) != 0;
507                         gps.connected = true;
508                         gps.lat = uint32(42) / 1.0e7;
509                         gps.lon = uint32(46) / 1.0e7;
510                         gps.alt = int16(50);
511                         gps.ground_speed = uint16(52) / 100.0;
512                         gps.course = uint8(54) * 2;
513                         gps.hdop = uint8(55) / 5.0;
514                         gps.h_error = uint16(58);
515                         gps.v_error = uint16(60);
516
517                         int     n_tracking_reported = uint8(70);
518                         if (n_tracking_reported > 12)
519                                 n_tracking_reported = 12;
520                         int     n_tracking_actual = 0;
521                         for (int i = 0; i < n_tracking_reported; i++) {
522                                 if (uint8(71 + i*2) != 0)
523                                         n_tracking_actual++;
524                         }
525                         if (n_tracking_actual > 0) {
526                                 gps.cc_gps_sat = new AltosGPSSat[n_tracking_actual];
527
528                                 n_tracking_actual = 0;
529                                 for (int i = 0; i < n_tracking_reported; i++) {
530                                         int     svid = uint8(71 + i*2);
531                                         int     c_n0 = uint8(72 + i*2);
532                                         if (svid != 0)
533                                                 gps.cc_gps_sat[n_tracking_actual++] = new AltosGPSSat(svid, c_n0);
534                                 }
535                         }
536                 }
537         }
538
539         public void update_state(AltosState state) {
540                 state.set_tick(tick);
541                 state.set_state(this.state);
542                 state.set_flight(flight);
543                 state.set_serial(serial);
544                 state.set_rssi(rssi, status);
545
546                 state.set_pressure(AltosConvert.barometer_to_pressure(pres));
547                 state.set_accel_g(accel_plus_g, accel_minus_g);
548                 state.set_accel(accel);
549                 if (kalman_height != AltosLib.MISSING)
550                         state.set_kalman(kalman_height, kalman_speed, kalman_acceleration);
551                 state.set_temperature(AltosConvert.thermometer_to_temperature(temp));
552                 state.set_battery_voltage(AltosConvert.cc_battery_to_voltage(batt));
553                 state.set_apogee_voltage(AltosConvert.cc_ignitor_to_voltage(apogee));
554                 state.set_main_voltage(AltosConvert.cc_ignitor_to_voltage(main));
555                 if (gps != null)
556                         state.set_gps(gps, gps_sequence);
557         }
558 }