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(AltosRecord 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 state.pres = baro.set(record.pres(), record.temp());
126 state.temp = baro.cc;
127 state.imu = new AltosIMU();
128 state.imu.accel_x = record.accel_x();
129 state.imu.accel_y = record.accel_y();
130 state.imu.accel_z = record.accel_z();
131 state.imu.gyro_x = record.gyro_x();
132 state.imu.gyro_y = record.gyro_y();
133 state.imu.gyro_z = record.gyro_z();
134 state.mag = new AltosMag();
135 state.mag.x = record.mag_x();
136 state.mag.y = record.mag_y();
137 state.mag.z = record.mag_z();
138 if (state.state < AltosLib.ao_flight_boost) {
139 eeprom.n_pad_samples++;
140 eeprom.ground_pres += state.pres;
141 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
142 state.flight_pres = state.ground_pres;
144 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
146 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
147 if ((eeprom.seen & seen_sensor) == 0)
148 eeprom.sensor_tick = record.tick - 1;
149 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
150 eeprom.seen |= seen_sensor;
151 eeprom.sensor_tick = record.tick;
154 case AltosLib.AO_LOG_PRESSURE:
155 state.pres = record.b;
156 state.flight_pres = state.pres;
157 if (eeprom.n_pad_samples == 0) {
158 eeprom.n_pad_samples++;
159 state.ground_pres = state.pres;
161 eeprom.seen |= seen_sensor;
163 case AltosLib.AO_LOG_TEMP_VOLT:
164 state.batt = record.v_batt();
165 eeprom.seen |= seen_temp_volt;
167 case AltosLib.AO_LOG_DEPLOY:
168 state.drogue = record.a;
169 state.main = record.b;
170 eeprom.seen |= seen_deploy;
173 case AltosLib.AO_LOG_STATE:
174 state.state = record.state();
176 case AltosLib.AO_LOG_GPS_TIME:
177 eeprom.gps_tick = state.tick;
178 AltosGPS old = state.gps;
179 state.gps = new AltosGPS();
181 /* GPS date doesn't get repeated through the file */
183 state.gps.year = old.year;
184 state.gps.month = old.month;
185 state.gps.day = old.day;
187 state.gps.hour = (record.a & 0xff);
188 state.gps.minute = (record.a >> 8);
189 state.gps.second = (record.b & 0xff);
191 int flags = (record.b >> 8);
192 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
193 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
194 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
195 AltosLib.AO_GPS_NUM_SAT_SHIFT;
196 state.new_gps = true;
199 case AltosLib.AO_LOG_GPS_LAT:
200 int lat32 = record.a | (record.b << 16);
201 state.gps.lat = (double) lat32 / 1e7;
203 case AltosLib.AO_LOG_GPS_LON:
204 int lon32 = record.a | (record.b << 16);
205 state.gps.lon = (double) lon32 / 1e7;
207 case AltosLib.AO_LOG_GPS_ALT:
208 state.gps.alt = record.a;
210 case AltosLib.AO_LOG_GPS_SAT:
211 if (state.tick == eeprom.gps_tick) {
213 int c_n0 = record.b >> 8;
214 state.gps.add_sat(svid, c_n0);
217 case AltosLib.AO_LOG_GPS_DATE:
218 state.gps.year = (record.a & 0xff) + 2000;
219 state.gps.month = record.a >> 8;
220 state.gps.day = record.b & 0xff;
223 case AltosLib.AO_LOG_CONFIG_VERSION:
225 case AltosLib.AO_LOG_MAIN_DEPLOY:
227 case AltosLib.AO_LOG_APOGEE_DELAY:
229 case AltosLib.AO_LOG_RADIO_CHANNEL:
231 case AltosLib.AO_LOG_CALLSIGN:
232 state.callsign = record.data;
234 case AltosLib.AO_LOG_ACCEL_CAL:
235 state.accel_plus_g = record.a;
236 state.accel_minus_g = record.b;
238 case AltosLib.AO_LOG_RADIO_CAL:
240 case AltosLib.AO_LOG_MANUFACTURER:
242 case AltosLib.AO_LOG_PRODUCT:
244 case AltosLib.AO_LOG_SERIAL_NUMBER:
245 state.serial = record.a;
247 case AltosLib.AO_LOG_SOFTWARE_VERSION:
249 case AltosLib.AO_LOG_BARO_RESERVED:
250 baro.reserved = record.a;
252 case AltosLib.AO_LOG_BARO_SENS:
255 case AltosLib.AO_LOG_BARO_OFF:
258 case AltosLib.AO_LOG_BARO_TCS:
261 case AltosLib.AO_LOG_BARO_TCO:
264 case AltosLib.AO_LOG_BARO_TREF:
267 case AltosLib.AO_LOG_BARO_TEMPSENS:
268 baro.tempsens =record.a;
270 case AltosLib.AO_LOG_BARO_CRC:
274 state.seen |= eeprom.seen;
277 LinkedList<AltosRecord> make_list() {
278 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
279 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
280 AltosOrderedMegaRecord record = null;
281 AltosRecord state = new AltosRecord();
282 boolean last_reported = false;
283 EepromState eeprom = new EepromState();
285 state.state = AltosLib.ao_flight_pad;
286 state.accel_plus_g = 15758;
287 state.accel_minus_g = 16294;
289 /* Pull in static data from the flight and gps_date records */
290 if (flight_record != null)
291 update_state(state, flight_record, eeprom);
292 if (gps_date_record != null)
293 update_state(state, gps_date_record, eeprom);
295 while (iterator.hasNext()) {
296 record = iterator.next();
297 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
298 AltosRecord r = new AltosRecord(state);
299 r.time = (r.tick - eeprom.boost_tick) / 100.0;
302 update_state(state, record, eeprom);
304 AltosRecord r = new AltosRecord(state);
305 r.time = (r.tick - eeprom.boost_tick) / 100.0;
310 public Iterator<AltosRecord> iterator() {
313 return list.iterator();
316 public boolean has_gps() { return has_gps; }
317 public boolean has_accel() { return has_accel; }
318 public boolean has_ignite() { return has_ignite; }
320 public void write_comments(PrintStream out) {
321 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
322 out.printf("# Comments\n");
323 while (iterator.hasNext()) {
324 AltosOrderedMegaRecord record = iterator.next();
325 switch (record.cmd) {
326 case AltosLib.AO_LOG_CONFIG_VERSION:
327 out.printf("# Config version: %s\n", record.data);
329 case AltosLib.AO_LOG_MAIN_DEPLOY:
330 out.printf("# Main deploy: %s\n", record.a);
332 case AltosLib.AO_LOG_APOGEE_DELAY:
333 out.printf("# Apogee delay: %s\n", record.a);
335 case AltosLib.AO_LOG_RADIO_CHANNEL:
336 out.printf("# Radio channel: %s\n", record.a);
338 case AltosLib.AO_LOG_CALLSIGN:
339 out.printf("# Callsign: %s\n", record.data);
341 case AltosLib.AO_LOG_ACCEL_CAL:
342 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
344 case AltosLib.AO_LOG_RADIO_CAL:
345 out.printf ("# Radio cal: %d\n", record.a);
347 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
348 out.printf ("# Max flight log: %d\n", record.a);
350 case AltosLib.AO_LOG_MANUFACTURER:
351 out.printf ("# Manufacturer: %s\n", record.data);
353 case AltosLib.AO_LOG_PRODUCT:
354 out.printf ("# Product: %s\n", record.data);
356 case AltosLib.AO_LOG_SERIAL_NUMBER:
357 out.printf ("# Serial number: %d\n", record.a);
359 case AltosLib.AO_LOG_SOFTWARE_VERSION:
360 out.printf ("# Software version: %s\n", record.data);
362 case AltosLib.AO_LOG_BARO_RESERVED:
363 out.printf ("# Baro reserved: %d\n", record.a);
365 case AltosLib.AO_LOG_BARO_SENS:
366 out.printf ("# Baro sens: %d\n", record.a);
368 case AltosLib.AO_LOG_BARO_OFF:
369 out.printf ("# Baro off: %d\n", record.a);
371 case AltosLib.AO_LOG_BARO_TCS:
372 out.printf ("# Baro tcs: %d\n", record.a);
374 case AltosLib.AO_LOG_BARO_TCO:
375 out.printf ("# Baro tco: %d\n", record.a);
377 case AltosLib.AO_LOG_BARO_TREF:
378 out.printf ("# Baro tref: %d\n", record.a);
380 case AltosLib.AO_LOG_BARO_TEMPSENS:
381 out.printf ("# Baro tempsens: %d\n", record.a);
383 case AltosLib.AO_LOG_BARO_CRC:
384 out.printf ("# Baro crc: %d\n", record.a);
391 * Given an AO_LOG_GPS_TIME record with correct time, and one
392 * missing time, rewrite the missing time values with the good
393 * ones, assuming that the difference between them is 'diff' seconds
395 void update_time(AltosOrderedMegaRecord good, AltosOrderedMegaRecord bad) {
397 int diff = (bad.tick - good.tick + 50) / 100;
399 int hour = (good.a & 0xff);
400 int minute = (good.a >> 8);
401 int second = (good.b & 0xff);
402 int flags = (good.b >> 8);
403 int seconds = hour * 3600 + minute * 60 + second;
405 /* Make sure this looks like a good GPS value */
406 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
407 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
408 flags |= AltosLib.AO_GPS_RUNNING;
409 flags |= AltosLib.AO_GPS_VALID;
411 int new_seconds = seconds + diff;
413 new_seconds += 24 * 3600;
414 int new_second = (new_seconds % 60);
415 int new_minutes = (new_seconds / 60);
416 int new_minute = (new_minutes % 60);
417 int new_hours = (new_minutes / 60);
418 int new_hour = (new_hours % 24);
420 bad.a = new_hour + (new_minute << 8);
421 bad.b = new_second + (flags << 8);
425 * Read the whole file, dumping records into a RB tree so
426 * we can enumerate them in time order -- the eeprom data
427 * are sometimes out of order with GPS data getting timestamps
428 * matching the first packet out of the GPS unit but not
429 * written until the final GPS packet has been received.
431 public AltosEepromMegaIterable (FileInputStream input) {
432 records = new TreeSet<AltosOrderedMegaRecord>();
434 AltosOrderedMegaRecord last_gps_time = null;
436 baro = new AltosMs5607();
440 boolean prev_tick_valid = false;
441 boolean missing_time = false;
445 String line = AltosRecord.gets(input);
448 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(line, index++, prev_tick, prev_tick_valid);
451 if (record.cmd == AltosLib.AO_LOG_INVALID)
453 prev_tick = record.tick;
454 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
455 prev_tick_valid = true;
456 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
457 flight_record = record;
461 /* Two firmware bugs caused the loss of some GPS data.
462 * The flight date would never be recorded, and often
463 * the flight time would get overwritten by another
464 * record. Detect the loss of the GPS date and fix up the
465 * missing time records
467 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
468 gps_date_record = record;
472 /* go back and fix up any missing time values */
473 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
474 last_gps_time = record;
476 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
477 while (iterator.hasNext()) {
478 AltosOrderedMegaRecord old = iterator.next();
479 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
480 old.a == -1 && old.b == -1)
482 update_time(record, old);
485 missing_time = false;
489 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
490 if (last_gps_time == null || last_gps_time.tick != record.tick) {
491 AltosOrderedMegaRecord add_gps_time = new AltosOrderedMegaRecord(AltosLib.AO_LOG_GPS_TIME,
494 if (last_gps_time != null)
495 update_time(last_gps_time, add_gps_time);
499 records.add(add_gps_time);
500 record.index = index++;
505 /* Bail after reading the 'landed' record; we're all done */
506 if (record.cmd == AltosLib.AO_LOG_STATE &&
507 record.a == AltosLib.ao_flight_landed)
510 } catch (IOException io) {
511 } catch (ParseException pe) {
515 } catch (IOException ie) {