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;
23 import java.util.prefs.*;
24 import java.util.concurrent.LinkedBlockingQueue;
27 * AltosRecords with an index field so they can be sorted by tick while preserving
28 * the original ordering for elements with matching ticks
30 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
34 public AltosOrderedRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
35 throws ParseException {
37 if (prev_tick_valid) {
38 tick |= (prev_tick & ~0xffff);
39 if (tick < prev_tick) {
40 if (prev_tick - tick > 0x8000)
43 if (tick - prev_tick > 0x8000)
50 public AltosOrderedRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
51 super(in_cmd, in_tick, in_a, in_b);
55 public String toString() {
56 return String.format("%d.%d %04x %04x %04x",
57 cmd, index, tick, a, b);
60 public int compareTo(AltosOrderedRecord o) {
61 int tick_diff = tick - o.tick;
64 return index - o.index;
68 public class AltosEepromIterable extends AltosRecordIterable {
70 static final int seen_flight = 1;
71 static final int seen_sensor = 2;
72 static final int seen_temp_volt = 4;
73 static final int seen_deploy = 8;
74 static final int seen_gps_time = 16;
75 static final int seen_gps_lat = 32;
76 static final int seen_gps_lon = 64;
78 static final int seen_basic = seen_flight|seen_sensor;
84 AltosEepromRecord flight_record;
85 AltosEepromRecord gps_date_record;
87 TreeSet<AltosOrderedRecord> records;
89 LinkedList<AltosRecord> list;
106 void update_state(AltosRecord state, AltosEepromRecord record, EepromState eeprom) {
107 state.tick = record.tick;
108 switch (record.cmd) {
109 case AltosLib.AO_LOG_FLIGHT:
110 eeprom.seen |= seen_flight;
111 state.ground_accel = record.a;
112 state.flight_accel = record.a;
113 state.flight = record.b;
114 eeprom.boost_tick = record.tick;
116 case AltosLib.AO_LOG_SENSOR:
117 state.accel = record.a;
118 state.pres = record.b;
119 if (state.state < AltosLib.ao_flight_boost) {
120 eeprom.n_pad_samples++;
121 eeprom.ground_pres += state.pres;
122 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
123 state.flight_pres = state.ground_pres;
125 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
126 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
127 state.flight_vel += (state.accel_plus_g - state.accel);
129 eeprom.seen |= seen_sensor;
132 case AltosLib.AO_LOG_PRESSURE:
133 state.pres = record.b;
134 state.flight_pres = state.pres;
135 if (eeprom.n_pad_samples == 0) {
136 eeprom.n_pad_samples++;
137 state.ground_pres = state.pres;
139 eeprom.seen |= seen_sensor;
141 case AltosLib.AO_LOG_TEMP_VOLT:
142 state.temp = record.a;
143 state.batt = record.b;
144 eeprom.seen |= seen_temp_volt;
146 case AltosLib.AO_LOG_DEPLOY:
147 state.drogue = record.a;
148 state.main = record.b;
149 eeprom.seen |= seen_deploy;
152 case AltosLib.AO_LOG_STATE:
153 state.state = record.a;
155 case AltosLib.AO_LOG_GPS_TIME:
156 eeprom.gps_tick = state.tick;
157 AltosGPS old = state.gps;
158 state.gps = new AltosGPS();
160 /* GPS date doesn't get repeated through the file */
162 state.gps.year = old.year;
163 state.gps.month = old.month;
164 state.gps.day = old.day;
166 state.gps.hour = (record.a & 0xff);
167 state.gps.minute = (record.a >> 8);
168 state.gps.second = (record.b & 0xff);
170 int flags = (record.b >> 8);
171 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
172 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
173 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
174 AltosLib.AO_GPS_NUM_SAT_SHIFT;
175 state.new_gps = true;
178 case AltosLib.AO_LOG_GPS_LAT:
179 int lat32 = record.a | (record.b << 16);
180 state.gps.lat = (double) lat32 / 1e7;
182 case AltosLib.AO_LOG_GPS_LON:
183 int lon32 = record.a | (record.b << 16);
184 state.gps.lon = (double) lon32 / 1e7;
186 case AltosLib.AO_LOG_GPS_ALT:
187 state.gps.alt = record.a;
189 case AltosLib.AO_LOG_GPS_SAT:
190 if (state.tick == eeprom.gps_tick) {
192 int c_n0 = record.b >> 8;
193 state.gps.add_sat(svid, c_n0);
196 case AltosLib.AO_LOG_GPS_DATE:
197 state.gps.year = (record.a & 0xff) + 2000;
198 state.gps.month = record.a >> 8;
199 state.gps.day = record.b & 0xff;
202 case AltosLib.AO_LOG_CONFIG_VERSION:
204 case AltosLib.AO_LOG_MAIN_DEPLOY:
206 case AltosLib.AO_LOG_APOGEE_DELAY:
208 case AltosLib.AO_LOG_RADIO_CHANNEL:
210 case AltosLib.AO_LOG_CALLSIGN:
211 state.callsign = record.data;
213 case AltosLib.AO_LOG_ACCEL_CAL:
214 state.accel_plus_g = record.a;
215 state.accel_minus_g = record.b;
217 case AltosLib.AO_LOG_RADIO_CAL:
219 case AltosLib.AO_LOG_MANUFACTURER:
221 case AltosLib.AO_LOG_PRODUCT:
223 case AltosLib.AO_LOG_SERIAL_NUMBER:
224 state.serial = record.a;
226 case AltosLib.AO_LOG_SOFTWARE_VERSION:
229 state.seen |= eeprom.seen;
232 LinkedList<AltosRecord> make_list() {
233 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
234 Iterator<AltosOrderedRecord> iterator = records.iterator();
235 AltosOrderedRecord record = null;
236 AltosRecord state = new AltosRecord();
237 boolean last_reported = false;
238 EepromState eeprom = new EepromState();
240 state.state = AltosLib.ao_flight_pad;
241 state.accel_plus_g = 15758;
242 state.accel_minus_g = 16294;
244 /* Pull in static data from the flight and gps_date records */
245 if (flight_record != null)
246 update_state(state, flight_record, eeprom);
247 if (gps_date_record != null)
248 update_state(state, gps_date_record, eeprom);
250 while (iterator.hasNext()) {
251 record = iterator.next();
252 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
253 AltosRecord r = new AltosRecord(state);
254 r.time = (r.tick - eeprom.boost_tick) / 100.0;
257 update_state(state, record, eeprom);
259 AltosRecord r = new AltosRecord(state);
260 r.time = (r.tick - eeprom.boost_tick) / 100.0;
265 public Iterator<AltosRecord> iterator() {
268 return list.iterator();
271 public boolean has_gps() { return has_gps; }
272 public boolean has_accel() { return has_accel; }
273 public boolean has_ignite() { return has_ignite; }
275 public void write_comments(PrintStream out) {
276 Iterator<AltosOrderedRecord> iterator = records.iterator();
277 out.printf("# Comments\n");
278 while (iterator.hasNext()) {
279 AltosOrderedRecord record = iterator.next();
280 switch (record.cmd) {
281 case AltosLib.AO_LOG_CONFIG_VERSION:
282 out.printf("# Config version: %s\n", record.data);
284 case AltosLib.AO_LOG_MAIN_DEPLOY:
285 out.printf("# Main deploy: %s\n", record.a);
287 case AltosLib.AO_LOG_APOGEE_DELAY:
288 out.printf("# Apogee delay: %s\n", record.a);
290 case AltosLib.AO_LOG_RADIO_CHANNEL:
291 out.printf("# Radio channel: %s\n", record.a);
293 case AltosLib.AO_LOG_CALLSIGN:
294 out.printf("# Callsign: %s\n", record.data);
296 case AltosLib.AO_LOG_ACCEL_CAL:
297 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
299 case AltosLib.AO_LOG_RADIO_CAL:
300 out.printf ("# Radio cal: %d\n", record.a);
302 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
303 out.printf ("# Max flight log: %d\n", record.a);
305 case AltosLib.AO_LOG_MANUFACTURER:
306 out.printf ("# Manufacturer: %s\n", record.data);
308 case AltosLib.AO_LOG_PRODUCT:
309 out.printf ("# Product: %s\n", record.data);
311 case AltosLib.AO_LOG_SERIAL_NUMBER:
312 out.printf ("# Serial number: %d\n", record.a);
314 case AltosLib.AO_LOG_SOFTWARE_VERSION:
315 out.printf ("# Software version: %s\n", record.data);
322 * Given an AO_LOG_GPS_TIME record with correct time, and one
323 * missing time, rewrite the missing time values with the good
324 * ones, assuming that the difference between them is 'diff' seconds
326 void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
328 int diff = (bad.tick - good.tick + 50) / 100;
330 int hour = (good.a & 0xff);
331 int minute = (good.a >> 8);
332 int second = (good.b & 0xff);
333 int flags = (good.b >> 8);
334 int seconds = hour * 3600 + minute * 60 + second;
336 /* Make sure this looks like a good GPS value */
337 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
338 flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
339 flags |= AltosLib.AO_GPS_RUNNING;
340 flags |= AltosLib.AO_GPS_VALID;
342 int new_seconds = seconds + diff;
344 new_seconds += 24 * 3600;
345 int new_second = (new_seconds % 60);
346 int new_minutes = (new_seconds / 60);
347 int new_minute = (new_minutes % 60);
348 int new_hours = (new_minutes / 60);
349 int new_hour = (new_hours % 24);
351 bad.a = new_hour + (new_minute << 8);
352 bad.b = new_second + (flags << 8);
356 * Read the whole file, dumping records into a RB tree so
357 * we can enumerate them in time order -- the eeprom data
358 * are sometimes out of order with GPS data getting timestamps
359 * matching the first packet out of the GPS unit but not
360 * written until the final GPS packet has been received.
362 public AltosEepromIterable (FileInputStream input) {
363 records = new TreeSet<AltosOrderedRecord>();
365 AltosOrderedRecord last_gps_time = null;
369 boolean prev_tick_valid = false;
370 boolean missing_time = false;
374 String line = AltosRecord.gets(input);
377 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
380 if (record.cmd == AltosLib.AO_LOG_INVALID)
382 prev_tick = record.tick;
383 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
384 prev_tick_valid = true;
385 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
386 flight_record = record;
390 /* Two firmware bugs caused the loss of some GPS data.
391 * The flight date would never be recorded, and often
392 * the flight time would get overwritten by another
393 * record. Detect the loss of the GPS date and fix up the
394 * missing time records
396 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
397 gps_date_record = record;
401 /* go back and fix up any missing time values */
402 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
403 last_gps_time = record;
405 Iterator<AltosOrderedRecord> iterator = records.iterator();
406 while (iterator.hasNext()) {
407 AltosOrderedRecord old = iterator.next();
408 if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
409 old.a == -1 && old.b == -1)
411 update_time(record, old);
414 missing_time = false;
418 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
419 if (last_gps_time == null || last_gps_time.tick != record.tick) {
420 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
423 if (last_gps_time != null)
424 update_time(last_gps_time, add_gps_time);
428 records.add(add_gps_time);
429 record.index = index++;
434 /* Bail after reading the 'landed' record; we're all done */
435 if (record.cmd == AltosLib.AO_LOG_STATE &&
436 record.a == AltosLib.ao_flight_landed)
439 } catch (IOException io) {
440 } catch (ParseException pe) {
444 } catch (IOException ie) {