Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosGPS.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.text.*;
22 import java.util.concurrent.*;
23 import java.io.*;
24
25 public class AltosGPS implements Cloneable {
26
27         public final static int MISSING = AltosLib.MISSING;
28
29         public int      nsat;
30         public boolean  locked;
31         public boolean  connected;
32         public double   lat;            /* degrees (+N -S) */
33         public double   lon;            /* degrees (+E -W) */
34         public double   alt;            /* m */
35         public int      year;
36         public int      month;
37         public int      day;
38         public int      hour;
39         public int      minute;
40         public int      second;
41
42         public double   ground_speed;   /* m/s */
43         public int      course;         /* degrees */
44         public double   climb_rate;     /* m/s */
45         public double   pdop;           /* unitless */
46         public double   hdop;           /* unitless */
47         public double   vdop;           /* unitless */
48         public double   h_error;        /* m */
49         public double   v_error;        /* m */
50
51         public AltosGPSSat[] cc_gps_sat;        /* tracking data */
52
53         public void ParseGPSDate(String date) throws ParseException {
54                 String[] ymd = date.split("-");
55                 if (ymd.length != 3)
56                         throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
57                 year = AltosParse.parse_int(ymd[0]);
58                 month = AltosParse.parse_int(ymd[1]);
59                 day = AltosParse.parse_int(ymd[2]);
60         }
61
62         public void ParseGPSTime(String time) throws ParseException {
63                 String[] hms = time.split(":");
64                 if (hms.length != 3)
65                         throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
66                 hour = AltosParse.parse_int(hms[0]);
67                 minute = AltosParse.parse_int(hms[1]);
68                 second = AltosParse.parse_int(hms[2]);
69         }
70
71         public void ClearGPSTime() {
72                 year = month = day = AltosLib.MISSING;
73                 hour = minute = second = AltosLib.MISSING;
74         }
75
76         public AltosGPS(AltosTelemetryMap map) throws ParseException {
77                 String  state = map.get_string(AltosTelemetryLegacy.AO_TELEM_GPS_STATE,
78                                                AltosTelemetryLegacy.AO_TELEM_GPS_STATE_ERROR);
79
80                 nsat = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_NUM_SAT, 0);
81                 if (state.equals(AltosTelemetryLegacy.AO_TELEM_GPS_STATE_LOCKED)) {
82                         connected = true;
83                         locked = true;
84                         lat = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LATITUDE, MISSING, 1.0e-7);
85                         lon = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_LONGITUDE, MISSING, 1.0e-7);
86                         alt = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_ALTITUDE, MISSING);
87                         year = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_YEAR, MISSING);
88                         if (year != MISSING)
89                                 year += 2000;
90                         month = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MONTH, MISSING);
91                         day = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_DAY, MISSING);
92
93                         hour = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_HOUR, 0);
94                         minute = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_MINUTE, 0);
95                         second = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_SECOND, 0);
96
97                         ground_speed = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_HORIZONTAL_SPEED,
98                                                       AltosLib.MISSING, 1/100.0);
99                         course = map.get_int(AltosTelemetryLegacy.AO_TELEM_GPS_COURSE,
100                                              AltosLib.MISSING);
101                         pdop = map.get_double(AltosTelemetryLegacy.AO_TELEM_GPS_PDOP, MISSING, 1.0);
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)) {
107                         connected = true;
108                         locked = false;
109                 } else {
110                         connected = false;
111                         locked = false;
112                 }
113         }
114
115         public boolean parse_string (String line, boolean says_done) {
116                 String[] bits = line.split("\\s+");
117                 if (bits.length == 0)
118                         return false;
119                 if (line.startsWith("Date:")) {
120                         if (bits.length < 2)
121                                 return false;
122                         String[] d = bits[1].split("/");
123                         if (d.length < 3)
124                                 return false;
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:")) {
129                         if (bits.length < 2)
130                                 return false;
131                         String[] d = bits[1].split(":");
132                         if (d.length < 3)
133                                 return false;
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:")) {
138                         if (bits.length < 3)
139                                 return false;
140                         lat = Integer.parseInt(bits[1]) * 1.0e-7;
141                         lon = Integer.parseInt(bits[2]) * 1.0e-7;
142                 } else if (line.startsWith("Alt:")) {
143                         if (bits.length < 2)
144                                 return false;
145                         alt = Integer.parseInt(bits[1]);
146                 } else if (line.startsWith("Flags:")) {
147                         if (bits.length < 2)
148                                 return false;
149                         int status = Integer.decode(bits[1]);
150                         connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
151                         locked = (status & AltosLib.AO_GPS_VALID) != 0;
152                         if (!says_done)
153                                 return false;
154                 } else if (line.startsWith("Sats:")) {
155                         if (bits.length < 2)
156                                 return false;
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);
163                         }
164                 } else if (line.startsWith("done")) {
165                         return false;
166                 } else
167                         return false;
168                 return true;
169         }
170
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");
175
176                 connected = false;
177                 locked = false;
178                 lat = lon = 0;
179                 alt = 0;
180                 ClearGPSTime();
181                 if ((words[i]).equals("unlocked")) {
182                         connected = true;
183                         i++;
184                 } else if ((words[i]).equals("not-connected")) {
185                         i++;
186                 } else if (words.length >= 40) {
187                         locked = true;
188                         connected = true;
189
190                         if (version > 1)
191                                 ParseGPSDate(words[i++]);
192                         else
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_net(AltosParse.strip_suffix(words[i++], "m/s(H)"));
200                                 course = AltosParse.parse_int(words[i++]);
201                                 climb_rate = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(V)"));
202                                 hdop = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "(hdop)"));
203                                 h_error = AltosParse.parse_int(words[i++]);
204                                 v_error = AltosParse.parse_int(words[i++]);
205                         }
206                 } else {
207                         i++;
208                 }
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;
214                         else
215                                 tracking_channels = AltosParse.parse_int(words[i]);
216                         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 */
222                                 if (version < 2)
223                                         i++;
224                                 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
225                         }
226                 } else
227                         cc_gps_sat = new AltosGPSSat[0];
228         }
229
230         public void set_latitude(int in_lat) {
231                 lat = in_lat / 10.0e7;
232         }
233
234         public void set_longitude(int in_lon) {
235                 lon = in_lon / 10.0e7;
236         }
237
238         public void set_time(int in_hour, int in_minute, int in_second) {
239                 hour = in_hour;
240                 minute = in_minute;
241                 second = in_second;
242         }
243
244         public void set_date(int in_year, int in_month, int in_day) {
245                 year = in_year;
246                 month = in_month;
247                 day = in_day;
248         }
249
250         /*
251         public void set_flags(int in_flags) {
252                 flags = in_flags;
253         }
254         */
255
256         public void set_altitude(int in_altitude) {
257                 alt = in_altitude;
258         }
259
260         public void add_sat(int svid, int c_n0) {
261                 if (cc_gps_sat == null) {
262                         cc_gps_sat = new AltosGPSSat[1];
263                 } else {
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;
268                 }
269                 AltosGPSSat     sat = new AltosGPSSat();
270                 sat.svid = svid;
271                 sat.c_n0 = c_n0;
272                 cc_gps_sat[cc_gps_sat.length - 1] = sat;
273         }
274
275         private void init() {
276                 lat = AltosLib.MISSING;
277                 lon = AltosLib.MISSING;
278                 alt = AltosLib.MISSING;
279                 ground_speed = AltosLib.MISSING;
280                 course = AltosLib.MISSING;
281                 climb_rate = AltosLib.MISSING;
282                 pdop = AltosLib.MISSING;
283                 hdop = AltosLib.MISSING;
284                 vdop = AltosLib.MISSING;
285                 h_error = AltosLib.MISSING;
286                 v_error = AltosLib.MISSING;
287                 ClearGPSTime();
288                 cc_gps_sat = null;
289         }
290
291         public AltosGPS() {
292                 init();
293         }
294
295         public AltosGPS clone() {
296                 AltosGPS        g = new AltosGPS();
297
298                 g.nsat = nsat;
299                 g.locked = locked;
300                 g.connected = connected;
301                 g.lat = lat;            /* degrees (+N -S) */
302                 g.lon = lon;            /* degrees (+E -W) */
303                 g.alt = alt;            /* m */
304                 g.year = year;
305                 g.month = month;
306                 g.day = day;
307                 g.hour = hour;
308                 g.minute = minute;
309                 g.second = second;
310
311                 g.ground_speed = ground_speed;  /* m/s */
312                 g.course = course;              /* degrees */
313                 g.climb_rate = climb_rate;      /* m/s */
314                 g.pdop = pdop;          /* unitless */
315                 g.hdop = hdop;          /* unitless */
316                 g.vdop = vdop;          /* unitless */
317                 g.h_error = h_error;    /* m */
318                 g.v_error = v_error;    /* m */
319
320                 if (cc_gps_sat != null) {
321                         g.cc_gps_sat = new AltosGPSSat[cc_gps_sat.length];
322                         for (int i = 0; i < cc_gps_sat.length; i++) {
323                                 g.cc_gps_sat[i] = new AltosGPSSat(cc_gps_sat[i].svid,
324                                                                   cc_gps_sat[i].c_n0);
325                         }
326                 }
327                 return g;
328         }
329
330         public AltosGPS(AltosGPS old) {
331                 if (old != null) {
332                         nsat = old.nsat;
333                         locked = old.locked;
334                         connected = old.connected;
335                         lat = old.lat;          /* degrees (+N -S) */
336                         lon = old.lon;          /* degrees (+E -W) */
337                         alt = old.alt;          /* m */
338                         year = old.year;
339                         month = old.month;
340                         day = old.day;
341                         hour = old.hour;
342                         minute = old.minute;
343                         second = old.second;
344
345                         ground_speed = old.ground_speed;        /* m/s */
346                         course = old.course;            /* degrees */
347                         climb_rate = old.climb_rate;    /* m/s */
348                         pdop = old.pdop;                /* unitless? */
349                         hdop = old.hdop;                /* unitless? */
350                         vdop = old.vdop;                /* unitless? */
351                         h_error = old.h_error;          /* m */
352                         v_error = old.v_error;          /* m */
353
354                         if (old.cc_gps_sat != null) {
355                                 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
356                                 for (int i = 0; i < old.cc_gps_sat.length; i++) {
357                                         cc_gps_sat[i] = new AltosGPSSat();
358                                         cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
359                                         cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
360                                 }
361                         }
362                 } else {
363                         init();
364                 }
365         }
366
367         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
368                 try {
369                         AltosGPS        gps = new AltosGPS(link, config_data);
370
371                         if (gps != null) {
372                                 state.set_gps(gps, state.gps_sequence++);
373                                 return;
374                         }
375                 } catch (TimeoutException te) {
376                 }
377                 state.set_gps(null, 0);
378         }
379
380         public AltosGPS (AltosLink link, AltosConfigData config_data) throws TimeoutException, InterruptedException {
381                 boolean says_done = config_data.compare_version("1.0") >= 0;
382                 init();
383                 link.printf("g\n");
384                 for (;;) {
385                         String line = link.get_reply_no_dialog(5000);
386                         if (line == null)
387                                 throw new TimeoutException();
388                         if (!parse_string(line, says_done))
389                                 break;
390                 }
391         }
392 }