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.
18 package org.altusmetrum.AltosLib;
23 import java.util.prefs.*;
24 import java.util.concurrent.LinkedBlockingQueue;
27 * AltosRecords with an index field so they can be sorted by tick while preserving
28 * the original ordering for elements with matching ticks
30 class AltosOrderedMegaRecord extends AltosEepromMega implements Comparable<AltosOrderedMegaRecord> {
34 public AltosOrderedMegaRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
35 throws ParseException {
37 if (prev_tick_valid) {
38 tick |= (prev_tick & ~0xffff);
39 if (tick < prev_tick) {
40 if (prev_tick - tick > 0x8000)
43 if (tick - prev_tick > 0x8000)
50 public AltosOrderedMegaRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
51 super(in_cmd, in_tick);
57 public String toString() {
58 return String.format("%d.%d %04x %04x %04x",
59 cmd, index, tick, a, b);
62 public int compareTo(AltosOrderedMegaRecord o) {
63 int tick_diff = tick - o.tick;
66 return index - o.index;
70 public class AltosEepromMegaIterable extends AltosRecordIterable {
72 static final int seen_flight = 1;
73 static final int seen_sensor = 2;
74 static final int seen_temp_volt = 4;
75 static final int seen_deploy = 8;
76 static final int seen_gps_time = 16;
77 static final int seen_gps_lat = 32;
78 static final int seen_gps_lon = 64;
80 static final int seen_basic = seen_flight|seen_sensor;
86 AltosEepromMega flight_record;
87 AltosEepromMega gps_date_record;
89 TreeSet<AltosOrderedMegaRecord> records;
93 LinkedList<AltosRecord> list;
111 void update_state(AltosRecordMM state, AltosEepromMega record, EepromState eeprom) {
112 state.tick = record.tick;
113 switch (record.cmd) {
114 case AltosLib.AO_LOG_FLIGHT:
115 eeprom.seen |= seen_flight;
116 state.ground_accel = record.ground_accel();
117 state.flight_accel = record.ground_accel();
118 state.ground_pres = baro.set(record.ground_pres(), record.ground_temp());
119 state.flight_pres = state.ground_pres;
120 state.flight = record.data16(0);
121 eeprom.boost_tick = record.tick;
123 case AltosLib.AO_LOG_SENSOR:
124 state.accel = record.accel();
125 baro.set(record.pres(), record.temp());
126 state.pres = baro.pa;
127 state.temp = baro.cc;
128 state.imu = new AltosIMU();
129 state.imu.accel_x = record.accel_x();
130 state.imu.accel_y = record.accel_y();
131 state.imu.accel_z = record.accel_z();
132 state.imu.gyro_x = record.gyro_x();
133 state.imu.gyro_y = record.gyro_y();
134 state.imu.gyro_z = record.gyro_z();
135 state.mag = new AltosMag();
136 state.mag.x = record.mag_x();
137 state.mag.y = record.mag_y();
138 state.mag.z = record.mag_z();
139 if (state.state < AltosLib.ao_flight_boost) {
140 eeprom.n_pad_samples++;
141 eeprom.ground_pres += state.pres;
142 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
143 state.flight_pres = state.ground_pres;
145 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
147 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
148 if ((eeprom.seen & seen_sensor) == 0)
149 eeprom.sensor_tick = record.tick - 1;
150 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
151 eeprom.seen |= seen_sensor;
152 eeprom.sensor_tick = record.tick;
155 case AltosLib.AO_LOG_PRESSURE:
156 state.pres = record.b;
157 state.flight_pres = state.pres;
158 if (eeprom.n_pad_samples == 0) {
159 eeprom.n_pad_samples++;
160 state.ground_pres = state.pres;
162 eeprom.seen |= seen_sensor;
164 case AltosLib.AO_LOG_TEMP_VOLT:
165 state.v_batt = record.v_batt();
166 state.v_pyro = record.v_pbatt();
167 for (int i = 0; i < AltosRecordMM.num_sense; i++)
168 state.sense[i] = record.sense(i);
169 eeprom.seen |= seen_temp_volt;
172 // case AltosLib.AO_LOG_DEPLOY:
173 // state.drogue = record.a;
174 // state.main = record.b;
175 // eeprom.seen |= seen_deploy;
176 // has_ignite = true;
179 case AltosLib.AO_LOG_STATE:
180 state.state = record.state();
182 case AltosLib.AO_LOG_GPS_TIME:
183 eeprom.gps_tick = state.tick;
184 AltosGPS old = state.gps;
185 state.gps = new AltosGPS();
187 /* GPS date doesn't get repeated through the file */
189 state.gps.year = old.year;
190 state.gps.month = old.month;
191 state.gps.day = old.day;
193 state.gps.hour = (record.a & 0xff);
194 state.gps.minute = (record.a >> 8);
195 state.gps.second = (record.b & 0xff);
197 int flags = (record.b >> 8);
198 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
199 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
200 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
201 AltosLib.AO_GPS_NUM_SAT_SHIFT;
202 state.new_gps = true;
205 case AltosLib.AO_LOG_GPS_LAT:
206 int lat32 = record.a | (record.b << 16);
207 state.gps.lat = (double) lat32 / 1e7;
209 case AltosLib.AO_LOG_GPS_LON:
210 int lon32 = record.a | (record.b << 16);
211 state.gps.lon = (double) lon32 / 1e7;
213 case AltosLib.AO_LOG_GPS_ALT:
214 state.gps.alt = record.a;
216 case AltosLib.AO_LOG_GPS_SAT:
217 if (state.tick == eeprom.gps_tick) {
219 int c_n0 = record.b >> 8;
220 state.gps.add_sat(svid, c_n0);
223 case AltosLib.AO_LOG_GPS_DATE:
224 state.gps.year = (record.a & 0xff) + 2000;
225 state.gps.month = record.a >> 8;
226 state.gps.day = record.b & 0xff;
229 case AltosLib.AO_LOG_CONFIG_VERSION:
231 case AltosLib.AO_LOG_MAIN_DEPLOY:
233 case AltosLib.AO_LOG_APOGEE_DELAY:
235 case AltosLib.AO_LOG_RADIO_CHANNEL:
237 case AltosLib.AO_LOG_CALLSIGN:
238 state.callsign = record.data;
240 case AltosLib.AO_LOG_ACCEL_CAL:
241 state.accel_plus_g = record.a;
242 state.accel_minus_g = record.b;
244 case AltosLib.AO_LOG_RADIO_CAL:
246 case AltosLib.AO_LOG_MANUFACTURER:
248 case AltosLib.AO_LOG_PRODUCT:
250 case AltosLib.AO_LOG_SERIAL_NUMBER:
251 state.serial = record.a;
253 case AltosLib.AO_LOG_SOFTWARE_VERSION:
255 case AltosLib.AO_LOG_BARO_RESERVED:
256 baro.reserved = record.a;
258 case AltosLib.AO_LOG_BARO_SENS:
261 case AltosLib.AO_LOG_BARO_OFF:
264 case AltosLib.AO_LOG_BARO_TCS:
267 case AltosLib.AO_LOG_BARO_TCO:
270 case AltosLib.AO_LOG_BARO_TREF:
273 case AltosLib.AO_LOG_BARO_TEMPSENS:
274 baro.tempsens =record.a;
276 case AltosLib.AO_LOG_BARO_CRC:
280 state.seen |= eeprom.seen;
283 LinkedList<AltosRecord> make_list() {
284 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
285 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
286 AltosOrderedMegaRecord record = null;
287 AltosRecordMM state = new AltosRecordMM();
288 boolean last_reported = false;
289 EepromState eeprom = new EepromState();
291 state.state = AltosLib.ao_flight_pad;
292 state.accel_plus_g = 15758;
293 state.accel_minus_g = 16294;
295 /* Pull in static data from the flight and gps_date records */
296 if (flight_record != null)
297 update_state(state, flight_record, eeprom);
298 if (gps_date_record != null)
299 update_state(state, gps_date_record, eeprom);
301 while (iterator.hasNext()) {
302 record = iterator.next();
303 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
304 AltosRecordMM r = state.clone();
305 r.time = (r.tick - eeprom.boost_tick) / 100.0;
308 update_state(state, record, eeprom);
310 AltosRecordMM r = state.clone();
311 r.time = (r.tick - eeprom.boost_tick) / 100.0;
316 public Iterator<AltosRecord> iterator() {
319 return list.iterator();
322 public boolean has_gps() { return has_gps; }
323 public boolean has_accel() { return has_accel; }
324 public boolean has_ignite() { return has_ignite; }
326 public void write_comments(PrintStream out) {
327 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
328 out.printf("# Comments\n");
329 while (iterator.hasNext()) {
330 AltosOrderedMegaRecord record = iterator.next();
331 switch (record.cmd) {
332 case AltosLib.AO_LOG_CONFIG_VERSION:
333 out.printf("# Config version: %s\n", record.data);
335 case AltosLib.AO_LOG_MAIN_DEPLOY:
336 out.printf("# Main deploy: %s\n", record.a);
338 case AltosLib.AO_LOG_APOGEE_DELAY:
339 out.printf("# Apogee delay: %s\n", record.a);
341 case AltosLib.AO_LOG_RADIO_CHANNEL:
342 out.printf("# Radio channel: %s\n", record.a);
344 case AltosLib.AO_LOG_CALLSIGN:
345 out.printf("# Callsign: %s\n", record.data);
347 case AltosLib.AO_LOG_ACCEL_CAL:
348 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
350 case AltosLib.AO_LOG_RADIO_CAL:
351 out.printf ("# Radio cal: %d\n", record.a);
353 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
354 out.printf ("# Max flight log: %d\n", record.a);
356 case AltosLib.AO_LOG_MANUFACTURER:
357 out.printf ("# Manufacturer: %s\n", record.data);
359 case AltosLib.AO_LOG_PRODUCT:
360 out.printf ("# Product: %s\n", record.data);
362 case AltosLib.AO_LOG_SERIAL_NUMBER:
363 out.printf ("# Serial number: %d\n", record.a);
365 case AltosLib.AO_LOG_SOFTWARE_VERSION:
366 out.printf ("# Software version: %s\n", record.data);
368 case AltosLib.AO_LOG_BARO_RESERVED:
369 out.printf ("# Baro reserved: %d\n", record.a);
371 case AltosLib.AO_LOG_BARO_SENS:
372 out.printf ("# Baro sens: %d\n", record.a);
374 case AltosLib.AO_LOG_BARO_OFF:
375 out.printf ("# Baro off: %d\n", record.a);
377 case AltosLib.AO_LOG_BARO_TCS:
378 out.printf ("# Baro tcs: %d\n", record.a);
380 case AltosLib.AO_LOG_BARO_TCO:
381 out.printf ("# Baro tco: %d\n", record.a);
383 case AltosLib.AO_LOG_BARO_TREF:
384 out.printf ("# Baro tref: %d\n", record.a);
386 case AltosLib.AO_LOG_BARO_TEMPSENS:
387 out.printf ("# Baro tempsens: %d\n", record.a);
389 case AltosLib.AO_LOG_BARO_CRC:
390 out.printf ("# Baro crc: %d\n", record.a);
397 * Given an AO_LOG_GPS_TIME record with correct time, and one
398 * missing time, rewrite the missing time values with the good
399 * ones, assuming that the difference between them is 'diff' seconds
401 void update_time(AltosOrderedMegaRecord good, AltosOrderedMegaRecord bad) {
403 int diff = (bad.tick - good.tick + 50) / 100;
405 int hour = (good.a & 0xff);
406 int minute = (good.a >> 8);
407 int second = (good.b & 0xff);
408 int flags = (good.b >> 8);
409 int seconds = hour * 3600 + minute * 60 + second;
411 /* Make sure this looks like a good GPS value */
412 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
413 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
414 flags |= AltosLib.AO_GPS_RUNNING;
415 flags |= AltosLib.AO_GPS_VALID;
417 int new_seconds = seconds + diff;
419 new_seconds += 24 * 3600;
420 int new_second = (new_seconds % 60);
421 int new_minutes = (new_seconds / 60);
422 int new_minute = (new_minutes % 60);
423 int new_hours = (new_minutes / 60);
424 int new_hour = (new_hours % 24);
426 bad.a = new_hour + (new_minute << 8);
427 bad.b = new_second + (flags << 8);
431 * Read the whole file, dumping records into a RB tree so
432 * we can enumerate them in time order -- the eeprom data
433 * are sometimes out of order with GPS data getting timestamps
434 * matching the first packet out of the GPS unit but not
435 * written until the final GPS packet has been received.
437 public AltosEepromMegaIterable (FileInputStream input) {
438 records = new TreeSet<AltosOrderedMegaRecord>();
440 AltosOrderedMegaRecord last_gps_time = null;
442 baro = new AltosMs5607();
446 boolean prev_tick_valid = false;
447 boolean missing_time = false;
451 String line = AltosLib.gets(input);
454 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(line, index++, prev_tick, prev_tick_valid);
457 if (record.cmd == AltosLib.AO_LOG_INVALID)
459 prev_tick = record.tick;
460 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
461 prev_tick_valid = true;
462 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
463 flight_record = record;
467 /* Two firmware bugs caused the loss of some GPS data.
468 * The flight date would never be recorded, and often
469 * the flight time would get overwritten by another
470 * record. Detect the loss of the GPS date and fix up the
471 * missing time records
473 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
474 gps_date_record = record;
478 /* go back and fix up any missing time values */
479 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
480 last_gps_time = record;
482 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
483 while (iterator.hasNext()) {
484 AltosOrderedMegaRecord old = iterator.next();
485 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
486 old.a == -1 && old.b == -1)
488 update_time(record, old);
491 missing_time = false;
495 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
496 if (last_gps_time == null || last_gps_time.tick != record.tick) {
497 AltosOrderedMegaRecord add_gps_time = new AltosOrderedMegaRecord(AltosLib.AO_LOG_GPS_TIME,
500 if (last_gps_time != null)
501 update_time(last_gps_time, add_gps_time);
505 records.add(add_gps_time);
506 record.index = index++;
511 /* Bail after reading the 'landed' record; we're all done */
512 if (record.cmd == AltosLib.AO_LOG_STATE &&
513 record.a == AltosLib.ao_flight_landed)
516 } catch (IOException io) {
517 } catch (ParseException pe) {
521 } catch (IOException ie) {