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_2;
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 hdop; /* unitless */
44 public double vdop; /* unitless */
45 public int h_error; /* m */
46 public int v_error; /* m */
48 public AltosGPSSat[] cc_gps_sat; /* tracking data */
50 public void ParseGPSDate(String date) throws ParseException {
51 String[] ymd = date.split("-");
53 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
54 year = AltosParse.parse_int(ymd[0]);
55 month = AltosParse.parse_int(ymd[1]);
56 day = AltosParse.parse_int(ymd[2]);
59 public void ParseGPSTime(String time) throws ParseException {
60 String[] hms = time.split(":");
62 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
63 hour = AltosParse.parse_int(hms[0]);
64 minute = AltosParse.parse_int(hms[1]);
65 second = AltosParse.parse_int(hms[2]);
68 public void ClearGPSTime() {
69 year = month = day = AltosLib.MISSING;
70 hour = minute = second = AltosLib.MISSING;
73 public AltosGPS(AltosTelemetryMap map) throws ParseException {
74 String state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
75 AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
77 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
78 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
81 lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
82 lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
83 alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
84 year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
87 month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
88 day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
90 hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
91 minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
92 second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
94 ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
95 AltosLib.MISSING, 1/100.0);
96 course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
98 hdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HDOP, MISSING, 1.0);
99 vdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_VDOP, MISSING, 1.0);
100 h_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HERROR, MISSING);
101 v_error = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_VERROR, MISSING);
102 } else if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_UNLOCKED)) {
111 public boolean parse_string (String line, boolean says_done) {
112 String[] bits = line.split("\\s+");
113 if (bits.length == 0)
115 if (line.startsWith("Date:")) {
118 String[] d = bits[1].split("/");
121 year = Integer.parseInt(d[0]) + 2000;
122 month = Integer.parseInt(d[1]);
123 day = Integer.parseInt(d[2]);
124 } else if (line.startsWith("Time:")) {
127 String[] d = bits[1].split(":");
130 hour = Integer.parseInt(d[0]);
131 minute = Integer.parseInt(d[1]);
132 second = Integer.parseInt(d[2]);
133 } else if (line.startsWith("Lat/Lon:")) {
136 lat = Integer.parseInt(bits[1]) * 1.0e-7;
137 lon = Integer.parseInt(bits[2]) * 1.0e-7;
138 } else if (line.startsWith("Alt:")) {
141 alt = Integer.parseInt(bits[1]);
142 } else if (line.startsWith("Flags:")) {
145 int status = Integer.decode(bits[1]);
146 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
147 locked = (status & AltosLib.AO_GPS_VALID) != 0;
150 } else if (line.startsWith("Sats:")) {
153 nsat = Integer.parseInt(bits[1]);
154 cc_gps_sat = new AltosGPSSat[nsat];
155 for (int i = 0; i < nsat; i++) {
156 int svid = Integer.parseInt(bits[2+i*2]);
157 int cc_n0 = Integer.parseInt(bits[3+i*2]);
158 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
160 } else if (line.startsWith("done")) {
167 public AltosGPS(String[] words, int i, int version) throws ParseException {
168 AltosParse.word(words[i++], "GPS");
169 nsat = AltosParse.parse_int(words[i++]);
170 AltosParse.word(words[i++], "sat");
177 if ((words[i]).equals("unlocked")) {
180 } else if ((words[i]).equals("not-connected")) {
182 } else if (words.length >= 40) {
187 ParseGPSDate(words[i++]);
189 year = month = day = 0;
190 ParseGPSTime(words[i++]);
191 lat = AltosParse.parse_coord(words[i++]);
192 lon = AltosParse.parse_coord(words[i++]);
193 alt = AltosParse.parse_int(words[i++]);
194 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
195 ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
196 course = AltosParse.parse_int(words[i++]);
197 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
198 hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
199 h_error = AltosParse.parse_int(words[i++]);
200 v_error = AltosParse.parse_int(words[i++]);
205 if (i < words.length) {
206 AltosParse.word(words[i++], "SAT");
207 int tracking_channels = 0;
208 if (words[i].equals("not-connected"))
209 tracking_channels = 0;
211 tracking_channels = AltosParse.parse_int(words[i]);
213 cc_gps_sat = new AltosGPSSat[tracking_channels];
214 for (int chan = 0; chan < tracking_channels; chan++) {
215 cc_gps_sat[chan] = new AltosGPSSat();
216 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
217 /* Older versions included SiRF status bits */
220 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
223 cc_gps_sat = new AltosGPSSat[0];
226 public void set_latitude(int in_lat) {
227 lat = in_lat / 10.0e7;
230 public void set_longitude(int in_lon) {
231 lon = in_lon / 10.0e7;
234 public void set_time(int in_hour, int in_minute, int in_second) {
240 public void set_date(int in_year, int in_month, int in_day) {
247 public void set_flags(int in_flags) {
252 public void set_altitude(int in_altitude) {
256 public void add_sat(int svid, int c_n0) {
257 if (cc_gps_sat == null) {
258 cc_gps_sat = new AltosGPSSat[1];
260 AltosGPSSat[] new_gps_sat = new AltosGPSSat[cc_gps_sat.length + 1];
261 for (int i = 0; i < cc_gps_sat.length; i++)
262 new_gps_sat[i] = cc_gps_sat[i];
263 cc_gps_sat = new_gps_sat;
265 AltosGPSSat sat = new AltosGPSSat();
268 cc_gps_sat[cc_gps_sat.length - 1] = sat;
272 lat = AltosLib.MISSING;
273 lon = AltosLib.MISSING;
274 alt = AltosLib.MISSING;
279 public AltosGPS clone() {
280 AltosGPS g = new AltosGPS();
284 g.connected = connected;
285 g.lat = lat; /* degrees (+N -S) */
286 g.lon = lon; /* degrees (+E -W) */
295 g.ground_speed = ground_speed; /* m/s */
296 g.course = course; /* degrees */
297 g.climb_rate = climb_rate; /* m/s */
298 g.hdop = hdop; /* unitless? */
299 g.h_error = h_error; /* m */
300 g.v_error = v_error; /* m */
302 if (cc_gps_sat != null) {
303 g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
304 for (int i = 0; i < cc_gps_sat.length; i++) {
305 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
312 public AltosGPS(AltosGPS old) {
316 connected = old.connected;
317 lat = old.lat; /* degrees (+N -S) */
318 lon = old.lon; /* degrees (+E -W) */
319 alt = old.alt; /* m */
327 ground_speed = old.ground_speed; /* m/s */
328 course = old.course; /* degrees */
329 climb_rate = old.climb_rate; /* m/s */
330 hdop = old.hdop; /* unitless? */
331 h_error = old.h_error; /* m */
332 v_error = old.v_error; /* m */
334 if (old.cc_gps_sat != null) {
335 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
336 for (int i = 0; i < old.cc_gps_sat.length; i++) {
337 cc_gps_sat[i] = new AltosGPSSat();
338 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
339 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
343 lat = AltosLib.MISSING;
344 lon = AltosLib.MISSING;
345 alt = AltosLib.MISSING;
351 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
353 AltosGPS gps = new AltosGPS(link, config_data);
356 state.set_gps(gps, state.gps_sequence++);
359 } catch (TimeoutException te) {
361 state.set_gps(null, 0);
364 public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
365 boolean says_done = config_data.compare_version("1.0") >= 0;
368 String line = link.get_reply_no_dialog(5000);
370 throw new TimeoutException();
371 if (!parse_string(line, says_done))