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 AltosEepromMegaIterable 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 AltosEepromMega flight_record;
41 AltosEepromMega gps_date_record;
43 TreeSet<AltosOrderedMegaRecord> records;
47 LinkedList<AltosRecord> list;
65 void update_state(AltosRecordMM state, AltosEepromMega record, EepromState eeprom) {
66 state.tick = record.tick;
68 case AltosLib.AO_LOG_FLIGHT:
69 eeprom.seen |= seen_flight;
70 state.ground_accel = record.ground_accel();
71 state.flight_accel = record.ground_accel();
72 state.ground_pres = baro.set(record.ground_pres(), record.ground_temp());
73 state.flight_pres = state.ground_pres;
74 state.flight = record.data16(0);
75 eeprom.boost_tick = record.tick;
77 case AltosLib.AO_LOG_SENSOR:
78 state.accel = record.accel();
79 baro.set(record.pres(), record.temp());
82 state.imu = new AltosIMU();
83 state.imu.accel_x = record.accel_x();
84 state.imu.accel_y = record.accel_y();
85 state.imu.accel_z = record.accel_z();
86 state.imu.gyro_x = record.gyro_x();
87 state.imu.gyro_y = record.gyro_y();
88 state.imu.gyro_z = record.gyro_z();
89 state.mag = new AltosMag();
90 state.mag.x = record.mag_x();
91 state.mag.y = record.mag_y();
92 state.mag.z = record.mag_z();
93 if (state.state < AltosLib.ao_flight_boost) {
94 eeprom.n_pad_samples++;
95 eeprom.ground_pres += state.pres;
96 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
97 state.flight_pres = state.ground_pres;
99 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
101 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
102 if ((eeprom.seen & seen_sensor) == 0)
103 eeprom.sensor_tick = record.tick - 1;
104 state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
105 eeprom.seen |= seen_sensor;
106 eeprom.sensor_tick = record.tick;
109 case AltosLib.AO_LOG_TEMP_VOLT:
110 state.v_batt = record.v_batt();
111 state.v_pyro = record.v_pbatt();
112 for (int i = 0; i < AltosRecordMM.num_sense; i++)
113 state.sense[i] = record.sense(i);
114 eeprom.seen |= seen_temp_volt;
116 case AltosLib.AO_LOG_STATE:
117 state.state = record.state();
119 case AltosLib.AO_LOG_GPS_TIME:
120 eeprom.gps_tick = state.tick;
121 AltosGPS old = state.gps;
122 state.gps = new AltosGPS();
124 /* GPS date doesn't get repeated through the file */
126 state.gps.year = old.year;
127 state.gps.month = old.month;
128 state.gps.day = old.day;
130 state.gps.hour = (record.a & 0xff);
131 state.gps.minute = (record.a >> 8);
132 state.gps.second = (record.b & 0xff);
134 int flags = (record.b >> 8);
135 state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
136 state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
137 state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
138 AltosLib.AO_GPS_NUM_SAT_SHIFT;
139 state.new_gps = true;
142 case AltosLib.AO_LOG_GPS_LAT:
143 int lat32 = record.a | (record.b << 16);
144 state.gps.lat = (double) lat32 / 1e7;
146 case AltosLib.AO_LOG_GPS_LON:
147 int lon32 = record.a | (record.b << 16);
148 state.gps.lon = (double) lon32 / 1e7;
150 case AltosLib.AO_LOG_GPS_ALT:
151 state.gps.alt = record.a;
153 case AltosLib.AO_LOG_GPS_SAT:
154 if (state.tick == eeprom.gps_tick) {
156 int c_n0 = record.b >> 8;
157 state.gps.add_sat(svid, c_n0);
160 case AltosLib.AO_LOG_GPS_DATE:
161 state.gps.year = (record.a & 0xff) + 2000;
162 state.gps.month = record.a >> 8;
163 state.gps.day = record.b & 0xff;
166 case AltosLib.AO_LOG_CONFIG_VERSION:
168 case AltosLib.AO_LOG_MAIN_DEPLOY:
170 case AltosLib.AO_LOG_APOGEE_DELAY:
172 case AltosLib.AO_LOG_RADIO_CHANNEL:
174 case AltosLib.AO_LOG_CALLSIGN:
175 state.callsign = record.data;
177 case AltosLib.AO_LOG_ACCEL_CAL:
178 state.accel_plus_g = record.a;
179 state.accel_minus_g = record.b;
181 case AltosLib.AO_LOG_RADIO_CAL:
183 case AltosLib.AO_LOG_MANUFACTURER:
185 case AltosLib.AO_LOG_PRODUCT:
187 case AltosLib.AO_LOG_SERIAL_NUMBER:
188 state.serial = record.a;
190 case AltosLib.AO_LOG_SOFTWARE_VERSION:
192 case AltosLib.AO_LOG_BARO_RESERVED:
193 baro.reserved = record.a;
195 case AltosLib.AO_LOG_BARO_SENS:
198 case AltosLib.AO_LOG_BARO_OFF:
201 case AltosLib.AO_LOG_BARO_TCS:
204 case AltosLib.AO_LOG_BARO_TCO:
207 case AltosLib.AO_LOG_BARO_TREF:
210 case AltosLib.AO_LOG_BARO_TEMPSENS:
211 baro.tempsens =record.a;
213 case AltosLib.AO_LOG_BARO_CRC:
217 state.seen |= eeprom.seen;
220 LinkedList<AltosRecord> make_list() {
221 LinkedList<AltosRecord> list = new LinkedList<AltosRecord>();
222 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
223 AltosOrderedMegaRecord record = null;
224 AltosRecordMM state = new AltosRecordMM();
225 //boolean last_reported = false;
226 EepromState eeprom = new EepromState();
228 state.state = AltosLib.ao_flight_pad;
229 state.accel_plus_g = 15758;
230 state.accel_minus_g = 16294;
232 /* Pull in static data from the flight and gps_date records */
233 if (flight_record != null)
234 update_state(state, flight_record, eeprom);
235 if (gps_date_record != null)
236 update_state(state, gps_date_record, eeprom);
238 while (iterator.hasNext()) {
239 record = iterator.next();
240 if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
241 AltosRecordMM r = state.clone();
242 r.time = (r.tick - eeprom.boost_tick) / 100.0;
245 update_state(state, record, eeprom);
247 AltosRecordMM r = state.clone();
248 r.time = (r.tick - eeprom.boost_tick) / 100.0;
253 public Iterator<AltosRecord> iterator() {
256 return list.iterator();
259 public boolean has_gps() { return has_gps; }
260 public boolean has_accel() { return has_accel; }
261 public boolean has_ignite() { return has_ignite; }
263 public void write_comments(PrintStream out) {
264 Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
265 out.printf("# Comments\n");
266 while (iterator.hasNext()) {
267 AltosOrderedMegaRecord record = iterator.next();
268 switch (record.cmd) {
269 case AltosLib.AO_LOG_CONFIG_VERSION:
270 out.printf("# Config version: %s\n", record.data);
272 case AltosLib.AO_LOG_MAIN_DEPLOY:
273 out.printf("# Main deploy: %s\n", record.a);
275 case AltosLib.AO_LOG_APOGEE_DELAY:
276 out.printf("# Apogee delay: %s\n", record.a);
278 case AltosLib.AO_LOG_RADIO_CHANNEL:
279 out.printf("# Radio channel: %s\n", record.a);
281 case AltosLib.AO_LOG_CALLSIGN:
282 out.printf("# Callsign: %s\n", record.data);
284 case AltosLib.AO_LOG_ACCEL_CAL:
285 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
287 case AltosLib.AO_LOG_RADIO_CAL:
288 out.printf ("# Radio cal: %d\n", record.a);
290 case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
291 out.printf ("# Max flight log: %d\n", record.a);
293 case AltosLib.AO_LOG_MANUFACTURER:
294 out.printf ("# Manufacturer: %s\n", record.data);
296 case AltosLib.AO_LOG_PRODUCT:
297 out.printf ("# Product: %s\n", record.data);
299 case AltosLib.AO_LOG_SERIAL_NUMBER:
300 out.printf ("# Serial number: %d\n", record.a);
302 case AltosLib.AO_LOG_SOFTWARE_VERSION:
303 out.printf ("# Software version: %s\n", record.data);
305 case AltosLib.AO_LOG_BARO_RESERVED:
306 out.printf ("# Baro reserved: %d\n", record.a);
308 case AltosLib.AO_LOG_BARO_SENS:
309 out.printf ("# Baro sens: %d\n", record.a);
311 case AltosLib.AO_LOG_BARO_OFF:
312 out.printf ("# Baro off: %d\n", record.a);
314 case AltosLib.AO_LOG_BARO_TCS:
315 out.printf ("# Baro tcs: %d\n", record.a);
317 case AltosLib.AO_LOG_BARO_TCO:
318 out.printf ("# Baro tco: %d\n", record.a);
320 case AltosLib.AO_LOG_BARO_TREF:
321 out.printf ("# Baro tref: %d\n", record.a);
323 case AltosLib.AO_LOG_BARO_TEMPSENS:
324 out.printf ("# Baro tempsens: %d\n", record.a);
326 case AltosLib.AO_LOG_BARO_CRC:
327 out.printf ("# Baro crc: %d\n", record.a);
334 * Read the whole file, dumping records into a RB tree so
335 * we can enumerate them in time order -- the eeprom data
336 * are sometimes out of order with GPS data getting timestamps
337 * matching the first packet out of the GPS unit but not
338 * written until the final GPS packet has been received.
340 public AltosEepromMegaIterable (FileInputStream input) {
341 records = new TreeSet<AltosOrderedMegaRecord>();
343 AltosOrderedMegaRecord last_gps_time = null;
345 baro = new AltosMs5607();
349 boolean prev_tick_valid = false;
350 boolean missing_time = false;
354 String line = AltosLib.gets(input);
357 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(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;
370 /* Bail after reading the 'landed' record; we're all done */
371 if (record.cmd == AltosLib.AO_LOG_STATE &&
372 record.a == AltosLib.ao_flight_landed)
375 } catch (IOException io) {
376 } catch (ParseException pe) {
380 } catch (IOException ie) {