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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
31 import altosui.AltosRecord;
32 import altosui.AltosState;
33 import altosui.AltosDeviceDialog;
34 import altosui.AltosPreferences;
35 import altosui.AltosLog;
36 import altosui.AltosVoice;
37 import altosui.AltosEepromMonitor;
40 * AltosRecords with an index field so they can be sorted by tick while preserving
41 * the original ordering for elements with matching ticks
43 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
47 public AltosOrderedRecord(String line, int in_index, int prev_tick)
48 throws ParseException {
50 int new_tick = tick | (prev_tick & ~0xffff);
51 if (new_tick < prev_tick) {
52 if (prev_tick - new_tick > 0x8000)
59 public AltosOrderedRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
60 super(in_cmd, in_tick, in_a, in_b);
64 public int compareTo(AltosOrderedRecord o) {
65 int tick_diff = tick - o.tick;
68 return index - o.index;
72 public class AltosEepromReader extends AltosReader {
74 static final int seen_flight = 1;
75 static final int seen_sensor = 2;
76 static final int seen_temp_volt = 4;
77 static final int seen_deploy = 8;
78 static final int seen_gps_time = 16;
79 static final int seen_gps_lat = 32;
80 static final int seen_gps_lon = 64;
82 static final int seen_basic = seen_flight|seen_sensor|seen_temp_volt|seen_deploy;
85 AltosOrderedRecord record;
87 TreeSet<AltosOrderedRecord> records;
89 Iterator<AltosOrderedRecord> record_iterator;
95 boolean last_reported;
106 boolean saw_gps_date;
108 boolean missing_gps_time;
110 public AltosRecord read() throws IOException, ParseException {
112 if (record == null) {
113 if (!record_iterator.hasNext()) {
116 last_reported = true;
117 AltosRecord r = new AltosRecord(state);
118 r.time = (r.tick - boost_tick) / 100.0;
121 record = record_iterator.next();
123 if ((seen & seen_basic) == seen_basic && record.tick != state.tick) {
124 AltosRecord r = new AltosRecord(state);
125 r.time = (r.tick - boost_tick) / 100.0;
130 state.tick = record.tick;
131 switch (record.cmd) {
132 case Altos.AO_LOG_FLIGHT:
133 /* recorded when first read from the file */
135 case Altos.AO_LOG_SENSOR:
136 state.accel = record.a;
137 state.pres = record.b;
138 if (state.state < Altos.ao_flight_boost) {
140 ground_pres += state.pres;
141 state.ground_pres = (int) (ground_pres / n_pad_samples);
142 state.flight_pres = state.ground_pres;
143 ground_accel += state.accel;
144 state.ground_accel = (int) (ground_accel / n_pad_samples);
145 state.flight_accel = state.ground_accel;
147 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
148 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
149 state.flight_vel += (state.accel_plus_g - state.accel);
153 case Altos.AO_LOG_TEMP_VOLT:
154 state.temp = record.a;
155 state.batt = record.b;
156 seen |= seen_temp_volt;
158 case Altos.AO_LOG_DEPLOY:
159 state.drogue = record.a;
160 state.main = record.b;
163 case Altos.AO_LOG_STATE:
164 state.state = record.a;
166 case Altos.AO_LOG_GPS_TIME:
167 gps_tick = state.tick;
168 AltosGPS old = state.gps;
169 state.gps = new AltosGPS();
171 /* GPS date doesn't get repeated through the file */
173 state.gps.year = old.year;
174 state.gps.month = old.month;
175 state.gps.day = old.day;
177 state.gps.hour = (record.a & 0xff);
178 state.gps.minute = (record.a >> 8);
179 state.gps.second = (record.b & 0xff);
180 int flags = (record.b >> 8);
181 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
182 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
183 state.gps.date_valid = (flags & Altos.AO_GPS_DATE_VALID) != 0;
184 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
185 Altos.AO_GPS_NUM_SAT_SHIFT;
187 case Altos.AO_LOG_GPS_LAT:
188 int lat32 = record.a | (record.b << 16);
189 state.gps.lat = (double) lat32 / 1e7;
191 case Altos.AO_LOG_GPS_LON:
192 int lon32 = record.a | (record.b << 16);
193 state.gps.lon = (double) lon32 / 1e7;
195 case Altos.AO_LOG_GPS_ALT:
196 state.gps.alt = record.a;
198 case Altos.AO_LOG_GPS_SAT:
199 if (state.tick == gps_tick) {
201 int c_n0 = record.b >> 8;
202 state.gps.add_sat(svid, c_n0);
205 case Altos.AO_LOG_GPS_DATE:
206 state.gps.year = (record.a & 0xff) + 2000;
207 state.gps.month = record.a >> 8;
208 state.gps.day = record.b & 0xff;
211 case Altos.AO_LOG_CONFIG_VERSION:
213 case Altos.AO_LOG_MAIN_DEPLOY:
215 case Altos.AO_LOG_APOGEE_DELAY:
217 case Altos.AO_LOG_RADIO_CHANNEL:
219 case Altos.AO_LOG_CALLSIGN:
220 state.callsign = record.data;
222 case Altos.AO_LOG_ACCEL_CAL:
223 state.accel_plus_g = record.a;
224 state.accel_minus_g = record.b;
226 case Altos.AO_LOG_RADIO_CAL:
228 case Altos.AO_LOG_MANUFACTURER:
230 case Altos.AO_LOG_PRODUCT:
232 case Altos.AO_LOG_SERIAL_NUMBER:
233 state.serial = record.a;
235 case Altos.AO_LOG_SOFTWARE_VERSION:
242 public void write_comments(PrintStream out) {
243 Iterator<AltosOrderedRecord> iterator = records.iterator();
244 out.printf("# Comments\n");
245 while (iterator.hasNext()) {
246 AltosOrderedRecord record = iterator.next();
247 switch (record.cmd) {
248 case Altos.AO_LOG_CONFIG_VERSION:
249 out.printf("# Config version: %s\n", record.data);
251 case Altos.AO_LOG_MAIN_DEPLOY:
252 out.printf("# Main deploy: %s\n", record.a);
254 case Altos.AO_LOG_APOGEE_DELAY:
255 out.printf("# Apogee delay: %s\n", record.a);
257 case Altos.AO_LOG_RADIO_CHANNEL:
258 out.printf("# Radio channel: %s\n", record.a);
260 case Altos.AO_LOG_CALLSIGN:
261 out.printf("# Callsign: %s\n", record.data);
263 case Altos.AO_LOG_ACCEL_CAL:
264 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
266 case Altos.AO_LOG_RADIO_CAL:
267 out.printf ("# Radio cal: %d\n", record.a);
269 case Altos.AO_LOG_MANUFACTURER:
270 out.printf ("# Manufacturer: %s\n", record.data);
272 case Altos.AO_LOG_PRODUCT:
273 out.printf ("# Product: %s\n", record.data);
275 case Altos.AO_LOG_SERIAL_NUMBER:
276 out.printf ("# Serial number: %d\n", record.a);
278 case Altos.AO_LOG_SOFTWARE_VERSION:
279 out.printf ("# Software version: %s\n", record.data);
286 * Given an AO_LOG_GPS_TIME record with correct time, and one
287 * missing time, rewrite the missing time values with the good
288 * ones, assuming that the difference between them is 'diff' seconds
290 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
292 int diff = (bad.tick - good.tick + 50) / 100;
294 int hour = (good.a & 0xff);
295 int minute = (good.a >> 8);
296 int second = (good.b & 0xff);
297 int flags = (good.b >> 8);
298 int seconds = hour * 3600 + minute * 60 + second;
300 int new_seconds = seconds + diff;
302 new_seconds += 24 * 3600;
303 int new_second = (new_seconds % 60);
304 int new_minutes = (new_seconds / 60);
305 int new_minute = (new_minutes % 60);
306 int new_hours = (new_minutes / 60);
307 int new_hour = (new_hours % 24);
309 bad.a = new_hour + (new_minute << 8);
310 bad.b = new_second + (flags << 8);
314 * Read the whole file, dumping records into a RB tree so
315 * we can enumerate them in time order -- the eeprom data
316 * are sometimes out of order with GPS data getting timestamps
317 * matching the first packet out of the GPS unit but not
318 * written until the final GPS packet has been received.
320 public AltosEepromReader (FileInputStream input) {
321 state = new AltosRecord();
322 state.state = Altos.ao_flight_pad;
323 state.accel_plus_g = 15758;
324 state.accel_minus_g = 16294;
326 records = new TreeSet<AltosOrderedRecord>();
328 AltosOrderedRecord last_gps_time = null;
333 boolean missing_time = false;
337 String line = AltosRecord.gets(input);
340 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, tick);
343 if (record.cmd == Altos.AO_LOG_INVALID)
346 if (record.cmd == Altos.AO_LOG_FLIGHT) {
347 state.ground_accel = record.a;
348 state.flight = record.b;
353 /* Two firmware bugs caused the loss of some GPS data.
354 * The flight date would never be recorded, and often
355 * the flight time would get overwritten by another
356 * record. Detect the loss of the GPS date and fix up the
357 * missing time records
359 if (record.cmd == Altos.AO_LOG_GPS_DATE)
362 /* go back and fix up any missing time values */
363 if (record.cmd == Altos.AO_LOG_GPS_TIME) {
364 last_gps_time = record;
366 Iterator<AltosOrderedRecord> iterator = records.iterator();
367 while (iterator.hasNext()) {
368 AltosOrderedRecord old = iterator.next();
369 if (old.cmd == Altos.AO_LOG_GPS_TIME &&
370 old.a == -1 && old.b == -1)
372 update_time(record, old);
375 missing_time = false;
379 if (record.cmd == Altos.AO_LOG_GPS_LAT) {
380 if (last_gps_time == null || last_gps_time.tick != record.tick) {
381 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(Altos.AO_LOG_GPS_TIME,
384 if (last_gps_time != null)
385 update_time(last_gps_time, add_gps_time);
389 records.add(add_gps_time);
390 record.index = index++;
395 /* Bail after reading the 'landed' record; we're all done */
396 if (record.cmd == Altos.AO_LOG_STATE &&
397 record.a == Altos.ao_flight_landed)
400 } catch (IOException io) {
401 } catch (ParseException pe) {
403 record_iterator = records.iterator();
406 } catch (IOException ie) {