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;
25 * AltosRecords with an index field so they can be sorted by tick while preserving
26 * the original ordering for elements with matching ticks
28 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
32 public AltosOrderedRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
33 throws ParseException {
35 if (prev_tick_valid) {
36 tick |= (prev_tick & ~0xffff);
37 if (tick < prev_tick) {
38 if (prev_tick - tick > 0x8000)
41 if (tick - prev_tick > 0x8000)
48 public AltosOrderedRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
49 super(in_cmd, in_tick, in_a, in_b);
53 public String toString() {
54 return String.format("%d.%d %04x %04x %04x",
55 cmd, index, tick, a, b);
58 public int compareTo(AltosOrderedRecord o) {
59 int tick_diff = tick - o.tick;
62 return index - o.index;
66 public class AltosEepromIterable extends AltosRecordIterable {
68 static final int seen_flight = 1;
69 static final int seen_sensor = 2;
70 static final int seen_temp_volt = 4;
71 static final int seen_deploy = 8;
72 static final int seen_gps_time = 16;
73 static final int seen_gps_lat = 32;
74 static final int seen_gps_lon = 64;
76 static final int seen_basic = seen_flight|seen_sensor;
82 AltosEepromRecord flight_record;
83 AltosEepromRecord gps_date_record;
85 TreeSet<AltosOrderedRecord> records;
87 LinkedList<AltosRecord> list;
105 void update_state(AltosRecordTM state, AltosEepromRecord record, EepromState eeprom) {
106 state.tick = record.tick;
107 switch (record.cmd) {
108 case AltosLib.AO_LOG_FLIGHT:
109 eeprom.seen |= seen_flight;
110 state.ground_accel = record.a;
111 state.flight_accel = record.a;
112 state.flight = record.b;
113 eeprom.boost_tick = record.tick;
115 case AltosLib.AO_LOG_SENSOR:
116 state.accel = record.a;
117 state.pres = record.b;
118 if (state.state < AltosLib.ao_flight_boost) {
119 eeprom.n_pad_samples++;
120 eeprom.ground_pres += state.pres;
121 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
122 state.flight_pres = state.ground_pres;
124 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
126 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
127 if ((eeprom.seen & seen_sensor) == 0)
128 eeprom.sensor_tick = record.tick - 1;
129 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
130 eeprom.seen |= seen_sensor;
131 eeprom.sensor_tick = record.tick;
134 case AltosLib.AO_LOG_PRESSURE:
135 state.pres = record.b;
136 state.flight_pres = state.pres;
137 if (eeprom.n_pad_samples == 0) {
138 eeprom.n_pad_samples++;
139 state.ground_pres = state.pres;
141 eeprom.seen |= seen_sensor;
143 case AltosLib.AO_LOG_TEMP_VOLT:
144 state.temp = record.a;
145 state.batt = record.b;
146 eeprom.seen |= seen_temp_volt;
148 case AltosLib.AO_LOG_DEPLOY:
149 state.drogue = record.a;
150 state.main = record.b;
151 eeprom.seen |= seen_deploy;
154 case AltosLib.AO_LOG_STATE:
155 state.state = record.a;
157 case AltosLib.AO_LOG_GPS_TIME:
158 eeprom.gps_tick = state.tick;
159 AltosGPS old = state.gps;
160 state.gps = new AltosGPS();
162 /* GPS date doesn't get repeated through the file */
164 state.gps.year = old.year;
165 state.gps.month = old.month;
166 state.gps.day = old.day;
168 state.gps.hour = (record.a & 0xff);
169 state.gps.minute = (record.a >> 8);
170 state.gps.second = (record.b & 0xff);
172 int flags = (record.b >> 8);
173 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
174 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
175 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
176 AltosLib.AO_GPS_NUM_SAT_SHIFT;
177 state.new_gps = true;
180 case AltosLib.AO_LOG_GPS_LAT:
181 int lat32 = record.a | (record.b << 16);
182 state.gps.lat = (double) lat32 / 1e7;
184 case AltosLib.AO_LOG_GPS_LON:
185 int lon32 = record.a | (record.b << 16);
186 state.gps.lon = (double) lon32 / 1e7;
188 case AltosLib.AO_LOG_GPS_ALT:
189 state.gps.alt = record.a;
191 case AltosLib.AO_LOG_GPS_SAT:
192 if (state.tick == eeprom.gps_tick) {
194 int c_n0 = record.b >> 8;
195 state.gps.add_sat(svid, c_n0);
198 case AltosLib.AO_LOG_GPS_DATE:
199 state.gps.year = (record.a & 0xff) + 2000;
200 state.gps.month = record.a >> 8;
201 state.gps.day = record.b & 0xff;
204 case AltosLib.AO_LOG_CONFIG_VERSION:
206 case AltosLib.AO_LOG_MAIN_DEPLOY:
208 case AltosLib.AO_LOG_APOGEE_DELAY:
210 case AltosLib.AO_LOG_RADIO_CHANNEL:
212 case AltosLib.AO_LOG_CALLSIGN:
213 state.callsign = record.data;
215 case AltosLib.AO_LOG_ACCEL_CAL:
216 state.accel_plus_g = record.a;
217 state.accel_minus_g = record.b;
219 case AltosLib.AO_LOG_RADIO_CAL:
221 case AltosLib.AO_LOG_MANUFACTURER:
223 case AltosLib.AO_LOG_PRODUCT:
225 case AltosLib.AO_LOG_SERIAL_NUMBER:
226 state.serial = record.a;
228 case AltosLib.AO_LOG_SOFTWARE_VERSION:
231 state.seen |= eeprom.seen;
234 LinkedList<AltosRecord> make_list() {
235 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
236 Iterator<AltosOrderedRecord> iterator = records.iterator();
237 AltosOrderedRecord record = null;
238 AltosRecordTM state = new AltosRecordTM();
239 boolean last_reported = false;
240 EepromState eeprom = new EepromState();
242 state.state = AltosLib.ao_flight_pad;
243 state.accel_plus_g = 15758;
244 state.accel_minus_g = 16294;
246 /* Pull in static data from the flight and gps_date records */
247 if (flight_record != null)
248 update_state(state, flight_record, eeprom);
249 if (gps_date_record != null)
250 update_state(state, gps_date_record, eeprom);
252 while (iterator.hasNext()) {
253 record = iterator.next();
254 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
255 AltosRecordTM r = state.clone();
256 r.time = (r.tick - eeprom.boost_tick) / 100.0;
259 update_state(state, record, eeprom);
261 AltosRecordTM r = state.clone();
262 r.time = (r.tick - eeprom.boost_tick) / 100.0;
267 public Iterator<AltosRecord> iterator() {
270 return list.iterator();
273 public boolean has_gps() { return has_gps; }
274 public boolean has_accel() { return has_accel; }
275 public boolean has_ignite() { return has_ignite; }
277 public void write_comments(PrintStream out) {
278 Iterator<AltosOrderedRecord> iterator = records.iterator();
279 out.printf("# Comments\n");
280 while (iterator.hasNext()) {
281 AltosOrderedRecord record = iterator.next();
282 switch (record.cmd) {
283 case AltosLib.AO_LOG_CONFIG_VERSION:
284 out.printf("# Config version: %s\n", record.data);
286 case AltosLib.AO_LOG_MAIN_DEPLOY:
287 out.printf("# Main deploy: %s\n", record.a);
289 case AltosLib.AO_LOG_APOGEE_DELAY:
290 out.printf("# Apogee delay: %s\n", record.a);
292 case AltosLib.AO_LOG_RADIO_CHANNEL:
293 out.printf("# Radio channel: %s\n", record.a);
295 case AltosLib.AO_LOG_CALLSIGN:
296 out.printf("# Callsign: %s\n", record.data);
298 case AltosLib.AO_LOG_ACCEL_CAL:
299 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
301 case AltosLib.AO_LOG_RADIO_CAL:
302 out.printf ("# Radio cal: %d\n", record.a);
304 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
305 out.printf ("# Max flight log: %d\n", record.a);
307 case AltosLib.AO_LOG_MANUFACTURER:
308 out.printf ("# Manufacturer: %s\n", record.data);
310 case AltosLib.AO_LOG_PRODUCT:
311 out.printf ("# Product: %s\n", record.data);
313 case AltosLib.AO_LOG_SERIAL_NUMBER:
314 out.printf ("# Serial number: %d\n", record.a);
316 case AltosLib.AO_LOG_SOFTWARE_VERSION:
317 out.printf ("# Software version: %s\n", record.data);
319 case AltosLib.AO_LOG_BARO_RESERVED:
320 out.printf ("# Baro reserved: %d\n", record.a);
322 case AltosLib.AO_LOG_BARO_SENS:
323 out.printf ("# Baro sens: %d\n", record.a);
325 case AltosLib.AO_LOG_BARO_OFF:
326 out.printf ("# Baro off: %d\n", record.a);
328 case AltosLib.AO_LOG_BARO_TCS:
329 out.printf ("# Baro tcs: %d\n", record.a);
331 case AltosLib.AO_LOG_BARO_TCO:
332 out.printf ("# Baro tco: %d\n", record.a);
334 case AltosLib.AO_LOG_BARO_TREF:
335 out.printf ("# Baro tref: %d\n", record.a);
337 case AltosLib.AO_LOG_BARO_TEMPSENS:
338 out.printf ("# Baro tempsens: %d\n", record.a);
340 case AltosLib.AO_LOG_BARO_CRC:
341 out.printf ("# Baro crc: %d\n", record.a);
348 * Given an AO_LOG_GPS_TIME record with correct time, and one
349 * missing time, rewrite the missing time values with the good
350 * ones, assuming that the difference between them is 'diff' seconds
352 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
354 int diff = (bad.tick - good.tick + 50) / 100;
356 int hour = (good.a & 0xff);
357 int minute = (good.a >> 8);
358 int second = (good.b & 0xff);
359 int flags = (good.b >> 8);
360 int seconds = hour * 3600 + minute * 60 + second;
362 /* Make sure this looks like a good GPS value */
363 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
364 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
365 flags |= AltosLib.AO_GPS_RUNNING;
366 flags |= AltosLib.AO_GPS_VALID;
368 int new_seconds = seconds + diff;
370 new_seconds += 24 * 3600;
371 int new_second = (new_seconds % 60);
372 int new_minutes = (new_seconds / 60);
373 int new_minute = (new_minutes % 60);
374 int new_hours = (new_minutes / 60);
375 int new_hour = (new_hours % 24);
377 bad.a = new_hour + (new_minute << 8);
378 bad.b = new_second + (flags << 8);
382 * Read the whole file, dumping records into a RB tree so
383 * we can enumerate them in time order -- the eeprom data
384 * are sometimes out of order with GPS data getting timestamps
385 * matching the first packet out of the GPS unit but not
386 * written until the final GPS packet has been received.
388 public AltosEepromIterable (FileInputStream input) {
389 records = new TreeSet<AltosOrderedRecord>();
391 AltosOrderedRecord last_gps_time = null;
395 boolean prev_tick_valid = false;
396 boolean missing_time = false;
400 String line = AltosLib.gets(input);
403 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
406 if (record.cmd == AltosLib.AO_LOG_INVALID)
408 prev_tick = record.tick;
409 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
410 prev_tick_valid = true;
411 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
412 flight_record = record;
416 /* Two firmware bugs caused the loss of some GPS data.
417 * The flight date would never be recorded, and often
418 * the flight time would get overwritten by another
419 * record. Detect the loss of the GPS date and fix up the
420 * missing time records
422 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
423 gps_date_record = record;
427 /* go back and fix up any missing time values */
428 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
429 last_gps_time = record;
431 Iterator<AltosOrderedRecord> iterator = records.iterator();
432 while (iterator.hasNext()) {
433 AltosOrderedRecord old = iterator.next();
434 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
435 old.a == -1 && old.b == -1)
437 update_time(record, old);
440 missing_time = false;
444 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
445 if (last_gps_time == null || last_gps_time.tick != record.tick) {
446 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
449 if (last_gps_time != null)
450 update_time(last_gps_time, add_gps_time);
454 records.add(add_gps_time);
455 record.index = index++;
460 /* Bail after reading the 'landed' record; we're all done */
461 if (record.cmd == AltosLib.AO_LOG_STATE &&
462 record.a == AltosLib.ao_flight_landed)
465 } catch (IOException io) {
466 } catch (ParseException pe) {
470 } catch (IOException ie) {