6b84d7a57ec29eb145ce603270546d93cb1f1d97
[fw/altos] / ao-tools / altosui / 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.text.*;
22 import altosui.AltosParse;
23
24
25 public class AltosGPS {
26         public class AltosGPSSat {
27                 int     svid;
28                 int     c_n0;
29         }
30
31         int     nsat;
32         boolean gps_locked;
33         boolean gps_connected;
34         double  lat;            /* degrees (+N -S) */
35         double  lon;            /* degrees (+E -W) */
36         int     alt;            /* m */
37         int     year;
38         int     month;
39         int     day;
40         int     hour;
41         int     minute;
42         int     second;
43
44         int     gps_extended;   /* has extra data */
45         double  ground_speed;   /* m/s */
46         int     course;         /* degrees */
47         double  climb_rate;     /* m/s */
48         double  hdop;           /* unitless? */
49         int     h_error;        /* m */
50         int     v_error;        /* m */
51
52         AltosGPSSat[] cc_gps_sat;       /* tracking data */
53
54         void ParseGPSTime(String date, String time) throws ParseException {
55                 String[] ymd = date.split("-");
56                 if (ymd.length != 3)
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]);
61
62                 String[] hms = time.split(":");
63                 if (hms.length != 3)
64                         throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
65                 hour = AltosParse.parse_int(hms[0]);
66                 minute = AltosParse.parse_int(hms[1]);
67                 second = AltosParse.parse_int(hms[2]);
68         }
69
70         void ClearGPSTime() {
71                 year = month = day = 0;
72                 hour = minute = second = 0;
73         }
74
75         public AltosGPS(String[] words, int i) throws ParseException {
76                 AltosParse.word(words[i++], "GPS");
77                 nsat = AltosParse.parse_int(words[i++]);
78                 AltosParse.word(words[i++], "sat");
79
80                 gps_connected = false;
81                 gps_locked = false;
82                 lat = lon = 0;
83                 alt = 0;
84                 ClearGPSTime();
85                 if ((words[i]).equals("unlocked")) {
86                         gps_connected = true;
87                         i++;
88                 } else if ((words[i]).equals("not-connected")) {
89                         i++;
90                 } else if (words.length >= 40) {
91                         gps_locked = true;
92                         gps_connected = true;
93
94                         ParseGPSTime(words[i], words[i+1]); i += 2;
95                         lat = AltosParse.parse_coord(words[i++]);
96                         lon = AltosParse.parse_coord(words[i++]);
97                         alt = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "m"));
98                         ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
99                         course = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "°"));
100                         climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
101                         hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
102                         h_error = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "(herr)"));
103                         v_error = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "(verr)"));
104                 } else {
105                         i++;
106                 }
107                 AltosParse.word(words[i++], "SAT");
108                 int tracking_channels = 0;
109                 if (words[i].equals("not-connected"))
110                         tracking_channels = 0;
111                 else
112                         tracking_channels = AltosParse.parse_int(words[i]);
113                 i++;
114                 cc_gps_sat = new AltosGPS.AltosGPSSat[tracking_channels];
115                 for (int chan = 0; chan < tracking_channels; chan++) {
116                         cc_gps_sat[chan] = new AltosGPS.AltosGPSSat();
117                         cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
118                         cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
119                 }
120         }
121
122         public void set_latitude(int in_lat) {
123                 lat = in_lat / 10.0e7;
124         }
125
126         public void set_longitude(int in_lon) {
127                 lon = in_lon / 10.0e7;
128         }
129
130         public void set_time(int hour, int minute, int second) {
131                 hour = hour;
132                 minute = minute;
133                 second = second;
134         }
135
136         public void set_date(int year, int month, int day) {
137                 year = year;
138                 month = month;
139                 day = day;
140         }
141
142         public void set_flags(int flags) {
143                 flags = flags;
144         }
145
146         public void set_altitude(int altitude) {
147                 altitude = altitude;
148         }
149
150         public void add_sat(int svid, int c_n0) {
151                 if (cc_gps_sat == null) {
152                         cc_gps_sat = new AltosGPS.AltosGPSSat[1];
153                 } else {
154                         AltosGPSSat[] new_gps_sat = new AltosGPS.AltosGPSSat[cc_gps_sat.length + 1];
155                         for (int i = 0; i < cc_gps_sat.length; i++)
156                                 new_gps_sat[i] = cc_gps_sat[i];
157                         cc_gps_sat = new_gps_sat;
158                 }
159                 AltosGPS.AltosGPSSat    sat = new AltosGPS.AltosGPSSat();
160                 sat.svid = svid;
161                 sat.c_n0 = c_n0;
162                 cc_gps_sat[cc_gps_sat.length - 1] = sat;
163         }
164
165         public AltosGPS() {
166                 ClearGPSTime();
167                 cc_gps_sat = null;
168         }
169
170         public AltosGPS(AltosGPS old) {
171                 nsat = old.nsat;
172                 gps_locked = old.gps_locked;
173                 gps_connected = old.gps_connected;
174                 lat = old.lat;          /* degrees (+N -S) */
175                 lon = old.lon;          /* degrees (+E -W) */
176                 alt = old.alt;          /* m */
177                 year = old.year;
178                 month = old.month;
179                 day = old.day;
180                 hour = old.hour;
181                 minute = old.minute;
182                 second = old.second;
183
184                 gps_extended = old.gps_extended;        /* has extra data */
185                 ground_speed = old.ground_speed;        /* m/s */
186                 course = old.course;            /* degrees */
187                 climb_rate = old.climb_rate;    /* m/s */
188                 hdop = old.hdop;                /* unitless? */
189                 h_error = old.h_error;  /* m */
190                 v_error = old.v_error;  /* m */
191
192                 AltosGPSSat[] cc_gps_sat;       /* tracking data */
193                 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
194                 for (int i = 0; i < old.cc_gps_sat.length; i++) {
195                         cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
196                         cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
197                 }
198         }
199 }