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 AltosEepromIterable extends AltosRecordIterable {
26 static final int seen_basic = AltosRecord.seen_flight|AltosRecord.seen_sensor;
32 AltosEepromRecord flight_record;
33 AltosEepromRecord gps_date_record;
35 TreeSet<AltosOrderedRecord> records;
37 LinkedList<AltosRecord> list;
55 void update_state(AltosRecordTM state, AltosEepromRecord record, EepromState eeprom) {
56 state.tick = record.tick;
58 case AltosLib.AO_LOG_FLIGHT:
59 eeprom.seen |= AltosRecord.seen_flight;
60 state.ground_accel = record.a;
61 state.flight_accel = record.a;
62 state.flight = record.b;
63 eeprom.boost_tick = record.tick;
65 case AltosLib.AO_LOG_SENSOR:
66 state.accel = record.a;
67 state.pres = record.b;
68 if (state.state < AltosLib.ao_flight_boost) {
69 eeprom.n_pad_samples++;
70 eeprom.ground_pres += state.pres;
71 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
72 state.flight_pres = state.ground_pres;
74 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
76 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
77 if ((eeprom.seen & AltosRecord.seen_sensor) == 0)
78 eeprom.sensor_tick = record.tick - 1;
79 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
80 eeprom.seen |= AltosRecord.seen_sensor;
81 eeprom.sensor_tick = record.tick;
84 case AltosLib.AO_LOG_PRESSURE:
85 state.pres = record.b;
86 state.flight_pres = state.pres;
87 if (eeprom.n_pad_samples == 0) {
88 eeprom.n_pad_samples++;
89 state.ground_pres = state.pres;
91 eeprom.seen |= AltosRecord.seen_sensor;
93 case AltosLib.AO_LOG_TEMP_VOLT:
94 state.temp = record.a;
95 state.batt = record.b;
96 eeprom.seen |= AltosRecord.seen_temp_volt;
98 case AltosLib.AO_LOG_DEPLOY:
99 state.drogue = record.a;
100 state.main = record.b;
101 eeprom.seen |= AltosRecord.seen_deploy;
104 case AltosLib.AO_LOG_STATE:
105 state.state = record.a;
107 case AltosLib.AO_LOG_GPS_TIME:
108 eeprom.gps_tick = state.tick;
109 AltosGPS old = state.gps;
110 state.gps = new AltosGPS();
112 /* GPS date doesn't get repeated through the file */
114 state.gps.year = old.year;
115 state.gps.month = old.month;
116 state.gps.day = old.day;
118 state.gps.hour = (record.a & 0xff);
119 state.gps.minute = (record.a >> 8);
120 state.gps.second = (record.b & 0xff);
122 int flags = (record.b >> 8);
123 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
124 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
125 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
126 AltosLib.AO_GPS_NUM_SAT_SHIFT;
127 state.new_gps = true;
130 case AltosLib.AO_LOG_GPS_LAT:
131 int lat32 = record.a | (record.b << 16);
132 state.gps.lat = (double) lat32 / 1e7;
134 case AltosLib.AO_LOG_GPS_LON:
135 int lon32 = record.a | (record.b << 16);
136 state.gps.lon = (double) lon32 / 1e7;
138 case AltosLib.AO_LOG_GPS_ALT:
139 state.gps.alt = record.a;
141 case AltosLib.AO_LOG_GPS_SAT:
142 if (state.tick == eeprom.gps_tick) {
144 int c_n0 = record.b >> 8;
145 state.gps.add_sat(svid, c_n0);
148 case AltosLib.AO_LOG_GPS_DATE:
149 state.gps.year = (record.a & 0xff) + 2000;
150 state.gps.month = record.a >> 8;
151 state.gps.day = record.b & 0xff;
154 case AltosLib.AO_LOG_CONFIG_VERSION:
156 case AltosLib.AO_LOG_MAIN_DEPLOY:
158 case AltosLib.AO_LOG_APOGEE_DELAY:
160 case AltosLib.AO_LOG_RADIO_CHANNEL:
162 case AltosLib.AO_LOG_CALLSIGN:
163 state.callsign = record.data;
165 case AltosLib.AO_LOG_ACCEL_CAL:
166 state.accel_plus_g = record.a;
167 state.accel_minus_g = record.b;
169 case AltosLib.AO_LOG_RADIO_CAL:
171 case AltosLib.AO_LOG_MANUFACTURER:
173 case AltosLib.AO_LOG_PRODUCT:
175 case AltosLib.AO_LOG_SERIAL_NUMBER:
176 state.serial = record.a;
178 case AltosLib.AO_LOG_SOFTWARE_VERSION:
181 state.seen |= eeprom.seen;
184 LinkedList<AltosRecord> make_list() {
185 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
186 Iterator<AltosOrderedRecord> iterator = records.iterator();
187 AltosOrderedRecord record = null;
188 AltosRecordTM state = new AltosRecordTM();
189 //boolean last_reported = false;
190 EepromState eeprom = new EepromState();
192 state.state = AltosLib.ao_flight_pad;
193 state.accel_plus_g = 15758;
194 state.accel_minus_g = 16294;
195 state.flight_vel = 0;
197 /* Pull in static data from the flight and gps_date records */
198 if (flight_record != null)
199 update_state(state, flight_record, eeprom);
200 if (gps_date_record != null)
201 update_state(state, gps_date_record, eeprom);
203 while (iterator.hasNext()) {
204 record = iterator.next();
205 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
206 AltosRecordTM r = state.clone();
207 r.time = (r.tick - eeprom.boost_tick) / 100.0;
210 update_state(state, record, eeprom);
212 AltosRecordTM r = state.clone();
213 r.time = (r.tick - eeprom.boost_tick) / 100.0;
218 public Iterator<AltosRecord> iterator() {
221 return list.iterator();
224 public boolean has_gps() { return has_gps; }
225 public boolean has_accel() { return has_accel; }
226 public boolean has_ignite() { return has_ignite; }
228 public void write_comments(PrintStream out) {
229 Iterator<AltosOrderedRecord> iterator = records.iterator();
230 out.printf("# Comments\n");
231 while (iterator.hasNext()) {
232 AltosOrderedRecord record = iterator.next();
233 switch (record.cmd) {
234 case AltosLib.AO_LOG_CONFIG_VERSION:
235 out.printf("# Config version: %s\n", record.data);
237 case AltosLib.AO_LOG_MAIN_DEPLOY:
238 out.printf("# Main deploy: %s\n", record.a);
240 case AltosLib.AO_LOG_APOGEE_DELAY:
241 out.printf("# Apogee delay: %s\n", record.a);
243 case AltosLib.AO_LOG_RADIO_CHANNEL:
244 out.printf("# Radio channel: %s\n", record.a);
246 case AltosLib.AO_LOG_CALLSIGN:
247 out.printf("# Callsign: %s\n", record.data);
249 case AltosLib.AO_LOG_ACCEL_CAL:
250 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
252 case AltosLib.AO_LOG_RADIO_CAL:
253 out.printf ("# Radio cal: %d\n", record.a);
255 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
256 out.printf ("# Max flight log: %d\n", record.a);
258 case AltosLib.AO_LOG_MANUFACTURER:
259 out.printf ("# Manufacturer: %s\n", record.data);
261 case AltosLib.AO_LOG_PRODUCT:
262 out.printf ("# Product: %s\n", record.data);
264 case AltosLib.AO_LOG_SERIAL_NUMBER:
265 out.printf ("# Serial number: %d\n", record.a);
267 case AltosLib.AO_LOG_SOFTWARE_VERSION:
268 out.printf ("# Software version: %s\n", record.data);
270 case AltosLib.AO_LOG_BARO_RESERVED:
271 out.printf ("# Baro reserved: %d\n", record.a);
273 case AltosLib.AO_LOG_BARO_SENS:
274 out.printf ("# Baro sens: %d\n", record.a);
276 case AltosLib.AO_LOG_BARO_OFF:
277 out.printf ("# Baro off: %d\n", record.a);
279 case AltosLib.AO_LOG_BARO_TCS:
280 out.printf ("# Baro tcs: %d\n", record.a);
282 case AltosLib.AO_LOG_BARO_TCO:
283 out.printf ("# Baro tco: %d\n", record.a);
285 case AltosLib.AO_LOG_BARO_TREF:
286 out.printf ("# Baro tref: %d\n", record.a);
288 case AltosLib.AO_LOG_BARO_TEMPSENS:
289 out.printf ("# Baro tempsens: %d\n", record.a);
291 case AltosLib.AO_LOG_BARO_CRC:
292 out.printf ("# Baro crc: %d\n", record.a);
299 * Given an AO_LOG_GPS_TIME record with correct time, and one
300 * missing time, rewrite the missing time values with the good
301 * ones, assuming that the difference between them is 'diff' seconds
303 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
305 int diff = (bad.tick - good.tick + 50) / 100;
307 int hour = (good.a & 0xff);
308 int minute = (good.a >> 8);
309 int second = (good.b & 0xff);
310 int flags = (good.b >> 8);
311 int seconds = hour * 3600 + minute * 60 + second;
313 /* Make sure this looks like a good GPS value */
314 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
315 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
316 flags |= AltosLib.AO_GPS_RUNNING;
317 flags |= AltosLib.AO_GPS_VALID;
319 int new_seconds = seconds + diff;
321 new_seconds += 24 * 3600;
322 int new_second = (new_seconds % 60);
323 int new_minutes = (new_seconds / 60);
324 int new_minute = (new_minutes % 60);
325 int new_hours = (new_minutes / 60);
326 int new_hour = (new_hours % 24);
328 bad.a = new_hour + (new_minute << 8);
329 bad.b = new_second + (flags << 8);
333 * Read the whole file, dumping records into a RB tree so
334 * we can enumerate them in time order -- the eeprom data
335 * are sometimes out of order with GPS data getting timestamps
336 * matching the first packet out of the GPS unit but not
337 * written until the final GPS packet has been received.
339 public AltosEepromIterable (FileInputStream input) {
340 records = new TreeSet<AltosOrderedRecord>();
342 AltosOrderedRecord last_gps_time = null;
346 boolean prev_tick_valid = false;
347 boolean missing_time = false;
351 String line = AltosLib.gets(input);
354 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
355 if (record.cmd == AltosLib.AO_LOG_INVALID)
357 prev_tick = record.tick;
358 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
359 prev_tick_valid = true;
360 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
361 flight_record = record;
365 /* Two firmware bugs caused the loss of some GPS data.
366 * The flight date would never be recorded, and often
367 * the flight time would get overwritten by another
368 * record. Detect the loss of the GPS date and fix up the
369 * missing time records
371 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
372 gps_date_record = record;
376 /* go back and fix up any missing time values */
377 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
378 last_gps_time = record;
380 Iterator<AltosOrderedRecord> iterator = records.iterator();
381 while (iterator.hasNext()) {
382 AltosOrderedRecord old = iterator.next();
383 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
384 old.a == -1 && old.b == -1)
386 update_time(record, old);
389 missing_time = false;
393 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
394 if (last_gps_time == null || last_gps_time.tick != record.tick) {
395 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
398 if (last_gps_time != null)
399 update_time(last_gps_time, add_gps_time);
403 records.add(add_gps_time);
404 record.index = index++;
409 /* Bail after reading the 'landed' record; we're all done */
410 if (record.cmd == AltosLib.AO_LOG_STATE &&
411 record.a == AltosLib.ao_flight_landed)
414 } catch (IOException io) {
415 } catch (ParseException pe) {
419 } catch (IOException ie) {