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;
30 import org.altusmetrum.AltosLib.*;
33 * AltosRecords with an index field so they can be sorted by tick while preserving
34 * the original ordering for elements with matching ticks
36 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
40 public AltosOrderedRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
41 throws ParseException {
43 if (prev_tick_valid) {
44 tick |= (prev_tick & ~0xffff);
45 if (tick < prev_tick) {
46 if (prev_tick - tick > 0x8000)
49 if (tick - prev_tick > 0x8000)
56 public AltosOrderedRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
57 super(in_cmd, in_tick, in_a, in_b);
61 public String toString() {
62 return String.format("%d.%d %04x %04x %04x",
63 cmd, index, tick, a, b);
66 public int compareTo(AltosOrderedRecord o) {
67 int tick_diff = tick - o.tick;
70 return index - o.index;
74 public class AltosEepromIterable extends AltosRecordIterable {
76 static final int seen_flight = 1;
77 static final int seen_sensor = 2;
78 static final int seen_temp_volt = 4;
79 static final int seen_deploy = 8;
80 static final int seen_gps_time = 16;
81 static final int seen_gps_lat = 32;
82 static final int seen_gps_lon = 64;
84 static final int seen_basic = seen_flight|seen_sensor;
90 AltosEepromRecord flight_record;
91 AltosEepromRecord gps_date_record;
93 TreeSet<AltosOrderedRecord> records;
95 LinkedList<AltosRecord> list;
112 void update_state(AltosRecord state, AltosEepromRecord record, EepromState eeprom) {
113 state.tick = record.tick;
114 switch (record.cmd) {
115 case Altos.AO_LOG_FLIGHT:
116 eeprom.seen |= seen_flight;
117 state.ground_accel = record.a;
118 state.flight_accel = record.a;
119 state.flight = record.b;
120 eeprom.boost_tick = record.tick;
122 case Altos.AO_LOG_SENSOR:
123 state.accel = record.a;
124 state.pres = record.b;
125 if (state.state < Altos.ao_flight_boost) {
126 eeprom.n_pad_samples++;
127 eeprom.ground_pres += state.pres;
128 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
129 state.flight_pres = state.ground_pres;
131 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
132 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
133 state.flight_vel += (state.accel_plus_g - state.accel);
135 eeprom.seen |= seen_sensor;
138 case Altos.AO_LOG_PRESSURE:
139 state.pres = record.b;
140 state.flight_pres = state.pres;
141 if (eeprom.n_pad_samples == 0) {
142 eeprom.n_pad_samples++;
143 state.ground_pres = state.pres;
145 eeprom.seen |= seen_sensor;
147 case Altos.AO_LOG_TEMP_VOLT:
148 state.temp = record.a;
149 state.batt = record.b;
150 eeprom.seen |= seen_temp_volt;
152 case Altos.AO_LOG_DEPLOY:
153 state.drogue = record.a;
154 state.main = record.b;
155 eeprom.seen |= seen_deploy;
158 case Altos.AO_LOG_STATE:
159 state.state = record.a;
161 case Altos.AO_LOG_GPS_TIME:
162 eeprom.gps_tick = state.tick;
163 AltosGPS old = state.gps;
164 state.gps = new AltosGPS();
166 /* GPS date doesn't get repeated through the file */
168 state.gps.year = old.year;
169 state.gps.month = old.month;
170 state.gps.day = old.day;
172 state.gps.hour = (record.a & 0xff);
173 state.gps.minute = (record.a >> 8);
174 state.gps.second = (record.b & 0xff);
176 int flags = (record.b >> 8);
177 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
178 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
179 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
180 Altos.AO_GPS_NUM_SAT_SHIFT;
181 state.new_gps = true;
184 case Altos.AO_LOG_GPS_LAT:
185 int lat32 = record.a | (record.b << 16);
186 state.gps.lat = (double) lat32 / 1e7;
188 case Altos.AO_LOG_GPS_LON:
189 int lon32 = record.a | (record.b << 16);
190 state.gps.lon = (double) lon32 / 1e7;
192 case Altos.AO_LOG_GPS_ALT:
193 state.gps.alt = record.a;
195 case Altos.AO_LOG_GPS_SAT:
196 if (state.tick == eeprom.gps_tick) {
198 int c_n0 = record.b >> 8;
199 state.gps.add_sat(svid, c_n0);
202 case Altos.AO_LOG_GPS_DATE:
203 state.gps.year = (record.a & 0xff) + 2000;
204 state.gps.month = record.a >> 8;
205 state.gps.day = record.b & 0xff;
208 case Altos.AO_LOG_CONFIG_VERSION:
210 case Altos.AO_LOG_MAIN_DEPLOY:
212 case Altos.AO_LOG_APOGEE_DELAY:
214 case Altos.AO_LOG_RADIO_CHANNEL:
216 case Altos.AO_LOG_CALLSIGN:
217 state.callsign = record.data;
219 case Altos.AO_LOG_ACCEL_CAL:
220 state.accel_plus_g = record.a;
221 state.accel_minus_g = record.b;
223 case Altos.AO_LOG_RADIO_CAL:
225 case Altos.AO_LOG_MANUFACTURER:
227 case Altos.AO_LOG_PRODUCT:
229 case Altos.AO_LOG_SERIAL_NUMBER:
230 state.serial = record.a;
232 case Altos.AO_LOG_SOFTWARE_VERSION:
235 state.seen |= eeprom.seen;
238 LinkedList<AltosRecord> make_list() {
239 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
240 Iterator<AltosOrderedRecord> iterator = records.iterator();
241 AltosOrderedRecord record = null;
242 AltosRecord state = new AltosRecord();
243 boolean last_reported = false;
244 EepromState eeprom = new EepromState();
246 state.state = Altos.ao_flight_pad;
247 state.accel_plus_g = 15758;
248 state.accel_minus_g = 16294;
250 /* Pull in static data from the flight and gps_date records */
251 if (flight_record != null)
252 update_state(state, flight_record, eeprom);
253 if (gps_date_record != null)
254 update_state(state, gps_date_record, eeprom);
256 while (iterator.hasNext()) {
257 record = iterator.next();
258 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
259 AltosRecord r = new AltosRecord(state);
260 r.time = (r.tick - eeprom.boost_tick) / 100.0;
263 update_state(state, record, eeprom);
265 AltosRecord r = new AltosRecord(state);
266 r.time = (r.tick - eeprom.boost_tick) / 100.0;
271 public Iterator<AltosRecord> iterator() {
274 return list.iterator();
277 public boolean has_gps() { return has_gps; }
278 public boolean has_accel() { return has_accel; }
279 public boolean has_ignite() { return has_ignite; }
281 public void write_comments(PrintStream out) {
282 Iterator<AltosOrderedRecord> iterator = records.iterator();
283 out.printf("# Comments\n");
284 while (iterator.hasNext()) {
285 AltosOrderedRecord record = iterator.next();
286 switch (record.cmd) {
287 case Altos.AO_LOG_CONFIG_VERSION:
288 out.printf("# Config version: %s\n", record.data);
290 case Altos.AO_LOG_MAIN_DEPLOY:
291 out.printf("# Main deploy: %s\n", record.a);
293 case Altos.AO_LOG_APOGEE_DELAY:
294 out.printf("# Apogee delay: %s\n", record.a);
296 case Altos.AO_LOG_RADIO_CHANNEL:
297 out.printf("# Radio channel: %s\n", record.a);
299 case Altos.AO_LOG_CALLSIGN:
300 out.printf("# Callsign: %s\n", record.data);
302 case Altos.AO_LOG_ACCEL_CAL:
303 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
305 case Altos.AO_LOG_RADIO_CAL:
306 out.printf ("# Radio cal: %d\n", record.a);
308 case Altos.AO_LOG_MAX_FLIGHT_LOG:
309 out.printf ("# Max flight log: %d\n", record.a);
311 case Altos.AO_LOG_MANUFACTURER:
312 out.printf ("# Manufacturer: %s\n", record.data);
314 case Altos.AO_LOG_PRODUCT:
315 out.printf ("# Product: %s\n", record.data);
317 case Altos.AO_LOG_SERIAL_NUMBER:
318 out.printf ("# Serial number: %d\n", record.a);
320 case Altos.AO_LOG_SOFTWARE_VERSION:
321 out.printf ("# Software version: %s\n", record.data);
328 * Given an AO_LOG_GPS_TIME record with correct time, and one
329 * missing time, rewrite the missing time values with the good
330 * ones, assuming that the difference between them is 'diff' seconds
332 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
334 int diff = (bad.tick - good.tick + 50) / 100;
336 int hour = (good.a & 0xff);
337 int minute = (good.a >> 8);
338 int second = (good.b & 0xff);
339 int flags = (good.b >> 8);
340 int seconds = hour * 3600 + minute * 60 + second;
342 /* Make sure this looks like a good GPS value */
343 if ((flags & Altos.AO_GPS_NUM_SAT_MASK) >> Altos.AO_GPS_NUM_SAT_SHIFT < 4)
344 flags = (flags & ~Altos.AO_GPS_NUM_SAT_MASK) | (4 << Altos.AO_GPS_NUM_SAT_SHIFT);
345 flags |= Altos.AO_GPS_RUNNING;
346 flags |= Altos.AO_GPS_VALID;
348 int new_seconds = seconds + diff;
350 new_seconds += 24 * 3600;
351 int new_second = (new_seconds % 60);
352 int new_minutes = (new_seconds / 60);
353 int new_minute = (new_minutes % 60);
354 int new_hours = (new_minutes / 60);
355 int new_hour = (new_hours % 24);
357 bad.a = new_hour + (new_minute << 8);
358 bad.b = new_second + (flags << 8);
362 * Read the whole file, dumping records into a RB tree so
363 * we can enumerate them in time order -- the eeprom data
364 * are sometimes out of order with GPS data getting timestamps
365 * matching the first packet out of the GPS unit but not
366 * written until the final GPS packet has been received.
368 public AltosEepromIterable (FileInputStream input) {
369 records = new TreeSet<AltosOrderedRecord>();
371 AltosOrderedRecord last_gps_time = null;
375 boolean prev_tick_valid = false;
376 boolean missing_time = false;
380 String line = AltosRecord.gets(input);
383 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
386 if (record.cmd == Altos.AO_LOG_INVALID)
388 prev_tick = record.tick;
389 if (record.cmd < Altos.AO_LOG_CONFIG_VERSION)
390 prev_tick_valid = true;
391 if (record.cmd == Altos.AO_LOG_FLIGHT) {
392 flight_record = record;
396 /* Two firmware bugs caused the loss of some GPS data.
397 * The flight date would never be recorded, and often
398 * the flight time would get overwritten by another
399 * record. Detect the loss of the GPS date and fix up the
400 * missing time records
402 if (record.cmd == Altos.AO_LOG_GPS_DATE) {
403 gps_date_record = record;
407 /* go back and fix up any missing time values */
408 if (record.cmd == Altos.AO_LOG_GPS_TIME) {
409 last_gps_time = record;
411 Iterator<AltosOrderedRecord> iterator = records.iterator();
412 while (iterator.hasNext()) {
413 AltosOrderedRecord old = iterator.next();
414 if (old.cmd == Altos.AO_LOG_GPS_TIME &&
415 old.a == -1 && old.b == -1)
417 update_time(record, old);
420 missing_time = false;
424 if (record.cmd == Altos.AO_LOG_GPS_LAT) {
425 if (last_gps_time == null || last_gps_time.tick != record.tick) {
426 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(Altos.AO_LOG_GPS_TIME,
429 if (last_gps_time != null)
430 update_time(last_gps_time, add_gps_time);
434 records.add(add_gps_time);
435 record.index = index++;
440 /* Bail after reading the 'landed' record; we're all done */
441 if (record.cmd == Altos.AO_LOG_STATE &&
442 record.a == Altos.ao_flight_landed)
445 } catch (IOException io) {
446 } catch (ParseException pe) {
450 } catch (IOException ie) {