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_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 AltosEepromRecord flight_record;
41 AltosEepromRecord gps_date_record;
43 TreeSet<AltosOrderedRecord> records;
45 LinkedList<AltosRecord> list;
63 void update_state(AltosRecordTM state, AltosEepromRecord record, EepromState eeprom) {
64 state.tick = record.tick;
66 case AltosLib.AO_LOG_FLIGHT:
67 eeprom.seen |= seen_flight;
68 state.ground_accel = record.a;
69 state.flight_accel = record.a;
70 state.flight = record.b;
71 eeprom.boost_tick = record.tick;
73 case AltosLib.AO_LOG_SENSOR:
74 state.accel = record.a;
75 state.pres = record.b;
76 if (state.state < AltosLib.ao_flight_boost) {
77 eeprom.n_pad_samples++;
78 eeprom.ground_pres += state.pres;
79 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
80 state.flight_pres = state.ground_pres;
82 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
84 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
85 if ((eeprom.seen & seen_sensor) == 0)
86 eeprom.sensor_tick = record.tick - 1;
87 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
88 eeprom.seen |= seen_sensor;
89 eeprom.sensor_tick = record.tick;
92 case AltosLib.AO_LOG_PRESSURE:
93 state.pres = record.b;
94 state.flight_pres = state.pres;
95 if (eeprom.n_pad_samples == 0) {
96 eeprom.n_pad_samples++;
97 state.ground_pres = state.pres;
99 eeprom.seen |= seen_sensor;
101 case AltosLib.AO_LOG_TEMP_VOLT:
102 state.temp = record.a;
103 state.batt = record.b;
104 eeprom.seen |= seen_temp_volt;
106 case AltosLib.AO_LOG_DEPLOY:
107 state.drogue = record.a;
108 state.main = record.b;
109 eeprom.seen |= seen_deploy;
112 case AltosLib.AO_LOG_STATE:
113 state.state = record.a;
115 case AltosLib.AO_LOG_GPS_TIME:
116 eeprom.gps_tick = state.tick;
117 AltosGPS old = state.gps;
118 state.gps = new AltosGPS();
120 /* GPS date doesn't get repeated through the file */
122 state.gps.year = old.year;
123 state.gps.month = old.month;
124 state.gps.day = old.day;
126 state.gps.hour = (record.a & 0xff);
127 state.gps.minute = (record.a >> 8);
128 state.gps.second = (record.b & 0xff);
130 int flags = (record.b >> 8);
131 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
132 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
133 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
134 AltosLib.AO_GPS_NUM_SAT_SHIFT;
135 state.new_gps = true;
138 case AltosLib.AO_LOG_GPS_LAT:
139 int lat32 = record.a | (record.b << 16);
140 state.gps.lat = (double) lat32 / 1e7;
142 case AltosLib.AO_LOG_GPS_LON:
143 int lon32 = record.a | (record.b << 16);
144 state.gps.lon = (double) lon32 / 1e7;
146 case AltosLib.AO_LOG_GPS_ALT:
147 state.gps.alt = record.a;
149 case AltosLib.AO_LOG_GPS_SAT:
150 if (state.tick == eeprom.gps_tick) {
152 int c_n0 = record.b >> 8;
153 state.gps.add_sat(svid, c_n0);
156 case AltosLib.AO_LOG_GPS_DATE:
157 state.gps.year = (record.a & 0xff) + 2000;
158 state.gps.month = record.a >> 8;
159 state.gps.day = record.b & 0xff;
162 case AltosLib.AO_LOG_CONFIG_VERSION:
164 case AltosLib.AO_LOG_MAIN_DEPLOY:
166 case AltosLib.AO_LOG_APOGEE_DELAY:
168 case AltosLib.AO_LOG_RADIO_CHANNEL:
170 case AltosLib.AO_LOG_CALLSIGN:
171 state.callsign = record.data;
173 case AltosLib.AO_LOG_ACCEL_CAL:
174 state.accel_plus_g = record.a;
175 state.accel_minus_g = record.b;
177 case AltosLib.AO_LOG_RADIO_CAL:
179 case AltosLib.AO_LOG_MANUFACTURER:
181 case AltosLib.AO_LOG_PRODUCT:
183 case AltosLib.AO_LOG_SERIAL_NUMBER:
184 state.serial = record.a;
186 case AltosLib.AO_LOG_SOFTWARE_VERSION:
189 state.seen |= eeprom.seen;
192 LinkedList<AltosRecord> make_list() {
193 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
194 Iterator<AltosOrderedRecord> iterator = records.iterator();
195 AltosOrderedRecord record = null;
196 AltosRecordTM state = new AltosRecordTM();
197 //boolean last_reported = false;
198 EepromState eeprom = new EepromState();
200 state.state = AltosLib.ao_flight_pad;
201 state.accel_plus_g = 15758;
202 state.accel_minus_g = 16294;
204 /* Pull in static data from the flight and gps_date records */
205 if (flight_record != null)
206 update_state(state, flight_record, eeprom);
207 if (gps_date_record != null)
208 update_state(state, gps_date_record, eeprom);
210 while (iterator.hasNext()) {
211 record = iterator.next();
212 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
213 AltosRecordTM r = state.clone();
214 r.time = (r.tick - eeprom.boost_tick) / 100.0;
217 update_state(state, record, eeprom);
219 AltosRecordTM r = state.clone();
220 r.time = (r.tick - eeprom.boost_tick) / 100.0;
225 public Iterator<AltosRecord> iterator() {
228 return list.iterator();
231 public boolean has_gps() { return has_gps; }
232 public boolean has_accel() { return has_accel; }
233 public boolean has_ignite() { return has_ignite; }
235 public void write_comments(PrintStream out) {
236 Iterator<AltosOrderedRecord> iterator = records.iterator();
237 out.printf("# Comments\n");
238 while (iterator.hasNext()) {
239 AltosOrderedRecord record = iterator.next();
240 switch (record.cmd) {
241 case AltosLib.AO_LOG_CONFIG_VERSION:
242 out.printf("# Config version: %s\n", record.data);
244 case AltosLib.AO_LOG_MAIN_DEPLOY:
245 out.printf("# Main deploy: %s\n", record.a);
247 case AltosLib.AO_LOG_APOGEE_DELAY:
248 out.printf("# Apogee delay: %s\n", record.a);
250 case AltosLib.AO_LOG_RADIO_CHANNEL:
251 out.printf("# Radio channel: %s\n", record.a);
253 case AltosLib.AO_LOG_CALLSIGN:
254 out.printf("# Callsign: %s\n", record.data);
256 case AltosLib.AO_LOG_ACCEL_CAL:
257 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
259 case AltosLib.AO_LOG_RADIO_CAL:
260 out.printf ("# Radio cal: %d\n", record.a);
262 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
263 out.printf ("# Max flight log: %d\n", record.a);
265 case AltosLib.AO_LOG_MANUFACTURER:
266 out.printf ("# Manufacturer: %s\n", record.data);
268 case AltosLib.AO_LOG_PRODUCT:
269 out.printf ("# Product: %s\n", record.data);
271 case AltosLib.AO_LOG_SERIAL_NUMBER:
272 out.printf ("# Serial number: %d\n", record.a);
274 case AltosLib.AO_LOG_SOFTWARE_VERSION:
275 out.printf ("# Software version: %s\n", record.data);
277 case AltosLib.AO_LOG_BARO_RESERVED:
278 out.printf ("# Baro reserved: %d\n", record.a);
280 case AltosLib.AO_LOG_BARO_SENS:
281 out.printf ("# Baro sens: %d\n", record.a);
283 case AltosLib.AO_LOG_BARO_OFF:
284 out.printf ("# Baro off: %d\n", record.a);
286 case AltosLib.AO_LOG_BARO_TCS:
287 out.printf ("# Baro tcs: %d\n", record.a);
289 case AltosLib.AO_LOG_BARO_TCO:
290 out.printf ("# Baro tco: %d\n", record.a);
292 case AltosLib.AO_LOG_BARO_TREF:
293 out.printf ("# Baro tref: %d\n", record.a);
295 case AltosLib.AO_LOG_BARO_TEMPSENS:
296 out.printf ("# Baro tempsens: %d\n", record.a);
298 case AltosLib.AO_LOG_BARO_CRC:
299 out.printf ("# Baro crc: %d\n", record.a);
306 * Given an AO_LOG_GPS_TIME record with correct time, and one
307 * missing time, rewrite the missing time values with the good
308 * ones, assuming that the difference between them is 'diff' seconds
310 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
312 int diff = (bad.tick - good.tick + 50) / 100;
314 int hour = (good.a & 0xff);
315 int minute = (good.a >> 8);
316 int second = (good.b & 0xff);
317 int flags = (good.b >> 8);
318 int seconds = hour * 3600 + minute * 60 + second;
320 /* Make sure this looks like a good GPS value */
321 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
322 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
323 flags |= AltosLib.AO_GPS_RUNNING;
324 flags |= AltosLib.AO_GPS_VALID;
326 int new_seconds = seconds + diff;
328 new_seconds += 24 * 3600;
329 int new_second = (new_seconds % 60);
330 int new_minutes = (new_seconds / 60);
331 int new_minute = (new_minutes % 60);
332 int new_hours = (new_minutes / 60);
333 int new_hour = (new_hours % 24);
335 bad.a = new_hour + (new_minute << 8);
336 bad.b = new_second + (flags << 8);
340 * Read the whole file, dumping records into a RB tree so
341 * we can enumerate them in time order -- the eeprom data
342 * are sometimes out of order with GPS data getting timestamps
343 * matching the first packet out of the GPS unit but not
344 * written until the final GPS packet has been received.
346 public AltosEepromIterable (FileInputStream input) {
347 records = new TreeSet<AltosOrderedRecord>();
349 AltosOrderedRecord last_gps_time = null;
353 boolean prev_tick_valid = false;
354 boolean missing_time = false;
358 String line = AltosLib.gets(input);
361 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
362 if (record.cmd == AltosLib.AO_LOG_INVALID)
364 prev_tick = record.tick;
365 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
366 prev_tick_valid = true;
367 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
368 flight_record = record;
372 /* Two firmware bugs caused the loss of some GPS data.
373 * The flight date would never be recorded, and often
374 * the flight time would get overwritten by another
375 * record. Detect the loss of the GPS date and fix up the
376 * missing time records
378 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
379 gps_date_record = record;
383 /* go back and fix up any missing time values */
384 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
385 last_gps_time = record;
387 Iterator<AltosOrderedRecord> iterator = records.iterator();
388 while (iterator.hasNext()) {
389 AltosOrderedRecord old = iterator.next();
390 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
391 old.a == -1 && old.b == -1)
393 update_time(record, old);
396 missing_time = false;
400 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
401 if (last_gps_time == null || last_gps_time.tick != record.tick) {
402 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
405 if (last_gps_time != null)
406 update_time(last_gps_time, add_gps_time);
410 records.add(add_gps_time);
411 record.index = index++;
416 /* Bail after reading the 'landed' record; we're all done */
417 if (record.cmd == AltosLib.AO_LOG_STATE &&
418 record.a == AltosLib.ao_flight_landed)
421 } catch (IOException io) {
422 } catch (ParseException pe) {
426 } catch (IOException ie) {