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_4;
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 int state; /* for TeleGPS */
50 static final int AO_GPS_STATE_VALID = 0x80;
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 public AltosGPS(AltosTelemetryMap map) throws ParseException {
78 String state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
79 AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
81 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
82 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
85 lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
86 lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
87 alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
88 year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
91 month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
92 day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
94 hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
95 minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
96 second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
98 ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
99 AltosLib.MISSING, 1/100.0);
100 course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
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(AltosParse.strip_suffix(words[i++], "m/s(H)"));
200 course = AltosParse.parse_int(words[i++]);
201 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
202 hdop = AltosParse.parse_double(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;
276 lat = AltosLib.MISSING;
277 lon = AltosLib.MISSING;
278 alt = AltosLib.MISSING;
283 public AltosGPS clone() {
284 AltosGPS g = new AltosGPS();
288 g.connected = connected;
289 g.lat = lat; /* degrees (+N -S) */
290 g.lon = lon; /* degrees (+E -W) */
299 g.ground_speed = ground_speed; /* m/s */
300 g.course = course; /* degrees */
301 g.climb_rate = climb_rate; /* m/s */
302 g.hdop = hdop; /* unitless? */
303 g.h_error = h_error; /* m */
304 g.v_error = v_error; /* m */
307 if (cc_gps_sat != null) {
308 g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
309 for (int i = 0; i < cc_gps_sat.length; i++) {
310 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
317 public AltosGPS(AltosGPS old) {
321 connected = old.connected;
322 lat = old.lat; /* degrees (+N -S) */
323 lon = old.lon; /* degrees (+E -W) */
324 alt = old.alt; /* m */
332 ground_speed = old.ground_speed; /* m/s */
333 course = old.course; /* degrees */
334 climb_rate = old.climb_rate; /* m/s */
335 hdop = old.hdop; /* unitless? */
336 h_error = old.h_error; /* m */
337 v_error = old.v_error; /* m */
340 if (old.cc_gps_sat != null) {
341 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
342 for (int i = 0; i < old.cc_gps_sat.length; i++) {
343 cc_gps_sat[i] = new AltosGPSSat();
344 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
345 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
349 lat = AltosLib.MISSING;
350 lon = AltosLib.MISSING;
351 alt = AltosLib.MISSING;
358 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
360 AltosGPS gps = new AltosGPS(link, config_data);
363 state.set_gps(gps, state.gps_sequence++);
366 } catch (TimeoutException te) {
368 state.set_gps(null, 0);
371 public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
372 boolean says_done = config_data.compare_version("1.0") >= 0;
375 String line = link.get_reply_no_dialog(5000);
377 throw new TimeoutException();
378 if (!parse_string(line, says_done))