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;
32 * AltosRecords with an index field so they can be sorted by tick while preserving
33 * the original ordering for elements with matching ticks
35 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
39 public AltosOrderedRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
40 throws ParseException {
42 if (prev_tick_valid) {
43 tick |= (prev_tick & ~0xffff);
44 if (tick < prev_tick) {
45 if (prev_tick - tick > 0x8000)
48 if (tick - prev_tick > 0x8000)
55 public AltosOrderedRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
56 super(in_cmd, in_tick, in_a, in_b);
60 public int compareTo(AltosOrderedRecord o) {
61 int tick_diff = tick - o.tick;
64 return index - o.index;
68 public class AltosEepromIterable extends AltosRecordIterable {
70 static final int seen_flight = 1;
71 static final int seen_sensor = 2;
72 static final int seen_temp_volt = 4;
73 static final int seen_deploy = 8;
74 static final int seen_gps_time = 16;
75 static final int seen_gps_lat = 32;
76 static final int seen_gps_lon = 64;
78 static final int seen_basic = seen_flight|seen_sensor|seen_temp_volt|seen_deploy;
80 AltosEepromRecord flight_record;
81 AltosEepromRecord gps_date_record;
83 TreeSet<AltosOrderedRecord> records;
85 LinkedList<AltosRecord> list;
102 void update_state(AltosRecord state, AltosEepromRecord record, EepromState eeprom) {
103 state.tick = record.tick;
104 switch (record.cmd) {
105 case Altos.AO_LOG_FLIGHT:
106 eeprom.seen |= seen_flight;
107 state.ground_accel = record.a;
108 state.flight_accel = record.a;
109 state.flight = record.b;
110 eeprom.boost_tick = record.tick;
112 case Altos.AO_LOG_SENSOR:
113 state.accel = record.a;
114 state.pres = record.b;
115 if (state.state < Altos.ao_flight_boost) {
116 eeprom.n_pad_samples++;
117 eeprom.ground_pres += state.pres;
118 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
119 state.flight_pres = state.ground_pres;
121 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
122 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
123 state.flight_vel += (state.accel_plus_g - state.accel);
125 eeprom.seen |= seen_sensor;
127 case Altos.AO_LOG_TEMP_VOLT:
128 state.temp = record.a;
129 state.batt = record.b;
130 eeprom.seen |= seen_temp_volt;
132 case Altos.AO_LOG_DEPLOY:
133 state.drogue = record.a;
134 state.main = record.b;
135 eeprom.seen |= seen_deploy;
137 case Altos.AO_LOG_STATE:
138 state.state = record.a;
140 case Altos.AO_LOG_GPS_TIME:
141 eeprom.gps_tick = state.tick;
142 AltosGPS old = state.gps;
143 state.gps = new AltosGPS();
145 /* GPS date doesn't get repeated through the file */
147 state.gps.year = old.year;
148 state.gps.month = old.month;
149 state.gps.day = old.day;
151 state.gps.hour = (record.a & 0xff);
152 state.gps.minute = (record.a >> 8);
153 state.gps.second = (record.b & 0xff);
155 int flags = (record.b >> 8);
156 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
157 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
158 state.gps.date_valid = (flags & Altos.AO_GPS_DATE_VALID) != 0;
159 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
160 Altos.AO_GPS_NUM_SAT_SHIFT;
162 case Altos.AO_LOG_GPS_LAT:
163 int lat32 = record.a | (record.b << 16);
164 state.gps.lat = (double) lat32 / 1e7;
166 case Altos.AO_LOG_GPS_LON:
167 int lon32 = record.a | (record.b << 16);
168 state.gps.lon = (double) lon32 / 1e7;
170 case Altos.AO_LOG_GPS_ALT:
171 state.gps.alt = record.a;
173 case Altos.AO_LOG_GPS_SAT:
174 if (state.tick == eeprom.gps_tick) {
176 int c_n0 = record.b >> 8;
177 state.gps.add_sat(svid, c_n0);
180 case Altos.AO_LOG_GPS_DATE:
181 state.gps.year = (record.a & 0xff) + 2000;
182 state.gps.month = record.a >> 8;
183 state.gps.day = record.b & 0xff;
186 case Altos.AO_LOG_CONFIG_VERSION:
188 case Altos.AO_LOG_MAIN_DEPLOY:
190 case Altos.AO_LOG_APOGEE_DELAY:
192 case Altos.AO_LOG_RADIO_CHANNEL:
194 case Altos.AO_LOG_CALLSIGN:
195 state.callsign = record.data;
197 case Altos.AO_LOG_ACCEL_CAL:
198 state.accel_plus_g = record.a;
199 state.accel_minus_g = record.b;
201 case Altos.AO_LOG_RADIO_CAL:
203 case Altos.AO_LOG_MANUFACTURER:
205 case Altos.AO_LOG_PRODUCT:
207 case Altos.AO_LOG_SERIAL_NUMBER:
208 state.serial = record.a;
210 case Altos.AO_LOG_SOFTWARE_VERSION:
215 LinkedList<AltosRecord> make_list() {
216 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
217 Iterator<AltosOrderedRecord> iterator = records.iterator();
218 AltosOrderedRecord record = null;
219 AltosRecord state = new AltosRecord();
220 boolean last_reported = false;
221 EepromState eeprom = new EepromState();
223 state.state = Altos.ao_flight_pad;
224 state.accel_plus_g = 15758;
225 state.accel_minus_g = 16294;
227 /* Pull in static data from the flight and gps_date records */
228 if (flight_record != null)
229 update_state(state, flight_record, eeprom);
230 if (gps_date_record != null)
231 update_state(state, gps_date_record, eeprom);
233 while (iterator.hasNext()) {
234 record = iterator.next();
235 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
236 AltosRecord r = new AltosRecord(state);
237 r.time = (r.tick - eeprom.boost_tick) / 100.0;
240 update_state(state, record, eeprom);
242 AltosRecord r = new AltosRecord(state);
243 r.time = (r.tick - eeprom.boost_tick) / 100.0;
248 public Iterator<AltosRecord> iterator() {
251 return list.iterator();
254 public void write_comments(PrintStream out) {
255 Iterator<AltosOrderedRecord> iterator = records.iterator();
256 out.printf("# Comments\n");
257 while (iterator.hasNext()) {
258 AltosOrderedRecord record = iterator.next();
259 switch (record.cmd) {
260 case Altos.AO_LOG_CONFIG_VERSION:
261 out.printf("# Config version: %s\n", record.data);
263 case Altos.AO_LOG_MAIN_DEPLOY:
264 out.printf("# Main deploy: %s\n", record.a);
266 case Altos.AO_LOG_APOGEE_DELAY:
267 out.printf("# Apogee delay: %s\n", record.a);
269 case Altos.AO_LOG_RADIO_CHANNEL:
270 out.printf("# Radio channel: %s\n", record.a);
272 case Altos.AO_LOG_CALLSIGN:
273 out.printf("# Callsign: %s\n", record.data);
275 case Altos.AO_LOG_ACCEL_CAL:
276 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
278 case Altos.AO_LOG_RADIO_CAL:
279 out.printf ("# Radio cal: %d\n", record.a);
281 case Altos.AO_LOG_MANUFACTURER:
282 out.printf ("# Manufacturer: %s\n", record.data);
284 case Altos.AO_LOG_PRODUCT:
285 out.printf ("# Product: %s\n", record.data);
287 case Altos.AO_LOG_SERIAL_NUMBER:
288 out.printf ("# Serial number: %d\n", record.a);
290 case Altos.AO_LOG_SOFTWARE_VERSION:
291 out.printf ("# Software version: %s\n", record.data);
298 * Given an AO_LOG_GPS_TIME record with correct time, and one
299 * missing time, rewrite the missing time values with the good
300 * ones, assuming that the difference between them is 'diff' seconds
302 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
304 int diff = (bad.tick - good.tick + 50) / 100;
306 int hour = (good.a & 0xff);
307 int minute = (good.a >> 8);
308 int second = (good.b & 0xff);
309 int flags = (good.b >> 8);
310 int seconds = hour * 3600 + minute * 60 + second;
312 int new_seconds = seconds + diff;
314 new_seconds += 24 * 3600;
315 int new_second = (new_seconds % 60);
316 int new_minutes = (new_seconds / 60);
317 int new_minute = (new_minutes % 60);
318 int new_hours = (new_minutes / 60);
319 int new_hour = (new_hours % 24);
321 bad.a = new_hour + (new_minute << 8);
322 bad.b = new_second + (flags << 8);
326 * Read the whole file, dumping records into a RB tree so
327 * we can enumerate them in time order -- the eeprom data
328 * are sometimes out of order with GPS data getting timestamps
329 * matching the first packet out of the GPS unit but not
330 * written until the final GPS packet has been received.
332 public AltosEepromIterable (FileInputStream input) {
333 records = new TreeSet<AltosOrderedRecord>();
335 AltosOrderedRecord last_gps_time = null;
339 boolean prev_tick_valid = false;
340 boolean missing_time = false;
344 String line = AltosRecord.gets(input);
347 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
350 if (record.cmd == Altos.AO_LOG_INVALID)
352 prev_tick = record.tick;
353 if (record.cmd < Altos.AO_LOG_CONFIG_VERSION)
354 prev_tick_valid = true;
355 if (record.cmd == Altos.AO_LOG_FLIGHT) {
356 flight_record = record;
360 /* Two firmware bugs caused the loss of some GPS data.
361 * The flight date would never be recorded, and often
362 * the flight time would get overwritten by another
363 * record. Detect the loss of the GPS date and fix up the
364 * missing time records
366 if (record.cmd == Altos.AO_LOG_GPS_DATE) {
367 gps_date_record = record;
371 /* go back and fix up any missing time values */
372 if (record.cmd == Altos.AO_LOG_GPS_TIME) {
373 last_gps_time = record;
375 Iterator<AltosOrderedRecord> iterator = records.iterator();
376 while (iterator.hasNext()) {
377 AltosOrderedRecord old = iterator.next();
378 if (old.cmd == Altos.AO_LOG_GPS_TIME &&
379 old.a == -1 && old.b == -1)
381 update_time(record, old);
384 missing_time = false;
388 if (record.cmd == Altos.AO_LOG_GPS_LAT) {
389 if (last_gps_time == null || last_gps_time.tick != record.tick) {
390 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(Altos.AO_LOG_GPS_TIME,
393 if (last_gps_time != null)
394 update_time(last_gps_time, add_gps_time);
398 records.add(add_gps_time);
399 record.index = index++;
404 /* Bail after reading the 'landed' record; we're all done */
405 if (record.cmd == Altos.AO_LOG_STATE &&
406 record.a == Altos.ao_flight_landed)
409 } catch (IOException io) {
410 } catch (ParseException pe) {
414 } catch (IOException ie) {