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;
23 public class AltosGPS {
25 public final static int MISSING = AltosRecord.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 int 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 = 0;
70 hour = minute = second = 0;
73 public AltosGPS(AltosTelemetryMap map) throws ParseException {
74 String state = map.get_string(AltosTelemetry.AO_TELEM_GPS_STATE,
75 AltosTelemetry.AO_TELEM_GPS_STATE_ERROR);
77 nsat = map.get_int(AltosTelemetry.AO_TELEM_GPS_NUM_SAT, 0);
78 if (state.equals(AltosTelemetry.AO_TELEM_GPS_STATE_LOCKED)) {
81 lat = map.get_double(AltosTelemetry.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
82 lon = map.get_double(AltosTelemetry.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
83 alt = map.get_int(AltosTelemetry.AO_TELEM_GPS_ALTITUDE, MISSING);
84 year = map.get_int(AltosTelemetry.AO_TELEM_GPS_YEAR, MISSING);
87 month = map.get_int(AltosTelemetry.AO_TELEM_GPS_MONTH, MISSING);
88 day = map.get_int(AltosTelemetry.AO_TELEM_GPS_DAY, MISSING);
90 hour = map.get_int(AltosTelemetry.AO_TELEM_GPS_HOUR, 0);
91 minute = map.get_int(AltosTelemetry.AO_TELEM_GPS_MINUTE, 0);
92 second = map.get_int(AltosTelemetry.AO_TELEM_GPS_SECOND, 0);
94 ground_speed = map.get_double(AltosTelemetry.AO_TELEM_GPS_HORIZONTAL_SPEED,
95 AltosRecord.MISSING, 1/100.0);
96 course = map.get_int(AltosTelemetry.AO_TELEM_GPS_COURSE,
98 hdop = map.get_double(AltosTelemetry.AO_TELEM_GPS_HDOP, MISSING, 1.0);
99 vdop = map.get_double(AltosTelemetry.AO_TELEM_GPS_VDOP, MISSING, 1.0);
100 h_error = map.get_int(AltosTelemetry.AO_TELEM_GPS_HERROR, MISSING);
101 v_error = map.get_int(AltosTelemetry.AO_TELEM_GPS_VERROR, MISSING);
102 } else if (state.equals(AltosTelemetry.AO_TELEM_GPS_STATE_UNLOCKED)) {
111 public AltosGPS(String[] words, int i, int version) throws ParseException {
112 AltosParse.word(words[i++], "GPS");
113 nsat = AltosParse.parse_int(words[i++]);
114 AltosParse.word(words[i++], "sat");
121 if ((words[i]).equals("unlocked")) {
124 } else if ((words[i]).equals("not-connected")) {
126 } else if (words.length >= 40) {
131 ParseGPSDate(words[i++]);
133 year = month = day = 0;
134 ParseGPSTime(words[i++]);
135 lat = AltosParse.parse_coord(words[i++]);
136 lon = AltosParse.parse_coord(words[i++]);
137 alt = AltosParse.parse_int(words[i++]);
138 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
139 ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
140 course = AltosParse.parse_int(words[i++]);
141 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
142 hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
143 h_error = AltosParse.parse_int(words[i++]);
144 v_error = AltosParse.parse_int(words[i++]);
149 if (i < words.length) {
150 AltosParse.word(words[i++], "SAT");
151 int tracking_channels = 0;
152 if (words[i].equals("not-connected"))
153 tracking_channels = 0;
155 tracking_channels = AltosParse.parse_int(words[i]);
157 cc_gps_sat = new AltosGPSSat[tracking_channels];
158 for (int chan = 0; chan < tracking_channels; chan++) {
159 cc_gps_sat[chan] = new AltosGPSSat();
160 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
161 /* Older versions included SiRF status bits */
164 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
167 cc_gps_sat = new AltosGPSSat[0];
170 public void set_latitude(int in_lat) {
171 lat = in_lat / 10.0e7;
174 public void set_longitude(int in_lon) {
175 lon = in_lon / 10.0e7;
178 public void set_time(int hour, int minute, int second) {
184 public void set_date(int year, int month, int day) {
190 public void set_flags(int flags) {
194 public void set_altitude(int altitude) {
198 public void add_sat(int svid, int c_n0) {
199 if (cc_gps_sat == null) {
200 cc_gps_sat = new AltosGPSSat[1];
202 AltosGPSSat[] new_gps_sat = new AltosGPSSat[cc_gps_sat.length + 1];
203 for (int i = 0; i < cc_gps_sat.length; i++)
204 new_gps_sat[i] = cc_gps_sat[i];
205 cc_gps_sat = new_gps_sat;
207 AltosGPSSat sat = new AltosGPSSat();
210 cc_gps_sat[cc_gps_sat.length - 1] = sat;
218 public AltosGPS(AltosGPS old) {
221 connected = old.connected;
222 lat = old.lat; /* degrees (+N -S) */
223 lon = old.lon; /* degrees (+E -W) */
224 alt = old.alt; /* m */
232 ground_speed = old.ground_speed; /* m/s */
233 course = old.course; /* degrees */
234 climb_rate = old.climb_rate; /* m/s */
235 hdop = old.hdop; /* unitless? */
236 h_error = old.h_error; /* m */
237 v_error = old.v_error; /* m */
239 if (old.cc_gps_sat != null) {
240 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
241 for (int i = 0; i < old.cc_gps_sat.length; i++) {
242 cc_gps_sat[i] = new AltosGPSSat();
243 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
244 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;