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