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_14;
22 import java.util.concurrent.*;
26 public class AltosGPS implements Cloneable {
28 public final static int MISSING = AltosLib.MISSING;
31 public boolean locked;
32 public boolean connected;
33 public double lat; /* degrees (+N -S) */
34 public double lon; /* degrees (+E -W) */
35 public double alt; /* m */
43 public double ground_speed; /* m/s */
44 public int course; /* degrees */
45 public double climb_rate; /* m/s */
46 public double pdop; /* unitless */
47 public double hdop; /* unitless */
48 public double vdop; /* unitless */
49 public double h_error; /* m */
50 public double v_error; /* m */
52 public AltosGPSSat[] cc_gps_sat; /* tracking data */
54 public void ParseGPSDate(String date) throws ParseException {
55 String[] ymd = date.split("-");
57 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
58 year = AltosParse.parse_int(ymd[0]);
59 month = AltosParse.parse_int(ymd[1]);
60 day = AltosParse.parse_int(ymd[2]);
63 public void ParseGPSTime(String time) throws ParseException {
64 String[] hms = time.split(":");
66 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
67 hour = AltosParse.parse_int(hms[0]);
68 minute = AltosParse.parse_int(hms[1]);
69 second = AltosParse.parse_int(hms[2]);
72 public void ClearGPSTime() {
73 year = month = day = AltosLib.MISSING;
74 hour = minute = second = AltosLib.MISSING;
77 /* Return time since epoc in seconds */
78 public long seconds() {
79 if (year == AltosLib.MISSING)
80 return AltosLib.MISSING;
81 if (month == AltosLib.MISSING)
82 return AltosLib.MISSING;
83 if (day == AltosLib.MISSING)
84 return AltosLib.MISSING;
85 if (hour == AltosLib.MISSING)
86 return AltosLib.MISSING;
87 if (minute == AltosLib.MISSING)
88 return AltosLib.MISSING;
89 if (second == AltosLib.MISSING)
90 return AltosLib.MISSING;
91 OffsetDateTime odt = OffsetDateTime.of(year, month, day, hour, minute, second, 0, ZoneOffset.UTC);
92 return odt.toEpochSecond();
95 public AltosLatLon lat_lon() {
96 return new AltosLatLon(lat, lon);
99 public AltosGPS(AltosTelemetryMap map) throws ParseException {
100 String state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
101 AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
103 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
104 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
107 lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
108 lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
109 alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
110 year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
113 month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
114 day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
116 hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
117 minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
118 second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
120 ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
121 AltosLib.MISSING, 1/100.0);
122 course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
124 pdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_PDOP, MISSING, 1.0);
125 hdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HDOP, MISSING, 1.0);
126 vdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_VDOP, MISSING, 1.0);
127 h_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HERROR, MISSING);
128 v_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_VERROR, MISSING);
129 } else if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_UNLOCKED)) {
138 public boolean parse_string (String line, boolean says_done) {
139 String[] bits = line.split("\\s+");
140 if (bits.length == 0)
142 if (line.startsWith("Date:")) {
145 String[] d = bits[1].split("/");
148 year = Integer.parseInt(d[0]) + 2000;
149 month = Integer.parseInt(d[1]);
150 day = Integer.parseInt(d[2]);
151 } else if (line.startsWith("Time:")) {
154 String[] d = bits[1].split(":");
157 hour = Integer.parseInt(d[0]);
158 minute = Integer.parseInt(d[1]);
159 second = Integer.parseInt(d[2]);
160 } else if (line.startsWith("Lat/Lon:")) {
163 lat = Integer.parseInt(bits[1]) * 1.0e-7;
164 lon = Integer.parseInt(bits[2]) * 1.0e-7;
165 } else if (line.startsWith("Alt:")) {
168 alt = Integer.parseInt(bits[1]);
169 } else if (line.startsWith("Flags:")) {
172 int status = Integer.decode(bits[1]);
173 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
174 locked = (status & AltosLib.AO_GPS_VALID) != 0;
177 } else if (line.startsWith("Sats:")) {
180 nsat = Integer.parseInt(bits[1]);
181 cc_gps_sat = new AltosGPSSat[nsat];
182 for (int i = 0; i < nsat; i++) {
183 int svid = Integer.parseInt(bits[2+i*2]);
184 int cc_n0 = Integer.parseInt(bits[3+i*2]);
185 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
187 } else if (line.startsWith("done")) {
194 public AltosGPS(String[] words, int i, int version) throws ParseException {
195 AltosParse.word(words[i++], "GPS");
196 nsat = AltosParse.parse_int(words[i++]);
197 AltosParse.word(words[i++], "sat");
204 if ((words[i]).equals("unlocked")) {
207 } else if ((words[i]).equals("not-connected")) {
209 } else if (words.length >= 40) {
214 ParseGPSDate(words[i++]);
216 year = month = day = 0;
217 ParseGPSTime(words[i++]);
218 lat = AltosParse.parse_coord(words[i++]);
219 lon = AltosParse.parse_coord(words[i++]);
220 alt = AltosParse.parse_int(words[i++]);
221 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
222 ground_speed = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(H)"));
223 course = AltosParse.parse_int(words[i++]);
224 climb_rate = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(V)"));
225 hdop = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "(hdop)"));
226 h_error = AltosParse.parse_int(words[i++]);
227 v_error = AltosParse.parse_int(words[i++]);
232 if (i < words.length) {
233 AltosParse.word(words[i++], "SAT");
234 int tracking_channels = 0;
235 if (words[i].equals("not-connected"))
236 tracking_channels = 0;
238 tracking_channels = AltosParse.parse_int(words[i]);
240 cc_gps_sat = new AltosGPSSat[tracking_channels];
241 for (int chan = 0; chan < tracking_channels; chan++) {
242 cc_gps_sat[chan] = new AltosGPSSat();
243 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
244 /* Older versions included SiRF status bits */
247 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
250 cc_gps_sat = new AltosGPSSat[0];
253 public void set_latitude(int in_lat) {
254 lat = in_lat / 10.0e7;
257 public void set_longitude(int in_lon) {
258 lon = in_lon / 10.0e7;
261 public void set_time(int in_hour, int in_minute, int in_second) {
267 public void set_date(int in_year, int in_month, int in_day) {
274 public void set_flags(int in_flags) {
279 public void set_altitude(int in_altitude) {
283 public void add_sat(int svid, int c_n0) {
284 if (cc_gps_sat == null) {
285 cc_gps_sat = new AltosGPSSat[1];
287 AltosGPSSat[] new_gps_sat = new AltosGPSSat[cc_gps_sat.length + 1];
288 for (int i = 0; i < cc_gps_sat.length; i++)
289 new_gps_sat[i] = cc_gps_sat[i];
290 cc_gps_sat = new_gps_sat;
292 AltosGPSSat sat = new AltosGPSSat();
295 cc_gps_sat[cc_gps_sat.length - 1] = sat;
298 private void init() {
299 lat = AltosLib.MISSING;
300 lon = AltosLib.MISSING;
301 alt = AltosLib.MISSING;
302 ground_speed = AltosLib.MISSING;
303 course = AltosLib.MISSING;
304 climb_rate = AltosLib.MISSING;
305 pdop = AltosLib.MISSING;
306 hdop = AltosLib.MISSING;
307 vdop = AltosLib.MISSING;
308 h_error = AltosLib.MISSING;
309 v_error = AltosLib.MISSING;
318 public AltosGPS clone() {
319 AltosGPS g = new AltosGPS();
323 g.connected = connected;
324 g.lat = lat; /* degrees (+N -S) */
325 g.lon = lon; /* degrees (+E -W) */
334 g.ground_speed = ground_speed; /* m/s */
335 g.course = course; /* degrees */
336 g.climb_rate = climb_rate; /* m/s */
337 g.pdop = pdop; /* unitless */
338 g.hdop = hdop; /* unitless */
339 g.vdop = vdop; /* unitless */
340 g.h_error = h_error; /* m */
341 g.v_error = v_error; /* m */
343 if (cc_gps_sat != null) {
344 g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
345 for (int i = 0; i < cc_gps_sat.length; i++) {
346 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
353 public AltosGPS(AltosGPS old) {
357 connected = old.connected;
358 lat = old.lat; /* degrees (+N -S) */
359 lon = old.lon; /* degrees (+E -W) */
360 alt = old.alt; /* m */
368 ground_speed = old.ground_speed; /* m/s */
369 course = old.course; /* degrees */
370 climb_rate = old.climb_rate; /* m/s */
371 pdop = old.pdop; /* unitless? */
372 hdop = old.hdop; /* unitless? */
373 vdop = old.vdop; /* unitless? */
374 h_error = old.h_error; /* m */
375 v_error = old.v_error; /* m */
377 if (old.cc_gps_sat != null) {
378 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
379 for (int i = 0; i < old.cc_gps_sat.length; i++) {
380 cc_gps_sat[i] = new AltosGPSSat();
381 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
382 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
390 static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
392 AltosGPS gps = new AltosGPS(link, link.config_data());
394 listener.set_gps(gps, true, true);
395 } catch (TimeoutException te) {
399 public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
400 boolean says_done = config_data.compare_version("1.0") >= 0;
404 String line = link.get_reply_no_dialog(5000);
406 throw new TimeoutException();
407 if (!parse_string(line, says_done))