2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
24 * Telemetry data contents
35 int parse_int(String v) throws ParseException {
37 return Integer.parseInt(v);
38 } catch (NumberFormatException e) {
39 throw new ParseException("error parsing GPS value " + v, 0);
43 public AltosGPSTime(String date, String time) throws ParseException {
44 String[] ymd = date.split("-");
46 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
47 year = parse_int(ymd[0]);
48 month = parse_int(ymd[1]);
49 day = parse_int(ymd[2]);
51 String[] hms = time.split(":");
53 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
54 hour = parse_int(hms[0]);
55 minute = parse_int(hms[1]);
56 second = parse_int(hms[2]);
59 public AltosGPSTime() {
60 year = month = day = 0;
61 hour = minute = second = 0;
69 AltosGPSTime gps_time;
70 double lat; /* degrees (+N -S) */
71 double lon; /* degrees (+E -W) */
74 int gps_extended; /* has extra data */
75 double ground_speed; /* m/s */
76 int course; /* degrees */
77 double climb_rate; /* m/s */
78 double hdop; /* unitless? */
89 class AltosGPSTracking {
91 AltosGPSSat[] cc_gps_sat;
95 * The telemetry data stream is a bit of a mess at present, with no consistent
96 * formatting. In particular, the GPS data is formatted for viewing instead of parsing.
97 * However, the key feature is that every telemetry line contains all of the information
98 * necessary to describe the current rocket state, including the calibration values
99 * for accelerometer and barometer.
103 * VERSION 2 CALL KB0G SERIAL 51 FLIGHT 2 RSSI -68 STATUS ff STATE pad 1001 \
104 * a: 16032 p: 21232 t: 20284 v: 25160 d: 204 m: 204 fa: 16038 ga: 16032 fv: 0 \
105 * fp: 21232 gp: 21230 a+: 16049 a-: 16304 GPS 0 sat unlocked SAT 1 15 30
109 * VERSION 2 CALL KB0G SERIAL 51 FLIGHT 2 RSSI -71 STATUS ff STATE pad 2504 \
110 * a: 16028 p: 21220 t: 20360 v: 25004 d: 208 m: 200 fa: 16031 ga: 16032 fv: 330 \
111 * fp: 21231 gp: 21230 a+: 16049 a-: 16304 \
112 * GPS 9 sat 2010-02-13 17:16:51 35°20.0803'N 106°45.2235'W 1790m \
113 * 0.00m/s(H) 0° 0.00m/s(V) 1.0(hdop) 0(herr) 0(verr) \
114 * SAT 10 29 30 24 28 5 25 21 20 15 33 1 23 30 24 18 26 10 29 2 26
117 public class AltosTelemetry {
140 AltosGPSTracking gps_tracking;
142 int parse_int(String v) throws ParseException {
144 return Integer.parseInt(v);
145 } catch (NumberFormatException e) {
146 throw new ParseException("error parsing int " + v, 0);
150 int parse_hex(String v) throws ParseException {
152 return Integer.parseInt(v, 16);
153 } catch (NumberFormatException e) {
154 throw new ParseException("error parsing hex " + v, 0);
158 double parse_double(String v) throws ParseException {
160 return Double.parseDouble(v);
161 } catch (NumberFormatException e) {
162 throw new ParseException("error parsing double " + v, 0);
166 double parse_coord(String coord) throws ParseException {
167 String[] dsf = coord.split("\\D+");
169 if (dsf.length != 3) {
170 throw new ParseException("error parsing coord " + coord, 0);
172 int deg = parse_int(dsf[0]);
173 int min = parse_int(dsf[1]);
174 int frac = parse_int(dsf[2]);
176 double r = deg + (min + frac / 10000.0) / 60.0;
177 if (coord.endsWith("S") || coord.endsWith("W"))
182 String strip_suffix(String v, String suffix) {
183 if (v.endsWith(suffix))
184 return v.substring(0, v.length() - suffix.length());
188 void word(String v, String m) throws ParseException {
190 throw new ParseException("error matching '" + v + "' '" + m + "'", 0);
194 public AltosTelemetry(String line) throws ParseException {
195 String[] words = line.split("\\s+");
199 word (words[i++], "VERSION");
200 version = parse_int(words[i++]);
202 word (words[i++], "CALL");
203 callsign = words[i++];
205 word (words[i++], "SERIAL");
206 serial = parse_int(words[i++]);
208 word (words[i++], "FLIGHT");
209 flight = parse_int(words[i++]);
211 word(words[i++], "RSSI");
212 rssi = parse_int(words[i++]);
214 word(words[i++], "STATUS");
215 status = parse_hex(words[i++]);
217 word(words[i++], "STATE");
220 tick = parse_int(words[i++]);
222 word(words[i++], "a:");
223 accel = parse_int(words[i++]);
225 word(words[i++], "p:");
226 pres = parse_int(words[i++]);
228 word(words[i++], "t:");
229 temp = parse_int(words[i++]);
231 word(words[i++], "v:");
232 batt = parse_int(words[i++]);
234 word(words[i++], "d:");
235 drogue = parse_int(words[i++]);
237 word(words[i++], "m:");
238 main = parse_int(words[i++]);
240 word(words[i++], "fa:");
241 flight_accel = parse_int(words[i++]);
243 word(words[i++], "ga:");
244 ground_accel = parse_int(words[i++]);
246 word(words[i++], "fv:");
247 flight_vel = parse_int(words[i++]);
249 word(words[i++], "fp:");
250 flight_pres = parse_int(words[i++]);
252 word(words[i++], "gp:");
253 ground_pres = parse_int(words[i++]);
255 word(words[i++], "a+:");
256 accel_plus_g = parse_int(words[i++]);
258 word(words[i++], "a-:");
259 accel_minus_g = parse_int(words[i++]);
261 word(words[i++], "GPS");
262 gps = new AltosGPS();
263 gps.nsat = parse_int(words[i++]);
264 word(words[i++], "sat");
266 gps.gps_connected = 0;
268 gps.lat = gps.lon = 0;
270 if ((words[i]).equals("unlocked")) {
271 gps.gps_connected = 1;
272 gps.gps_time = new AltosGPSTime();
274 } else if (words.length >= 40) {
276 gps.gps_connected = 1;
278 gps.gps_time = new AltosGPSTime(words[i], words[i+1]); i += 2;
279 gps.lat = parse_coord(words[i++]);
280 gps.lon = parse_coord(words[i++]);
281 gps.alt = parse_int(strip_suffix(words[i++], "m"));
282 gps.ground_speed = parse_double(strip_suffix(words[i++], "m/s(H)"));
283 gps.course = parse_int(strip_suffix(words[i++], "°"));
284 gps.climb_rate = parse_double(strip_suffix(words[i++], "m/s(V)"));
285 gps.hdop = parse_double(strip_suffix(words[i++], "(hdop)"));
286 gps.h_error = parse_int(strip_suffix(words[i++], "(herr)"));
287 gps.v_error = parse_int(strip_suffix(words[i++], "(verr)"));
289 gps.gps_time = new AltosGPSTime();
291 word(words[i++], "SAT");
292 gps_tracking = new AltosGPSTracking();
293 gps_tracking.channels = parse_int(words[i++]);
294 gps_tracking.cc_gps_sat = new AltosGPSSat[gps_tracking.channels];
295 for (int chan = 0; chan < gps_tracking.channels; chan++) {
296 gps_tracking.cc_gps_sat[chan] = new AltosGPSSat();
297 gps_tracking.cc_gps_sat[chan].svid = parse_int(words[i++]);
298 gps_tracking.cc_gps_sat[chan].c_n0 = parse_int(words[i++]);