]> git.gag.com Git - fw/altos/blob - altoslib/AltosEepromGPS.java
Merge branch 'branch-1.6' into debian
[fw/altos] / altoslib / AltosEepromGPS.java
1 /*
2  * Copyright © 2014 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 org.altusmetrum.altoslib_8;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromGPS extends AltosEeprom {
25         public static final int record_length = 32;
26
27         public static final int max_sat = 12;
28
29         public int record_length() { return record_length; }
30
31         /* AO_LOG_FLIGHT elements */
32         public int flight() { return data16(0); }
33         public int start_altitude() { return data16(2); }
34         public int start_latitude() { return data32(4); }
35         public int start_longitude() { return data32(8); }
36
37         /* AO_LOG_GPS_TIME elements */
38         public int latitude() { return data32(0); }
39         public int longitude() { return data32(4); }
40         public int altitude_low() { return data16(8); }
41         public int hour() { return data8(10); }
42         public int minute() { return data8(11); }
43         public int second() { return data8(12); }
44         public int flags() { return data8(13); }
45         public int year() { return data8(14); }
46         public int month() { return data8(15); }
47         public int day() { return data8(16); }
48         public int course() { return data8(17); }
49         public int ground_speed() { return data16(18); }
50         public int climb_rate() { return data16(20); }
51         public int pdop() { return data8(22); }
52         public int hdop() { return data8(23); }
53         public int vdop() { return data8(24); }
54         public int mode() { return data8(25); }
55         public int altitude_high() { return data16(26); }
56
57         public boolean has_seconds() { return cmd == AltosLib.AO_LOG_GPS_TIME; }
58
59         public int seconds() {
60                 switch (cmd) {
61                 case AltosLib.AO_LOG_GPS_TIME:
62                         return second() + 60 * (minute() + 60 * (hour() + 24 * (day() + 31 * month())));
63                 default:
64                         return 0;
65                 }
66         }
67
68         public AltosEepromGPS (AltosEepromChunk chunk, int start) throws ParseException {
69                 parse_chunk(chunk, start);
70         }
71
72         public void update_state(AltosState state) {
73                 super.update_state(state);
74
75                 AltosGPS        gps;
76
77                 /* Flush any pending GPS changes */
78                 if (state.gps_pending) {
79                         switch (cmd) {
80                         case AltosLib.AO_LOG_GPS_LAT:
81                         case AltosLib.AO_LOG_GPS_LON:
82                         case AltosLib.AO_LOG_GPS_ALT:
83                         case AltosLib.AO_LOG_GPS_SAT:
84                         case AltosLib.AO_LOG_GPS_DATE:
85                                 break;
86                         default:
87                                 state.set_temp_gps();
88                                 break;
89                         }
90                 }
91
92                 switch (cmd) {
93                 case AltosLib.AO_LOG_FLIGHT:
94                         if (state.flight == AltosLib.MISSING) {
95                                 state.set_boost_tick(tick);
96                                 state.set_flight(flight());
97                         }
98                         /* no place to log start lat/lon yet */
99                         break;
100                 case AltosLib.AO_LOG_GPS_TIME:
101                         state.set_tick(tick);
102                         gps = state.make_temp_gps(false);
103                         gps.lat = latitude() / 1e7;
104                         gps.lon = longitude() / 1e7;
105                         if (state.altitude_32())
106                                 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
107                         else
108                                 gps.alt = altitude_low();
109
110                         gps.hour = hour();
111                         gps.minute = minute();
112                         gps.second = second();
113
114                         int flags = flags();
115
116                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
117                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
118                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
119                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
120
121                         gps.year = 2000 + year();
122                         gps.month = month();
123                         gps.day = day();
124                         gps.ground_speed = ground_speed() * 1.0e-2;
125                         gps.course = course() * 2;
126                         gps.climb_rate = climb_rate() * 1.0e-2;
127                         if (state.compare_version("1.4.9") >= 0) {
128                                 gps.pdop = pdop() / 10.0;
129                                 gps.hdop = hdop() / 10.0;
130                                 gps.vdop = vdop() / 10.0;
131                         } else {
132                                 gps.pdop = pdop() / 100.0;
133                                 if (gps.pdop < 0.8)
134                                         gps.pdop += 2.56;
135                                 gps.hdop = hdop() / 100.0;
136                                 if (gps.hdop < 0.8)
137                                         gps.hdop += 2.56;
138                                 gps.vdop = vdop() / 100.0;
139                                 if (gps.vdop < 0.8)
140                                         gps.vdop += 2.56;
141                         }
142                         break;
143                 }
144         }
145
146         public AltosEepromGPS (String line) {
147                 parse_string(line);
148         }
149
150         static public LinkedList<AltosEeprom> read(FileInputStream input) {
151                 LinkedList<AltosEeprom> tgpss = new LinkedList<AltosEeprom>();
152
153                 for (;;) {
154                         try {
155                                 String line = AltosLib.gets(input);
156                                 if (line == null)
157                                         break;
158                                 try {
159                                         AltosEepromGPS tgps = new AltosEepromGPS(line);
160                                         if (tgps.cmd != AltosLib.AO_LOG_INVALID)
161                                                 tgpss.add(tgps);
162                                 } catch (Exception e) {
163                                         System.out.printf ("exception\n");
164                                 }
165                         } catch (IOException ie) {
166                                 break;
167                         }
168                 }
169
170                 return tgpss;
171         }
172 }