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_1;
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 eeprom.seen |= AltosRecord.seen_gps_time;
110 AltosGPS old = state.gps;
111 state.gps = new AltosGPS();
113 /* GPS date doesn't get repeated through the file */
115 state.gps.year = old.year;
116 state.gps.month = old.month;
117 state.gps.day = old.day;
119 state.gps.hour = (record.a & 0xff);
120 state.gps.minute = (record.a >> 8);
121 state.gps.second = (record.b & 0xff);
123 int flags = (record.b >> 8);
124 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
125 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
126 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
127 AltosLib.AO_GPS_NUM_SAT_SHIFT;
128 state.new_gps = true;
131 case AltosLib.AO_LOG_GPS_LAT:
132 eeprom.seen |= AltosRecord.seen_gps_lat;
133 int lat32 = record.a | (record.b << 16);
134 if (state.gps == null)
135 state.gps = new AltosGPS();
136 state.gps.lat = (double) lat32 / 1e7;
138 case AltosLib.AO_LOG_GPS_LON:
139 eeprom.seen |= AltosRecord.seen_gps_lon;
140 int lon32 = record.a | (record.b << 16);
141 if (state.gps == null)
142 state.gps = new AltosGPS();
143 state.gps.lon = (double) lon32 / 1e7;
145 case AltosLib.AO_LOG_GPS_ALT:
146 if (state.gps == null)
147 state.gps = new AltosGPS();
148 state.gps.alt = record.a;
150 case AltosLib.AO_LOG_GPS_SAT:
151 if (state.tick == eeprom.gps_tick) {
153 int c_n0 = record.b >> 8;
154 if (state.gps == null)
155 state.gps = new AltosGPS();
156 state.gps.add_sat(svid, c_n0);
159 case AltosLib.AO_LOG_GPS_DATE:
160 if (state.gps == null)
161 state.gps = new AltosGPS();
162 state.gps.year = (record.a & 0xff) + 2000;
163 state.gps.month = record.a >> 8;
164 state.gps.day = record.b & 0xff;
167 case AltosLib.AO_LOG_CONFIG_VERSION:
169 case AltosLib.AO_LOG_MAIN_DEPLOY:
171 case AltosLib.AO_LOG_APOGEE_DELAY:
173 case AltosLib.AO_LOG_RADIO_CHANNEL:
175 case AltosLib.AO_LOG_CALLSIGN:
176 state.callsign = record.data;
178 case AltosLib.AO_LOG_ACCEL_CAL:
179 state.accel_plus_g = record.a;
180 state.accel_minus_g = record.b;
182 case AltosLib.AO_LOG_RADIO_CAL:
184 case AltosLib.AO_LOG_MANUFACTURER:
186 case AltosLib.AO_LOG_PRODUCT:
188 case AltosLib.AO_LOG_SERIAL_NUMBER:
189 state.serial = record.a;
191 case AltosLib.AO_LOG_SOFTWARE_VERSION:
194 state.seen |= eeprom.seen;
197 LinkedList<AltosRecord> make_list() {
198 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
199 Iterator<AltosOrderedRecord> iterator = records.iterator();
200 AltosOrderedRecord record = null;
201 AltosRecordTM state = new AltosRecordTM();
202 //boolean last_reported = false;
203 EepromState eeprom = new EepromState();
205 state.state = AltosLib.ao_flight_pad;
206 state.accel_plus_g = 15758;
207 state.accel_minus_g = 16294;
208 state.flight_vel = 0;
210 /* Pull in static data from the flight and gps_date records */
211 if (flight_record != null)
212 update_state(state, flight_record, eeprom);
213 if (gps_date_record != null)
214 update_state(state, gps_date_record, eeprom);
216 while (iterator.hasNext()) {
217 record = iterator.next();
218 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
219 AltosRecordTM r = state.clone();
220 r.time = (r.tick - eeprom.boost_tick) / 100.0;
223 update_state(state, record, eeprom);
225 AltosRecordTM r = state.clone();
226 r.time = (r.tick - eeprom.boost_tick) / 100.0;
231 public Iterator<AltosRecord> iterator() {
234 return list.iterator();
237 public boolean has_gps() { return has_gps; }
238 public boolean has_accel() { return has_accel; }
239 public boolean has_ignite() { return has_ignite; }
241 public void write_comments(PrintStream out) {
242 Iterator<AltosOrderedRecord> iterator = records.iterator();
243 out.printf("# Comments\n");
244 while (iterator.hasNext()) {
245 AltosOrderedRecord record = iterator.next();
246 switch (record.cmd) {
247 case AltosLib.AO_LOG_CONFIG_VERSION:
248 out.printf("# Config version: %s\n", record.data);
250 case AltosLib.AO_LOG_MAIN_DEPLOY:
251 out.printf("# Main deploy: %s\n", record.a);
253 case AltosLib.AO_LOG_APOGEE_DELAY:
254 out.printf("# Apogee delay: %s\n", record.a);
256 case AltosLib.AO_LOG_RADIO_CHANNEL:
257 out.printf("# Radio channel: %s\n", record.a);
259 case AltosLib.AO_LOG_CALLSIGN:
260 out.printf("# Callsign: %s\n", record.data);
262 case AltosLib.AO_LOG_ACCEL_CAL:
263 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
265 case AltosLib.AO_LOG_RADIO_CAL:
266 out.printf ("# Radio cal: %d\n", record.a);
268 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
269 out.printf ("# Max flight log: %d\n", record.a);
271 case AltosLib.AO_LOG_MANUFACTURER:
272 out.printf ("# Manufacturer: %s\n", record.data);
274 case AltosLib.AO_LOG_PRODUCT:
275 out.printf ("# Product: %s\n", record.data);
277 case AltosLib.AO_LOG_SERIAL_NUMBER:
278 out.printf ("# Serial number: %d\n", record.a);
280 case AltosLib.AO_LOG_SOFTWARE_VERSION:
281 out.printf ("# Software version: %s\n", record.data);
283 case AltosLib.AO_LOG_BARO_RESERVED:
284 out.printf ("# Baro reserved: %d\n", record.a);
286 case AltosLib.AO_LOG_BARO_SENS:
287 out.printf ("# Baro sens: %d\n", record.a);
289 case AltosLib.AO_LOG_BARO_OFF:
290 out.printf ("# Baro off: %d\n", record.a);
292 case AltosLib.AO_LOG_BARO_TCS:
293 out.printf ("# Baro tcs: %d\n", record.a);
295 case AltosLib.AO_LOG_BARO_TCO:
296 out.printf ("# Baro tco: %d\n", record.a);
298 case AltosLib.AO_LOG_BARO_TREF:
299 out.printf ("# Baro tref: %d\n", record.a);
301 case AltosLib.AO_LOG_BARO_TEMPSENS:
302 out.printf ("# Baro tempsens: %d\n", record.a);
304 case AltosLib.AO_LOG_BARO_CRC:
305 out.printf ("# Baro crc: %d\n", record.a);
312 * Given an AO_LOG_GPS_TIME record with correct time, and one
313 * missing time, rewrite the missing time values with the good
314 * ones, assuming that the difference between them is 'diff' seconds
316 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
318 int diff = (bad.tick - good.tick + 50) / 100;
320 int hour = (good.a & 0xff);
321 int minute = (good.a >> 8);
322 int second = (good.b & 0xff);
323 int flags = (good.b >> 8);
324 int seconds = hour * 3600 + minute * 60 + second;
326 /* Make sure this looks like a good GPS value */
327 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
328 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
329 flags |= AltosLib.AO_GPS_RUNNING;
330 flags |= AltosLib.AO_GPS_VALID;
332 int new_seconds = seconds + diff;
334 new_seconds += 24 * 3600;
335 int new_second = (new_seconds % 60);
336 int new_minutes = (new_seconds / 60);
337 int new_minute = (new_minutes % 60);
338 int new_hours = (new_minutes / 60);
339 int new_hour = (new_hours % 24);
341 bad.a = new_hour + (new_minute << 8);
342 bad.b = new_second + (flags << 8);
346 * Read the whole file, dumping records into a RB tree so
347 * we can enumerate them in time order -- the eeprom data
348 * are sometimes out of order with GPS data getting timestamps
349 * matching the first packet out of the GPS unit but not
350 * written until the final GPS packet has been received.
352 public AltosEepromIterable (FileInputStream input) {
353 records = new TreeSet<AltosOrderedRecord>();
355 AltosOrderedRecord last_gps_time = null;
359 boolean prev_tick_valid = false;
360 boolean missing_time = false;
364 String line = AltosLib.gets(input);
367 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
368 if (record.cmd == AltosLib.AO_LOG_INVALID)
370 prev_tick = record.tick;
371 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
372 prev_tick_valid = true;
373 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
374 flight_record = record;
378 /* Two firmware bugs caused the loss of some GPS data.
379 * The flight date would never be recorded, and often
380 * the flight time would get overwritten by another
381 * record. Detect the loss of the GPS date and fix up the
382 * missing time records
384 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
385 gps_date_record = record;
389 /* go back and fix up any missing time values */
390 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
391 last_gps_time = record;
393 Iterator<AltosOrderedRecord> iterator = records.iterator();
394 while (iterator.hasNext()) {
395 AltosOrderedRecord old = iterator.next();
396 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
397 old.a == -1 && old.b == -1)
399 update_time(record, old);
402 missing_time = false;
406 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
407 if (last_gps_time == null || last_gps_time.tick != record.tick) {
408 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
411 if (last_gps_time != null)
412 update_time(last_gps_time, add_gps_time);
416 records.add(add_gps_time);
417 record.index = index++;
422 /* Bail after reading the 'landed' record; we're all done */
423 if (record.cmd == AltosLib.AO_LOG_STATE &&
424 record.a == AltosLib.ao_flight_landed)
427 } catch (IOException io) {
428 } catch (ParseException pe) {
432 } catch (IOException ie) {