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 AltosOrderedMegaRecord extends AltosEepromMega implements Comparable<AltosOrderedMegaRecord> {
32 public AltosOrderedMegaRecord(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 AltosOrderedMegaRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
49 super(in_cmd, in_tick);
55 public String toString() {
56 return String.format("%d.%d %04x %04x %04x",
57 cmd, index, tick, a, b);
60 public int compareTo(AltosOrderedMegaRecord o) {
61 int tick_diff = tick - o.tick;
64 return index - o.index;
68 public class AltosEepromMegaIterable 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;
84 AltosEepromMega flight_record;
85 AltosEepromMega gps_date_record;
87 TreeSet<AltosOrderedMegaRecord> records;
91 LinkedList<AltosRecord> list;
109 void update_state(AltosRecordMM state, AltosEepromMega record, EepromState eeprom) {
110 state.tick = record.tick;
111 switch (record.cmd) {
112 case AltosLib.AO_LOG_FLIGHT:
113 eeprom.seen |= seen_flight;
114 state.ground_accel = record.ground_accel();
115 state.flight_accel = record.ground_accel();
116 state.ground_pres = baro.set(record.ground_pres(), record.ground_temp());
117 state.flight_pres = state.ground_pres;
118 state.flight = record.data16(0);
119 eeprom.boost_tick = record.tick;
121 case AltosLib.AO_LOG_SENSOR:
122 state.accel = record.accel();
123 baro.set(record.pres(), record.temp());
124 state.pres = baro.pa;
125 state.temp = baro.cc;
126 state.imu = new AltosIMU();
127 state.imu.accel_x = record.accel_x();
128 state.imu.accel_y = record.accel_y();
129 state.imu.accel_z = record.accel_z();
130 state.imu.gyro_x = record.gyro_x();
131 state.imu.gyro_y = record.gyro_y();
132 state.imu.gyro_z = record.gyro_z();
133 state.mag = new AltosMag();
134 state.mag.x = record.mag_x();
135 state.mag.y = record.mag_y();
136 state.mag.z = record.mag_z();
137 if (state.state < AltosLib.ao_flight_boost) {
138 eeprom.n_pad_samples++;
139 eeprom.ground_pres += state.pres;
140 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
141 state.flight_pres = state.ground_pres;
143 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
145 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
146 if ((eeprom.seen & seen_sensor) == 0)
147 eeprom.sensor_tick = record.tick - 1;
148 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
149 eeprom.seen |= seen_sensor;
150 eeprom.sensor_tick = record.tick;
153 case AltosLib.AO_LOG_PRESSURE:
154 state.pres = record.b;
155 state.flight_pres = state.pres;
156 if (eeprom.n_pad_samples == 0) {
157 eeprom.n_pad_samples++;
158 state.ground_pres = state.pres;
160 eeprom.seen |= seen_sensor;
162 case AltosLib.AO_LOG_TEMP_VOLT:
163 state.v_batt = record.v_batt();
164 state.v_pyro = record.v_pbatt();
165 for (int i = 0; i < AltosRecordMM.num_sense; i++)
166 state.sense[i] = record.sense(i);
167 eeprom.seen |= seen_temp_volt;
170 // case AltosLib.AO_LOG_DEPLOY:
171 // state.drogue = record.a;
172 // state.main = record.b;
173 // eeprom.seen |= seen_deploy;
174 // has_ignite = true;
177 case AltosLib.AO_LOG_STATE:
178 state.state = record.state();
180 case AltosLib.AO_LOG_GPS_TIME:
181 eeprom.gps_tick = state.tick;
182 AltosGPS old = state.gps;
183 state.gps = new AltosGPS();
185 /* GPS date doesn't get repeated through the file */
187 state.gps.year = old.year;
188 state.gps.month = old.month;
189 state.gps.day = old.day;
191 state.gps.hour = (record.a & 0xff);
192 state.gps.minute = (record.a >> 8);
193 state.gps.second = (record.b & 0xff);
195 int flags = (record.b >> 8);
196 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
197 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
198 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
199 AltosLib.AO_GPS_NUM_SAT_SHIFT;
200 state.new_gps = true;
203 case AltosLib.AO_LOG_GPS_LAT:
204 int lat32 = record.a | (record.b << 16);
205 state.gps.lat = (double) lat32 / 1e7;
207 case AltosLib.AO_LOG_GPS_LON:
208 int lon32 = record.a | (record.b << 16);
209 state.gps.lon = (double) lon32 / 1e7;
211 case AltosLib.AO_LOG_GPS_ALT:
212 state.gps.alt = record.a;
214 case AltosLib.AO_LOG_GPS_SAT:
215 if (state.tick == eeprom.gps_tick) {
217 int c_n0 = record.b >> 8;
218 state.gps.add_sat(svid, c_n0);
221 case AltosLib.AO_LOG_GPS_DATE:
222 state.gps.year = (record.a & 0xff) + 2000;
223 state.gps.month = record.a >> 8;
224 state.gps.day = record.b & 0xff;
227 case AltosLib.AO_LOG_CONFIG_VERSION:
229 case AltosLib.AO_LOG_MAIN_DEPLOY:
231 case AltosLib.AO_LOG_APOGEE_DELAY:
233 case AltosLib.AO_LOG_RADIO_CHANNEL:
235 case AltosLib.AO_LOG_CALLSIGN:
236 state.callsign = record.data;
238 case AltosLib.AO_LOG_ACCEL_CAL:
239 state.accel_plus_g = record.a;
240 state.accel_minus_g = record.b;
242 case AltosLib.AO_LOG_RADIO_CAL:
244 case AltosLib.AO_LOG_MANUFACTURER:
246 case AltosLib.AO_LOG_PRODUCT:
248 case AltosLib.AO_LOG_SERIAL_NUMBER:
249 state.serial = record.a;
251 case AltosLib.AO_LOG_SOFTWARE_VERSION:
253 case AltosLib.AO_LOG_BARO_RESERVED:
254 baro.reserved = record.a;
256 case AltosLib.AO_LOG_BARO_SENS:
259 case AltosLib.AO_LOG_BARO_OFF:
262 case AltosLib.AO_LOG_BARO_TCS:
265 case AltosLib.AO_LOG_BARO_TCO:
268 case AltosLib.AO_LOG_BARO_TREF:
271 case AltosLib.AO_LOG_BARO_TEMPSENS:
272 baro.tempsens =record.a;
274 case AltosLib.AO_LOG_BARO_CRC:
278 state.seen |= eeprom.seen;
281 LinkedList<AltosRecord> make_list() {
282 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
283 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
284 AltosOrderedMegaRecord record = null;
285 AltosRecordMM state = new AltosRecordMM();
286 //boolean last_reported = false;
287 EepromState eeprom = new EepromState();
289 state.state = AltosLib.ao_flight_pad;
290 state.accel_plus_g = 15758;
291 state.accel_minus_g = 16294;
293 /* Pull in static data from the flight and gps_date records */
294 if (flight_record != null)
295 update_state(state, flight_record, eeprom);
296 if (gps_date_record != null)
297 update_state(state, gps_date_record, eeprom);
299 while (iterator.hasNext()) {
300 record = iterator.next();
301 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
302 AltosRecordMM r = state.clone();
303 r.time = (r.tick - eeprom.boost_tick) / 100.0;
306 update_state(state, record, eeprom);
308 AltosRecordMM r = state.clone();
309 r.time = (r.tick - eeprom.boost_tick) / 100.0;
314 public Iterator<AltosRecord> iterator() {
317 return list.iterator();
320 public boolean has_gps() { return has_gps; }
321 public boolean has_accel() { return has_accel; }
322 public boolean has_ignite() { return has_ignite; }
324 public void write_comments(PrintStream out) {
325 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
326 out.printf("# Comments\n");
327 while (iterator.hasNext()) {
328 AltosOrderedMegaRecord record = iterator.next();
329 switch (record.cmd) {
330 case AltosLib.AO_LOG_CONFIG_VERSION:
331 out.printf("# Config version: %s\n", record.data);
333 case AltosLib.AO_LOG_MAIN_DEPLOY:
334 out.printf("# Main deploy: %s\n", record.a);
336 case AltosLib.AO_LOG_APOGEE_DELAY:
337 out.printf("# Apogee delay: %s\n", record.a);
339 case AltosLib.AO_LOG_RADIO_CHANNEL:
340 out.printf("# Radio channel: %s\n", record.a);
342 case AltosLib.AO_LOG_CALLSIGN:
343 out.printf("# Callsign: %s\n", record.data);
345 case AltosLib.AO_LOG_ACCEL_CAL:
346 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
348 case AltosLib.AO_LOG_RADIO_CAL:
349 out.printf ("# Radio cal: %d\n", record.a);
351 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
352 out.printf ("# Max flight log: %d\n", record.a);
354 case AltosLib.AO_LOG_MANUFACTURER:
355 out.printf ("# Manufacturer: %s\n", record.data);
357 case AltosLib.AO_LOG_PRODUCT:
358 out.printf ("# Product: %s\n", record.data);
360 case AltosLib.AO_LOG_SERIAL_NUMBER:
361 out.printf ("# Serial number: %d\n", record.a);
363 case AltosLib.AO_LOG_SOFTWARE_VERSION:
364 out.printf ("# Software version: %s\n", record.data);
366 case AltosLib.AO_LOG_BARO_RESERVED:
367 out.printf ("# Baro reserved: %d\n", record.a);
369 case AltosLib.AO_LOG_BARO_SENS:
370 out.printf ("# Baro sens: %d\n", record.a);
372 case AltosLib.AO_LOG_BARO_OFF:
373 out.printf ("# Baro off: %d\n", record.a);
375 case AltosLib.AO_LOG_BARO_TCS:
376 out.printf ("# Baro tcs: %d\n", record.a);
378 case AltosLib.AO_LOG_BARO_TCO:
379 out.printf ("# Baro tco: %d\n", record.a);
381 case AltosLib.AO_LOG_BARO_TREF:
382 out.printf ("# Baro tref: %d\n", record.a);
384 case AltosLib.AO_LOG_BARO_TEMPSENS:
385 out.printf ("# Baro tempsens: %d\n", record.a);
387 case AltosLib.AO_LOG_BARO_CRC:
388 out.printf ("# Baro crc: %d\n", record.a);
395 * Given an AO_LOG_GPS_TIME record with correct time, and one
396 * missing time, rewrite the missing time values with the good
397 * ones, assuming that the difference between them is 'diff' seconds
399 void update_time(AltosOrderedMegaRecord good, AltosOrderedMegaRecord bad) {
401 int diff = (bad.tick - good.tick + 50) / 100;
403 int hour = (good.a & 0xff);
404 int minute = (good.a >> 8);
405 int second = (good.b & 0xff);
406 int flags = (good.b >> 8);
407 int seconds = hour * 3600 + minute * 60 + second;
409 /* Make sure this looks like a good GPS value */
410 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
411 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
412 flags |= AltosLib.AO_GPS_RUNNING;
413 flags |= AltosLib.AO_GPS_VALID;
415 int new_seconds = seconds + diff;
417 new_seconds += 24 * 3600;
418 int new_second = (new_seconds % 60);
419 int new_minutes = (new_seconds / 60);
420 int new_minute = (new_minutes % 60);
421 int new_hours = (new_minutes / 60);
422 int new_hour = (new_hours % 24);
424 bad.a = new_hour + (new_minute << 8);
425 bad.b = new_second + (flags << 8);
429 * Read the whole file, dumping records into a RB tree so
430 * we can enumerate them in time order -- the eeprom data
431 * are sometimes out of order with GPS data getting timestamps
432 * matching the first packet out of the GPS unit but not
433 * written until the final GPS packet has been received.
435 public AltosEepromMegaIterable (FileInputStream input) {
436 records = new TreeSet<AltosOrderedMegaRecord>();
438 AltosOrderedMegaRecord last_gps_time = null;
440 baro = new AltosMs5607();
444 boolean prev_tick_valid = false;
445 boolean missing_time = false;
449 String line = AltosLib.gets(input);
452 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(line, index++, prev_tick, prev_tick_valid);
455 if (record.cmd == AltosLib.AO_LOG_INVALID)
457 prev_tick = record.tick;
458 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
459 prev_tick_valid = true;
460 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
461 flight_record = record;
465 /* Two firmware bugs caused the loss of some GPS data.
466 * The flight date would never be recorded, and often
467 * the flight time would get overwritten by another
468 * record. Detect the loss of the GPS date and fix up the
469 * missing time records
471 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
472 gps_date_record = record;
476 /* go back and fix up any missing time values */
477 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
478 last_gps_time = record;
480 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
481 while (iterator.hasNext()) {
482 AltosOrderedMegaRecord old = iterator.next();
483 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
484 old.a == -1 && old.b == -1)
486 update_time(record, old);
489 missing_time = false;
493 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
494 if (last_gps_time == null || last_gps_time.tick != record.tick) {
495 AltosOrderedMegaRecord add_gps_time = new AltosOrderedMegaRecord(AltosLib.AO_LOG_GPS_TIME,
498 if (last_gps_time != null)
499 update_time(last_gps_time, add_gps_time);
503 records.add(add_gps_time);
504 record.index = index++;
509 /* Bail after reading the 'landed' record; we're all done */
510 if (record.cmd == AltosLib.AO_LOG_STATE &&
511 record.a == AltosLib.ao_flight_landed)
514 } catch (IOException io) {
515 } catch (ParseException pe) {
519 } catch (IOException ie) {