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