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 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 state.gps.lat = (double) lat32 / 1e7;
136 case AltosLib.AO_LOG_GPS_LON:
137 eeprom.seen |= AltosRecord.seen_gps_lon;
138 int lon32 = record.a | (record.b << 16);
139 state.gps.lon = (double) lon32 / 1e7;
141 case AltosLib.AO_LOG_GPS_ALT:
142 state.gps.alt = record.a;
144 case AltosLib.AO_LOG_GPS_SAT:
145 if (state.tick == eeprom.gps_tick) {
147 int c_n0 = record.b >> 8;
148 state.gps.add_sat(svid, c_n0);
151 case AltosLib.AO_LOG_GPS_DATE:
152 state.gps.year = (record.a & 0xff) + 2000;
153 state.gps.month = record.a >> 8;
154 state.gps.day = record.b & 0xff;
157 case AltosLib.AO_LOG_CONFIG_VERSION:
159 case AltosLib.AO_LOG_MAIN_DEPLOY:
161 case AltosLib.AO_LOG_APOGEE_DELAY:
163 case AltosLib.AO_LOG_RADIO_CHANNEL:
165 case AltosLib.AO_LOG_CALLSIGN:
166 state.callsign = record.data;
168 case AltosLib.AO_LOG_ACCEL_CAL:
169 state.accel_plus_g = record.a;
170 state.accel_minus_g = record.b;
172 case AltosLib.AO_LOG_RADIO_CAL:
174 case AltosLib.AO_LOG_MANUFACTURER:
176 case AltosLib.AO_LOG_PRODUCT:
178 case AltosLib.AO_LOG_SERIAL_NUMBER:
179 state.serial = record.a;
181 case AltosLib.AO_LOG_SOFTWARE_VERSION:
184 state.seen |= eeprom.seen;
187 LinkedList<AltosRecord> make_list() {
188 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
189 Iterator<AltosOrderedRecord> iterator = records.iterator();
190 AltosOrderedRecord record = null;
191 AltosRecordTM state = new AltosRecordTM();
192 //boolean last_reported = false;
193 EepromState eeprom = new EepromState();
195 state.state = AltosLib.ao_flight_pad;
196 state.accel_plus_g = 15758;
197 state.accel_minus_g = 16294;
198 state.flight_vel = 0;
200 /* Pull in static data from the flight and gps_date records */
201 if (flight_record != null)
202 update_state(state, flight_record, eeprom);
203 if (gps_date_record != null)
204 update_state(state, gps_date_record, eeprom);
206 while (iterator.hasNext()) {
207 record = iterator.next();
208 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
209 AltosRecordTM r = state.clone();
210 r.time = (r.tick - eeprom.boost_tick) / 100.0;
213 update_state(state, record, eeprom);
215 AltosRecordTM r = state.clone();
216 r.time = (r.tick - eeprom.boost_tick) / 100.0;
221 public Iterator<AltosRecord> iterator() {
224 return list.iterator();
227 public boolean has_gps() { return has_gps; }
228 public boolean has_accel() { return has_accel; }
229 public boolean has_ignite() { return has_ignite; }
231 public void write_comments(PrintStream out) {
232 Iterator<AltosOrderedRecord> iterator = records.iterator();
233 out.printf("# Comments\n");
234 while (iterator.hasNext()) {
235 AltosOrderedRecord record = iterator.next();
236 switch (record.cmd) {
237 case AltosLib.AO_LOG_CONFIG_VERSION:
238 out.printf("# Config version: %s\n", record.data);
240 case AltosLib.AO_LOG_MAIN_DEPLOY:
241 out.printf("# Main deploy: %s\n", record.a);
243 case AltosLib.AO_LOG_APOGEE_DELAY:
244 out.printf("# Apogee delay: %s\n", record.a);
246 case AltosLib.AO_LOG_RADIO_CHANNEL:
247 out.printf("# Radio channel: %s\n", record.a);
249 case AltosLib.AO_LOG_CALLSIGN:
250 out.printf("# Callsign: %s\n", record.data);
252 case AltosLib.AO_LOG_ACCEL_CAL:
253 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
255 case AltosLib.AO_LOG_RADIO_CAL:
256 out.printf ("# Radio cal: %d\n", record.a);
258 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
259 out.printf ("# Max flight log: %d\n", record.a);
261 case AltosLib.AO_LOG_MANUFACTURER:
262 out.printf ("# Manufacturer: %s\n", record.data);
264 case AltosLib.AO_LOG_PRODUCT:
265 out.printf ("# Product: %s\n", record.data);
267 case AltosLib.AO_LOG_SERIAL_NUMBER:
268 out.printf ("# Serial number: %d\n", record.a);
270 case AltosLib.AO_LOG_SOFTWARE_VERSION:
271 out.printf ("# Software version: %s\n", record.data);
273 case AltosLib.AO_LOG_BARO_RESERVED:
274 out.printf ("# Baro reserved: %d\n", record.a);
276 case AltosLib.AO_LOG_BARO_SENS:
277 out.printf ("# Baro sens: %d\n", record.a);
279 case AltosLib.AO_LOG_BARO_OFF:
280 out.printf ("# Baro off: %d\n", record.a);
282 case AltosLib.AO_LOG_BARO_TCS:
283 out.printf ("# Baro tcs: %d\n", record.a);
285 case AltosLib.AO_LOG_BARO_TCO:
286 out.printf ("# Baro tco: %d\n", record.a);
288 case AltosLib.AO_LOG_BARO_TREF:
289 out.printf ("# Baro tref: %d\n", record.a);
291 case AltosLib.AO_LOG_BARO_TEMPSENS:
292 out.printf ("# Baro tempsens: %d\n", record.a);
294 case AltosLib.AO_LOG_BARO_CRC:
295 out.printf ("# Baro crc: %d\n", record.a);
302 * Given an AO_LOG_GPS_TIME record with correct time, and one
303 * missing time, rewrite the missing time values with the good
304 * ones, assuming that the difference between them is 'diff' seconds
306 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
308 int diff = (bad.tick - good.tick + 50) / 100;
310 int hour = (good.a & 0xff);
311 int minute = (good.a >> 8);
312 int second = (good.b & 0xff);
313 int flags = (good.b >> 8);
314 int seconds = hour * 3600 + minute * 60 + second;
316 /* Make sure this looks like a good GPS value */
317 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
318 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
319 flags |= AltosLib.AO_GPS_RUNNING;
320 flags |= AltosLib.AO_GPS_VALID;
322 int new_seconds = seconds + diff;
324 new_seconds += 24 * 3600;
325 int new_second = (new_seconds % 60);
326 int new_minutes = (new_seconds / 60);
327 int new_minute = (new_minutes % 60);
328 int new_hours = (new_minutes / 60);
329 int new_hour = (new_hours % 24);
331 bad.a = new_hour + (new_minute << 8);
332 bad.b = new_second + (flags << 8);
336 * Read the whole file, dumping records into a RB tree so
337 * we can enumerate them in time order -- the eeprom data
338 * are sometimes out of order with GPS data getting timestamps
339 * matching the first packet out of the GPS unit but not
340 * written until the final GPS packet has been received.
342 public AltosEepromIterable (FileInputStream input) {
343 records = new TreeSet<AltosOrderedRecord>();
345 AltosOrderedRecord last_gps_time = null;
349 boolean prev_tick_valid = false;
350 boolean missing_time = false;
354 String line = AltosLib.gets(input);
357 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
358 if (record.cmd == AltosLib.AO_LOG_INVALID)
360 prev_tick = record.tick;
361 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
362 prev_tick_valid = true;
363 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
364 flight_record = record;
368 /* Two firmware bugs caused the loss of some GPS data.
369 * The flight date would never be recorded, and often
370 * the flight time would get overwritten by another
371 * record. Detect the loss of the GPS date and fix up the
372 * missing time records
374 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
375 gps_date_record = record;
379 /* go back and fix up any missing time values */
380 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
381 last_gps_time = record;
383 Iterator<AltosOrderedRecord> iterator = records.iterator();
384 while (iterator.hasNext()) {
385 AltosOrderedRecord old = iterator.next();
386 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
387 old.a == -1 && old.b == -1)
389 update_time(record, old);
392 missing_time = false;
396 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
397 if (last_gps_time == null || last_gps_time.tick != record.tick) {
398 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
401 if (last_gps_time != null)
402 update_time(last_gps_time, add_gps_time);
406 records.add(add_gps_time);
407 record.index = index++;
412 /* Bail after reading the 'landed' record; we're all done */
413 if (record.cmd == AltosLib.AO_LOG_STATE &&
414 record.a == AltosLib.ao_flight_landed)
417 } catch (IOException io) {
418 } catch (ParseException pe) {
422 } catch (IOException ie) {