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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
22 import java.util.concurrent.*;
25 public class AltosGPS implements Cloneable {
27 public final static int MISSING = AltosLib.MISSING;
30 public boolean locked;
31 public boolean connected;
32 public double lat; /* degrees (+N -S) */
33 public double lon; /* degrees (+E -W) */
34 public double alt; /* m */
42 public double ground_speed; /* m/s */
43 public int course; /* degrees */
44 public double climb_rate; /* m/s */
45 public double pdop; /* unitless */
46 public double hdop; /* unitless */
47 public double vdop; /* unitless */
48 public double h_error; /* m */
49 public double v_error; /* m */
51 public AltosGPSSat[] cc_gps_sat; /* tracking data */
53 public void ParseGPSDate(String date) throws ParseException {
54 String[] ymd = date.split("-");
56 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
57 year = AltosParse.parse_int(ymd[0]);
58 month = AltosParse.parse_int(ymd[1]);
59 day = AltosParse.parse_int(ymd[2]);
62 public void ParseGPSTime(String time) throws ParseException {
63 String[] hms = time.split(":");
65 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
66 hour = AltosParse.parse_int(hms[0]);
67 minute = AltosParse.parse_int(hms[1]);
68 second = AltosParse.parse_int(hms[2]);
71 public void ClearGPSTime() {
72 year = month = day = AltosLib.MISSING;
73 hour = minute = second = AltosLib.MISSING;
76 public AltosGPS(AltosTelemetryMap map) throws ParseException {
77 String state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
78 AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
80 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
81 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
84 lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
85 lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
86 alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
87 year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
90 month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
91 day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
93 hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
94 minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
95 second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
97 ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
98 AltosLib.MISSING, 1/100.0);
99 course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
101 pdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_PDOP, MISSING, 1.0);
102 hdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HDOP, MISSING, 1.0);
103 vdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_VDOP, MISSING, 1.0);
104 h_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HERROR, MISSING);
105 v_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_VERROR, MISSING);
106 } else if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_UNLOCKED)) {
115 public boolean parse_string (String line, boolean says_done) {
116 String[] bits = line.split("\\s+");
117 if (bits.length == 0)
119 if (line.startsWith("Date:")) {
122 String[] d = bits[1].split("/");
125 year = Integer.parseInt(d[0]) + 2000;
126 month = Integer.parseInt(d[1]);
127 day = Integer.parseInt(d[2]);
128 } else if (line.startsWith("Time:")) {
131 String[] d = bits[1].split(":");
134 hour = Integer.parseInt(d[0]);
135 minute = Integer.parseInt(d[1]);
136 second = Integer.parseInt(d[2]);
137 } else if (line.startsWith("Lat/Lon:")) {
140 lat = Integer.parseInt(bits[1]) * 1.0e-7;
141 lon = Integer.parseInt(bits[2]) * 1.0e-7;
142 } else if (line.startsWith("Alt:")) {
145 alt = Integer.parseInt(bits[1]);
146 } else if (line.startsWith("Flags:")) {
149 int status = Integer.decode(bits[1]);
150 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
151 locked = (status & AltosLib.AO_GPS_VALID) != 0;
154 } else if (line.startsWith("Sats:")) {
157 nsat = Integer.parseInt(bits[1]);
158 cc_gps_sat = new AltosGPSSat[nsat];
159 for (int i = 0; i < nsat; i++) {
160 int svid = Integer.parseInt(bits[2+i*2]);
161 int cc_n0 = Integer.parseInt(bits[3+i*2]);
162 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
164 } else if (line.startsWith("done")) {
171 public AltosGPS(String[] words, int i, int version) throws ParseException {
172 AltosParse.word(words[i++], "GPS");
173 nsat = AltosParse.parse_int(words[i++]);
174 AltosParse.word(words[i++], "sat");
181 if ((words[i]).equals("unlocked")) {
184 } else if ((words[i]).equals("not-connected")) {
186 } else if (words.length >= 40) {
191 ParseGPSDate(words[i++]);
193 year = month = day = 0;
194 ParseGPSTime(words[i++]);
195 lat = AltosParse.parse_coord(words[i++]);
196 lon = AltosParse.parse_coord(words[i++]);
197 alt = AltosParse.parse_int(words[i++]);
198 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
199 ground_speed = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(H)"));
200 course = AltosParse.parse_int(words[i++]);
201 climb_rate = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(V)"));
202 hdop = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "(hdop)"));
203 h_error = AltosParse.parse_int(words[i++]);
204 v_error = AltosParse.parse_int(words[i++]);
209 if (i < words.length) {
210 AltosParse.word(words[i++], "SAT");
211 int tracking_channels = 0;
212 if (words[i].equals("not-connected"))
213 tracking_channels = 0;
215 tracking_channels = AltosParse.parse_int(words[i]);
217 cc_gps_sat = new AltosGPSSat[tracking_channels];
218 for (int chan = 0; chan < tracking_channels; chan++) {
219 cc_gps_sat[chan] = new AltosGPSSat();
220 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
221 /* Older versions included SiRF status bits */
224 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
227 cc_gps_sat = new AltosGPSSat[0];
230 public void set_latitude(int in_lat) {
231 lat = in_lat / 10.0e7;
234 public void set_longitude(int in_lon) {
235 lon = in_lon / 10.0e7;
238 public void set_time(int in_hour, int in_minute, int in_second) {
244 public void set_date(int in_year, int in_month, int in_day) {
251 public void set_flags(int in_flags) {
256 public void set_altitude(int in_altitude) {
260 public void add_sat(int svid, int c_n0) {
261 if (cc_gps_sat == null) {
262 cc_gps_sat = new AltosGPSSat[1];
264 AltosGPSSat[] new_gps_sat = new AltosGPSSat[cc_gps_sat.length + 1];
265 for (int i = 0; i < cc_gps_sat.length; i++)
266 new_gps_sat[i] = cc_gps_sat[i];
267 cc_gps_sat = new_gps_sat;
269 AltosGPSSat sat = new AltosGPSSat();
272 cc_gps_sat[cc_gps_sat.length - 1] = sat;
275 private void init() {
276 lat = AltosLib.MISSING;
277 lon = AltosLib.MISSING;
278 alt = AltosLib.MISSING;
279 ground_speed = AltosLib.MISSING;
280 course = AltosLib.MISSING;
281 climb_rate = AltosLib.MISSING;
282 pdop = AltosLib.MISSING;
283 hdop = AltosLib.MISSING;
284 vdop = AltosLib.MISSING;
285 h_error = AltosLib.MISSING;
286 v_error = AltosLib.MISSING;
295 public AltosGPS clone() {
296 AltosGPS g = new AltosGPS();
300 g.connected = connected;
301 g.lat = lat; /* degrees (+N -S) */
302 g.lon = lon; /* degrees (+E -W) */
311 g.ground_speed = ground_speed; /* m/s */
312 g.course = course; /* degrees */
313 g.climb_rate = climb_rate; /* m/s */
314 g.pdop = pdop; /* unitless */
315 g.hdop = hdop; /* unitless */
316 g.vdop = vdop; /* unitless */
317 g.h_error = h_error; /* m */
318 g.v_error = v_error; /* m */
320 if (cc_gps_sat != null) {
321 g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
322 for (int i = 0; i < cc_gps_sat.length; i++) {
323 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
330 public AltosGPS(AltosGPS old) {
334 connected = old.connected;
335 lat = old.lat; /* degrees (+N -S) */
336 lon = old.lon; /* degrees (+E -W) */
337 alt = old.alt; /* m */
345 ground_speed = old.ground_speed; /* m/s */
346 course = old.course; /* degrees */
347 climb_rate = old.climb_rate; /* m/s */
348 pdop = old.pdop; /* unitless? */
349 hdop = old.hdop; /* unitless? */
350 vdop = old.vdop; /* unitless? */
351 h_error = old.h_error; /* m */
352 v_error = old.v_error; /* m */
354 if (old.cc_gps_sat != null) {
355 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
356 for (int i = 0; i < old.cc_gps_sat.length; i++) {
357 cc_gps_sat[i] = new AltosGPSSat();
358 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
359 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
367 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
369 AltosGPS gps = new AltosGPS(link, config_data);
372 state.set_gps(gps, state.gps_sequence++);
375 } catch (TimeoutException te) {
377 state.set_gps(null, 0);
380 public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
381 boolean says_done = config_data.compare_version("1.0") >= 0;
385 String line = link.get_reply_no_dialog(5000);
387 throw new TimeoutException();
388 if (!parse_string(line, says_done))