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;
24 public class AltosEepromMegaIterable extends AltosRecordIterable {
26 static final int seen_flight = 1;
27 static final int seen_sensor = 2;
28 static final int seen_temp_volt = 4;
29 static final int seen_deploy = 8;
30 static final int seen_gps_time = 16;
31 static final int seen_gps_lat = 32;
32 static final int seen_gps_lon = 64;
34 static final int seen_basic = seen_flight|seen_sensor;
40 AltosEepromMega flight_record;
41 AltosEepromMega gps_date_record;
43 TreeSet<AltosOrderedMegaRecord> records;
47 LinkedList<AltosRecord> list;
65 void update_state(AltosRecordMM state, AltosEepromMega record, EepromState eeprom) {
66 state.tick = record.tick;
68 case AltosLib.AO_LOG_FLIGHT:
69 eeprom.seen |= seen_flight;
70 state.ground_accel = record.ground_accel();
71 state.flight_accel = record.ground_accel();
72 state.ground_pres = baro.set(record.ground_pres(), record.ground_temp());
73 state.flight_pres = state.ground_pres;
74 state.flight = record.data16(0);
75 eeprom.boost_tick = record.tick;
77 case AltosLib.AO_LOG_SENSOR:
78 state.accel = record.accel();
79 baro.set(record.pres(), record.temp());
82 state.imu = new AltosIMU();
83 state.imu.accel_x = record.accel_x();
84 state.imu.accel_y = record.accel_y();
85 state.imu.accel_z = record.accel_z();
86 state.imu.gyro_x = record.gyro_x();
87 state.imu.gyro_y = record.gyro_y();
88 state.imu.gyro_z = record.gyro_z();
89 state.mag = new AltosMag();
90 state.mag.x = record.mag_x();
91 state.mag.y = record.mag_y();
92 state.mag.z = record.mag_z();
93 if (state.state < AltosLib.ao_flight_boost) {
94 eeprom.n_pad_samples++;
95 eeprom.ground_pres += state.pres;
96 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
97 state.flight_pres = state.ground_pres;
99 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
101 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
102 if ((eeprom.seen & seen_sensor) == 0)
103 eeprom.sensor_tick = record.tick - 1;
104 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
105 eeprom.seen |= seen_sensor;
106 eeprom.sensor_tick = record.tick;
109 case AltosLib.AO_LOG_PRESSURE:
110 state.pres = record.b;
111 state.flight_pres = state.pres;
112 if (eeprom.n_pad_samples == 0) {
113 eeprom.n_pad_samples++;
114 state.ground_pres = state.pres;
116 eeprom.seen |= seen_sensor;
118 case AltosLib.AO_LOG_TEMP_VOLT:
119 state.v_batt = record.v_batt();
120 state.v_pyro = record.v_pbatt();
121 for (int i = 0; i < AltosRecordMM.num_sense; i++)
122 state.sense[i] = record.sense(i);
123 eeprom.seen |= seen_temp_volt;
126 // case AltosLib.AO_LOG_DEPLOY:
127 // state.drogue = record.a;
128 // state.main = record.b;
129 // eeprom.seen |= seen_deploy;
130 // has_ignite = true;
133 case AltosLib.AO_LOG_STATE:
134 state.state = record.state();
136 case AltosLib.AO_LOG_GPS_TIME:
137 eeprom.gps_tick = state.tick;
138 AltosGPS old = state.gps;
139 state.gps = new AltosGPS();
141 /* GPS date doesn't get repeated through the file */
143 state.gps.year = old.year;
144 state.gps.month = old.month;
145 state.gps.day = old.day;
147 state.gps.hour = (record.a & 0xff);
148 state.gps.minute = (record.a >> 8);
149 state.gps.second = (record.b & 0xff);
151 int flags = (record.b >> 8);
152 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
153 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
154 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
155 AltosLib.AO_GPS_NUM_SAT_SHIFT;
156 state.new_gps = true;
159 case AltosLib.AO_LOG_GPS_LAT:
160 int lat32 = record.a | (record.b << 16);
161 state.gps.lat = (double) lat32 / 1e7;
163 case AltosLib.AO_LOG_GPS_LON:
164 int lon32 = record.a | (record.b << 16);
165 state.gps.lon = (double) lon32 / 1e7;
167 case AltosLib.AO_LOG_GPS_ALT:
168 state.gps.alt = record.a;
170 case AltosLib.AO_LOG_GPS_SAT:
171 if (state.tick == eeprom.gps_tick) {
173 int c_n0 = record.b >> 8;
174 state.gps.add_sat(svid, c_n0);
177 case AltosLib.AO_LOG_GPS_DATE:
178 state.gps.year = (record.a & 0xff) + 2000;
179 state.gps.month = record.a >> 8;
180 state.gps.day = record.b & 0xff;
183 case AltosLib.AO_LOG_CONFIG_VERSION:
185 case AltosLib.AO_LOG_MAIN_DEPLOY:
187 case AltosLib.AO_LOG_APOGEE_DELAY:
189 case AltosLib.AO_LOG_RADIO_CHANNEL:
191 case AltosLib.AO_LOG_CALLSIGN:
192 state.callsign = record.data;
194 case AltosLib.AO_LOG_ACCEL_CAL:
195 state.accel_plus_g = record.a;
196 state.accel_minus_g = record.b;
198 case AltosLib.AO_LOG_RADIO_CAL:
200 case AltosLib.AO_LOG_MANUFACTURER:
202 case AltosLib.AO_LOG_PRODUCT:
204 case AltosLib.AO_LOG_SERIAL_NUMBER:
205 state.serial = record.a;
207 case AltosLib.AO_LOG_SOFTWARE_VERSION:
209 case AltosLib.AO_LOG_BARO_RESERVED:
210 baro.reserved = record.a;
212 case AltosLib.AO_LOG_BARO_SENS:
215 case AltosLib.AO_LOG_BARO_OFF:
218 case AltosLib.AO_LOG_BARO_TCS:
221 case AltosLib.AO_LOG_BARO_TCO:
224 case AltosLib.AO_LOG_BARO_TREF:
227 case AltosLib.AO_LOG_BARO_TEMPSENS:
228 baro.tempsens =record.a;
230 case AltosLib.AO_LOG_BARO_CRC:
234 state.seen |= eeprom.seen;
237 LinkedList<AltosRecord> make_list() {
238 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
239 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
240 AltosOrderedMegaRecord record = null;
241 AltosRecordMM state = new AltosRecordMM();
242 //boolean last_reported = false;
243 EepromState eeprom = new EepromState();
245 state.state = AltosLib.ao_flight_pad;
246 state.accel_plus_g = 15758;
247 state.accel_minus_g = 16294;
249 /* Pull in static data from the flight and gps_date records */
250 if (flight_record != null)
251 update_state(state, flight_record, eeprom);
252 if (gps_date_record != null)
253 update_state(state, gps_date_record, eeprom);
255 while (iterator.hasNext()) {
256 record = iterator.next();
257 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
258 AltosRecordMM r = state.clone();
259 r.time = (r.tick - eeprom.boost_tick) / 100.0;
262 update_state(state, record, eeprom);
264 AltosRecordMM r = state.clone();
265 r.time = (r.tick - eeprom.boost_tick) / 100.0;
270 public Iterator<AltosRecord> iterator() {
273 return list.iterator();
276 public boolean has_gps() { return has_gps; }
277 public boolean has_accel() { return has_accel; }
278 public boolean has_ignite() { return has_ignite; }
280 public void write_comments(PrintStream out) {
281 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
282 out.printf("# Comments\n");
283 while (iterator.hasNext()) {
284 AltosOrderedMegaRecord record = iterator.next();
285 switch (record.cmd) {
286 case AltosLib.AO_LOG_CONFIG_VERSION:
287 out.printf("# Config version: %s\n", record.data);
289 case AltosLib.AO_LOG_MAIN_DEPLOY:
290 out.printf("# Main deploy: %s\n", record.a);
292 case AltosLib.AO_LOG_APOGEE_DELAY:
293 out.printf("# Apogee delay: %s\n", record.a);
295 case AltosLib.AO_LOG_RADIO_CHANNEL:
296 out.printf("# Radio channel: %s\n", record.a);
298 case AltosLib.AO_LOG_CALLSIGN:
299 out.printf("# Callsign: %s\n", record.data);
301 case AltosLib.AO_LOG_ACCEL_CAL:
302 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
304 case AltosLib.AO_LOG_RADIO_CAL:
305 out.printf ("# Radio cal: %d\n", record.a);
307 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
308 out.printf ("# Max flight log: %d\n", record.a);
310 case AltosLib.AO_LOG_MANUFACTURER:
311 out.printf ("# Manufacturer: %s\n", record.data);
313 case AltosLib.AO_LOG_PRODUCT:
314 out.printf ("# Product: %s\n", record.data);
316 case AltosLib.AO_LOG_SERIAL_NUMBER:
317 out.printf ("# Serial number: %d\n", record.a);
319 case AltosLib.AO_LOG_SOFTWARE_VERSION:
320 out.printf ("# Software version: %s\n", record.data);
322 case AltosLib.AO_LOG_BARO_RESERVED:
323 out.printf ("# Baro reserved: %d\n", record.a);
325 case AltosLib.AO_LOG_BARO_SENS:
326 out.printf ("# Baro sens: %d\n", record.a);
328 case AltosLib.AO_LOG_BARO_OFF:
329 out.printf ("# Baro off: %d\n", record.a);
331 case AltosLib.AO_LOG_BARO_TCS:
332 out.printf ("# Baro tcs: %d\n", record.a);
334 case AltosLib.AO_LOG_BARO_TCO:
335 out.printf ("# Baro tco: %d\n", record.a);
337 case AltosLib.AO_LOG_BARO_TREF:
338 out.printf ("# Baro tref: %d\n", record.a);
340 case AltosLib.AO_LOG_BARO_TEMPSENS:
341 out.printf ("# Baro tempsens: %d\n", record.a);
343 case AltosLib.AO_LOG_BARO_CRC:
344 out.printf ("# Baro crc: %d\n", record.a);
351 * Given an AO_LOG_GPS_TIME record with correct time, and one
352 * missing time, rewrite the missing time values with the good
353 * ones, assuming that the difference between them is 'diff' seconds
355 void update_time(AltosOrderedMegaRecord good, AltosOrderedMegaRecord bad) {
357 int diff = (bad.tick - good.tick + 50) / 100;
359 int hour = (good.a & 0xff);
360 int minute = (good.a >> 8);
361 int second = (good.b & 0xff);
362 int flags = (good.b >> 8);
363 int seconds = hour * 3600 + minute * 60 + second;
365 /* Make sure this looks like a good GPS value */
366 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
367 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
368 flags |= AltosLib.AO_GPS_RUNNING;
369 flags |= AltosLib.AO_GPS_VALID;
371 int new_seconds = seconds + diff;
373 new_seconds += 24 * 3600;
374 int new_second = (new_seconds % 60);
375 int new_minutes = (new_seconds / 60);
376 int new_minute = (new_minutes % 60);
377 int new_hours = (new_minutes / 60);
378 int new_hour = (new_hours % 24);
380 bad.a = new_hour + (new_minute << 8);
381 bad.b = new_second + (flags << 8);
385 * Read the whole file, dumping records into a RB tree so
386 * we can enumerate them in time order -- the eeprom data
387 * are sometimes out of order with GPS data getting timestamps
388 * matching the first packet out of the GPS unit but not
389 * written until the final GPS packet has been received.
391 public AltosEepromMegaIterable (FileInputStream input) {
392 records = new TreeSet<AltosOrderedMegaRecord>();
394 AltosOrderedMegaRecord last_gps_time = null;
396 baro = new AltosMs5607();
400 boolean prev_tick_valid = false;
401 boolean missing_time = false;
405 String line = AltosLib.gets(input);
408 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(line, index++, prev_tick, prev_tick_valid);
409 if (record.cmd == AltosLib.AO_LOG_INVALID)
411 prev_tick = record.tick;
412 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
413 prev_tick_valid = true;
414 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
415 flight_record = record;
419 /* Two firmware bugs caused the loss of some GPS data.
420 * The flight date would never be recorded, and often
421 * the flight time would get overwritten by another
422 * record. Detect the loss of the GPS date and fix up the
423 * missing time records
425 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
426 gps_date_record = record;
430 /* go back and fix up any missing time values */
431 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
432 last_gps_time = record;
434 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
435 while (iterator.hasNext()) {
436 AltosOrderedMegaRecord old = iterator.next();
437 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
438 old.a == -1 && old.b == -1)
440 update_time(record, old);
443 missing_time = false;
447 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
448 if (last_gps_time == null || last_gps_time.tick != record.tick) {
449 AltosOrderedMegaRecord add_gps_time = new AltosOrderedMegaRecord(AltosLib.AO_LOG_GPS_TIME,
452 if (last_gps_time != null)
453 update_time(last_gps_time, add_gps_time);
457 records.add(add_gps_time);
458 record.index = index++;
463 /* Bail after reading the 'landed' record; we're all done */
464 if (record.cmd == AltosLib.AO_LOG_STATE &&
465 record.a == AltosLib.ao_flight_landed)
468 } catch (IOException io) {
469 } catch (ParseException pe) {
473 } catch (IOException ie) {