Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosTelemetry.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 telemetry data stream is a bit of a mess at present, with no consistent
31  * formatting. In particular, the GPS data is formatted for viewing instead of parsing.
32  * However, the key feature is that every telemetry line contains all of the information
33  * necessary to describe the current rocket state, including the calibration values
34  * for accelerometer and barometer.
35  *
36  * GPS unlocked:
37  *
38  * VERSION 2 CALL KB0G SERIAL  51 FLIGHT     2 RSSI  -68 STATUS ff STATE     pad  1001 \
39  *    a: 16032 p: 21232 t: 20284 v: 25160 d:   204 m:   204 fa: 16038 ga: 16032 fv:       0 \
40  *    fp: 21232 gp: 21230 a+: 16049 a-: 16304 GPS  0 sat unlocked SAT 1   15  30
41  *
42  * GPS locked:
43  *
44  * VERSION 2 CALL KB0G SERIAL  51 FLIGHT     2 RSSI  -71 STATUS ff STATE     pad  2504 \
45  *     a: 16028 p: 21220 t: 20360 v: 25004 d:   208 m:   200 fa: 16031 ga: 16032 fv:     330 \
46  *     fp: 21231 gp: 21230 a+: 16049 a-: 16304 \
47  *     GPS  9 sat 2010-02-13 17:16:51 35°20.0803'N 106°45.2235'W  1790m  \
48  *     0.00m/s(H) 0°     0.00m/s(V) 1.0(hdop)     0(herr)     0(verr) \
49  *     SAT 10   29  30  24  28   5  25  21  20  15  33   1  23  30  24  18  26  10  29   2  26
50  */
51
52 public class AltosTelemetry extends AltosRecord {
53         public AltosTelemetry(String line) throws ParseException, AltosCRCException {
54                 String[] words = line.split("\\s+");
55                 int     i = 0;
56
57                 if (words[i].equals("CRC") && words[i+1].equals("INVALID")) {
58                         i += 2;
59                         AltosParse.word(words[i++], "RSSI");
60                         rssi = AltosParse.parse_int(words[i++]);
61                         throw new AltosCRCException(rssi);
62                 }
63                 if (words[i].equals("CALL")) {
64                         version = 0;
65                 } else {
66                         AltosParse.word (words[i++], "VERSION");
67                         version = AltosParse.parse_int(words[i++]);
68                 }
69
70                 AltosParse.word (words[i++], "CALL");
71                 callsign = words[i++];
72
73                 AltosParse.word (words[i++], "SERIAL");
74                 serial = AltosParse.parse_int(words[i++]);
75
76                 if (version >= 2) {
77                         AltosParse.word (words[i++], "FLIGHT");
78                         flight = AltosParse.parse_int(words[i++]);
79                 } else
80                         flight = 0;
81
82                 AltosParse.word(words[i++], "RSSI");
83                 rssi = AltosParse.parse_int(words[i++]);
84
85                 /* Older telemetry data had mis-computed RSSI value */
86                 if (version <= 2)
87                         rssi = (rssi + 74) / 2 - 74;
88
89                 AltosParse.word(words[i++], "STATUS");
90                 status = AltosParse.parse_hex(words[i++]);
91
92                 AltosParse.word(words[i++], "STATE");
93                 state = Altos.state(words[i++]);
94
95                 tick = AltosParse.parse_int(words[i++]);
96
97                 AltosParse.word(words[i++], "a:");
98                 accel = AltosParse.parse_int(words[i++]);
99
100                 AltosParse.word(words[i++], "p:");
101                 pres = AltosParse.parse_int(words[i++]);
102
103                 AltosParse.word(words[i++], "t:");
104                 temp = AltosParse.parse_int(words[i++]);
105
106                 AltosParse.word(words[i++], "v:");
107                 batt = AltosParse.parse_int(words[i++]);
108
109                 AltosParse.word(words[i++], "d:");
110                 drogue = AltosParse.parse_int(words[i++]);
111
112                 AltosParse.word(words[i++], "m:");
113                 main = AltosParse.parse_int(words[i++]);
114
115                 AltosParse.word(words[i++], "fa:");
116                 flight_accel = AltosParse.parse_int(words[i++]);
117
118                 AltosParse.word(words[i++], "ga:");
119                 ground_accel = AltosParse.parse_int(words[i++]);
120
121                 AltosParse.word(words[i++], "fv:");
122                 flight_vel = AltosParse.parse_int(words[i++]);
123
124                 AltosParse.word(words[i++], "fp:");
125                 flight_pres = AltosParse.parse_int(words[i++]);
126
127                 AltosParse.word(words[i++], "gp:");
128                 ground_pres = AltosParse.parse_int(words[i++]);
129
130                 if (version >= 1) {
131                         AltosParse.word(words[i++], "a+:");
132                         accel_plus_g = AltosParse.parse_int(words[i++]);
133
134                         AltosParse.word(words[i++], "a-:");
135                         accel_minus_g = AltosParse.parse_int(words[i++]);
136                 } else {
137                         accel_plus_g = ground_accel;
138                         accel_minus_g = ground_accel + 530;
139                 }
140
141                 gps = new AltosGPS(words, i, version);
142         }
143 }