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.
18 package org.altusmetrum.altoslib_5;
21 import java.util.concurrent.*;
23 public class AltosGPS implements Cloneable {
25 public final static int MISSING = AltosLib.MISSING;
28 public boolean locked;
29 public boolean connected;
30 public double lat; /* degrees (+N -S) */
31 public double lon; /* degrees (+E -W) */
32 public double alt; /* m */
40 public double ground_speed; /* m/s */
41 public int course; /* degrees */
42 public double climb_rate; /* m/s */
43 public double pdop; /* unitless */
44 public double hdop; /* unitless */
45 public double vdop; /* unitless */
46 public double h_error; /* m */
47 public double v_error; /* m */
49 public AltosGPSSat[] cc_gps_sat; /* tracking data */
51 public void ParseGPSDate(String date) throws ParseException {
52 String[] ymd = date.split("-");
54 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
55 year = AltosParse.parse_int(ymd[0]);
56 month = AltosParse.parse_int(ymd[1]);
57 day = AltosParse.parse_int(ymd[2]);
60 public void ParseGPSTime(String time) throws ParseException {
61 String[] hms = time.split(":");
63 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
64 hour = AltosParse.parse_int(hms[0]);
65 minute = AltosParse.parse_int(hms[1]);
66 second = AltosParse.parse_int(hms[2]);
69 public void ClearGPSTime() {
70 year = month = day = AltosLib.MISSING;
71 hour = minute = second = AltosLib.MISSING;
74 public AltosGPS(AltosTelemetryMap map) throws ParseException {
75 String state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
76 AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
78 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
79 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
82 lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
83 lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
84 alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
85 year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
88 month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
89 day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
91 hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
92 minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
93 second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
95 ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
96 AltosLib.MISSING, 1/100.0);
97 course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
99 pdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_PDOP, MISSING, 1.0);
100 hdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HDOP, MISSING, 1.0);
101 vdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_VDOP, MISSING, 1.0);
102 h_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HERROR, MISSING);
103 v_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_VERROR, MISSING);
104 } else if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_UNLOCKED)) {
113 public boolean parse_string (String line, boolean says_done) {
114 String[] bits = line.split("\\s+");
115 if (bits.length == 0)
117 if (line.startsWith("Date:")) {
120 String[] d = bits[1].split("/");
123 year = Integer.parseInt(d[0]) + 2000;
124 month = Integer.parseInt(d[1]);
125 day = Integer.parseInt(d[2]);
126 } else if (line.startsWith("Time:")) {
129 String[] d = bits[1].split(":");
132 hour = Integer.parseInt(d[0]);
133 minute = Integer.parseInt(d[1]);
134 second = Integer.parseInt(d[2]);
135 } else if (line.startsWith("Lat/Lon:")) {
138 lat = Integer.parseInt(bits[1]) * 1.0e-7;
139 lon = Integer.parseInt(bits[2]) * 1.0e-7;
140 } else if (line.startsWith("Alt:")) {
143 alt = Integer.parseInt(bits[1]);
144 } else if (line.startsWith("Flags:")) {
147 int status = Integer.decode(bits[1]);
148 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
149 locked = (status & AltosLib.AO_GPS_VALID) != 0;
152 } else if (line.startsWith("Sats:")) {
155 nsat = Integer.parseInt(bits[1]);
156 cc_gps_sat = new AltosGPSSat[nsat];
157 for (int i = 0; i < nsat; i++) {
158 int svid = Integer.parseInt(bits[2+i*2]);
159 int cc_n0 = Integer.parseInt(bits[3+i*2]);
160 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
162 } else if (line.startsWith("done")) {
169 public AltosGPS(String[] words, int i, int version) throws ParseException {
170 AltosParse.word(words[i++], "GPS");
171 nsat = AltosParse.parse_int(words[i++]);
172 AltosParse.word(words[i++], "sat");
179 if ((words[i]).equals("unlocked")) {
182 } else if ((words[i]).equals("not-connected")) {
184 } else if (words.length >= 40) {
189 ParseGPSDate(words[i++]);
191 year = month = day = 0;
192 ParseGPSTime(words[i++]);
193 lat = AltosParse.parse_coord(words[i++]);
194 lon = AltosParse.parse_coord(words[i++]);
195 alt = AltosParse.parse_int(words[i++]);
196 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
197 ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
198 course = AltosParse.parse_int(words[i++]);
199 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
200 hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
201 h_error = AltosParse.parse_int(words[i++]);
202 v_error = AltosParse.parse_int(words[i++]);
207 if (i < words.length) {
208 AltosParse.word(words[i++], "SAT");
209 int tracking_channels = 0;
210 if (words[i].equals("not-connected"))
211 tracking_channels = 0;
213 tracking_channels = AltosParse.parse_int(words[i]);
215 cc_gps_sat = new AltosGPSSat[tracking_channels];
216 for (int chan = 0; chan < tracking_channels; chan++) {
217 cc_gps_sat[chan] = new AltosGPSSat();
218 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
219 /* Older versions included SiRF status bits */
222 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
225 cc_gps_sat = new AltosGPSSat[0];
228 public void set_latitude(int in_lat) {
229 lat = in_lat / 10.0e7;
232 public void set_longitude(int in_lon) {
233 lon = in_lon / 10.0e7;
236 public void set_time(int in_hour, int in_minute, int in_second) {
242 public void set_date(int in_year, int in_month, int in_day) {
249 public void set_flags(int in_flags) {
254 public void set_altitude(int in_altitude) {
258 public void add_sat(int svid, int c_n0) {
259 if (cc_gps_sat == null) {
260 cc_gps_sat = new AltosGPSSat[1];
262 AltosGPSSat[] new_gps_sat = new AltosGPSSat[cc_gps_sat.length + 1];
263 for (int i = 0; i < cc_gps_sat.length; i++)
264 new_gps_sat[i] = cc_gps_sat[i];
265 cc_gps_sat = new_gps_sat;
267 AltosGPSSat sat = new AltosGPSSat();
270 cc_gps_sat[cc_gps_sat.length - 1] = sat;
273 private void init() {
274 lat = AltosLib.MISSING;
275 lon = AltosLib.MISSING;
276 alt = AltosLib.MISSING;
277 ground_speed = AltosLib.MISSING;
278 course = AltosLib.MISSING;
279 climb_rate = AltosLib.MISSING;
280 pdop = AltosLib.MISSING;
281 hdop = AltosLib.MISSING;
282 vdop = AltosLib.MISSING;
283 h_error = AltosLib.MISSING;
284 v_error = AltosLib.MISSING;
293 public AltosGPS clone() {
294 AltosGPS g = new AltosGPS();
298 g.connected = connected;
299 g.lat = lat; /* degrees (+N -S) */
300 g.lon = lon; /* degrees (+E -W) */
309 g.ground_speed = ground_speed; /* m/s */
310 g.course = course; /* degrees */
311 g.climb_rate = climb_rate; /* m/s */
312 g.pdop = pdop; /* unitless */
313 g.hdop = hdop; /* unitless */
314 g.vdop = vdop; /* unitless */
315 g.h_error = h_error; /* m */
316 g.v_error = v_error; /* m */
318 if (cc_gps_sat != null) {
319 g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
320 for (int i = 0; i < cc_gps_sat.length; i++) {
321 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
328 public AltosGPS(AltosGPS old) {
332 connected = old.connected;
333 lat = old.lat; /* degrees (+N -S) */
334 lon = old.lon; /* degrees (+E -W) */
335 alt = old.alt; /* m */
343 ground_speed = old.ground_speed; /* m/s */
344 course = old.course; /* degrees */
345 climb_rate = old.climb_rate; /* m/s */
346 pdop = old.pdop; /* unitless? */
347 hdop = old.hdop; /* unitless? */
348 vdop = old.vdop; /* unitless? */
349 h_error = old.h_error; /* m */
350 v_error = old.v_error; /* m */
352 if (old.cc_gps_sat != null) {
353 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
354 for (int i = 0; i < old.cc_gps_sat.length; i++) {
355 cc_gps_sat[i] = new AltosGPSSat();
356 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
357 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
365 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
367 AltosGPS gps = new AltosGPS(link, config_data);
370 state.set_gps(gps, state.gps_sequence++);
373 } catch (TimeoutException te) {
375 state.set_gps(null, 0);
378 public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
379 boolean says_done = config_data.compare_version("1.0") >= 0;
382 String line = link.get_reply_no_dialog(5000);
384 throw new TimeoutException();
385 if (!parse_string(line, says_done))