summaryrefslogtreecommitdiff
path: root/altoslib/AltosEepromMegaIterable.java
blob: f62cc45bac38ed3bec280b9cef1d3cd58b9058ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.AltosLib;

import java.io.*;
import java.util.*;
import java.text.*;
import java.util.prefs.*;
import java.util.concurrent.LinkedBlockingQueue;

/*
 * AltosRecords with an index field so they can be sorted by tick while preserving
 * the original ordering for elements with matching ticks
 */
class AltosOrderedMegaRecord extends AltosEepromMega implements Comparable<AltosOrderedMegaRecord> {

	public int	index;

	public AltosOrderedMegaRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
		throws ParseException {
		super(line);
		if (prev_tick_valid) {
			tick |= (prev_tick & ~0xffff);
			if (tick < prev_tick) {
				if (prev_tick - tick > 0x8000)
					tick += 0x10000;
			} else {
				if (tick - prev_tick > 0x8000)
					tick -= 0x10000;
			}
		}
		index = in_index;
	}

	public AltosOrderedMegaRecord(int in_cmd, int in_tick, int in_a, int in_b, int in_index) {
		super(in_cmd, in_tick);
		a = in_a;
		b = in_b;
		index = in_index;
	}

	public String toString() {
		return String.format("%d.%d %04x %04x %04x",
				     cmd, index, tick, a, b);
	}

	public int compareTo(AltosOrderedMegaRecord o) {
		int	tick_diff = tick - o.tick;
		if (tick_diff != 0)
			return tick_diff;
		return index - o.index;
	}
}

public class AltosEepromMegaIterable extends AltosRecordIterable {

	static final int	seen_flight = 1;
	static final int	seen_sensor = 2;
	static final int	seen_temp_volt = 4;
	static final int	seen_deploy = 8;
	static final int	seen_gps_time = 16;
	static final int	seen_gps_lat = 32;
	static final int	seen_gps_lon = 64;

	static final int	seen_basic = seen_flight|seen_sensor;

	boolean			has_accel;
	boolean			has_gps;
	boolean			has_ignite;

	AltosEepromMega	flight_record;
	AltosEepromMega	gps_date_record;

	TreeSet<AltosOrderedMegaRecord>	records;

	AltosMs5607		baro;

	LinkedList<AltosRecord>	list;

	class EepromState {
		int	seen;
		int	n_pad_samples;
		double	ground_pres;
		int	gps_tick;
		int	boost_tick;
		int	sensor_tick;

		EepromState() {
			seen = 0;
			n_pad_samples = 0;
			ground_pres = 0.0;
			gps_tick = 0;
		}
	}

	void update_state(AltosRecordMM state, AltosEepromMega record, EepromState eeprom) {
		state.tick = record.tick;
		switch (record.cmd) {
		case AltosLib.AO_LOG_FLIGHT:
			eeprom.seen |= seen_flight;
			state.ground_accel = record.ground_accel();
			state.flight_accel = record.ground_accel();
			state.ground_pres = baro.set(record.ground_pres(), record.ground_temp());
			state.flight_pres = state.ground_pres;
			state.flight = record.data16(0);
			eeprom.boost_tick = record.tick;
			break;
		case AltosLib.AO_LOG_SENSOR:
			state.accel = record.accel();
			baro.set(record.pres(), record.temp());
			state.pres = baro.pa;
			state.temp = baro.cc;
			state.imu = new AltosIMU();
			state.imu.accel_x = record.accel_x();
			state.imu.accel_y = record.accel_y();
			state.imu.accel_z = record.accel_z();
			state.imu.gyro_x = record.gyro_x();
			state.imu.gyro_y = record.gyro_y();
			state.imu.gyro_z = record.gyro_z();
			state.mag = new AltosMag();
			state.mag.x = record.mag_x();
			state.mag.y = record.mag_y();
			state.mag.z = record.mag_z();
			if (state.state < AltosLib.ao_flight_boost) {
				eeprom.n_pad_samples++;
				eeprom.ground_pres += state.pres;
				state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
				state.flight_pres = state.ground_pres;
			} else {
				state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
			}
			state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
			if ((eeprom.seen & seen_sensor) == 0)
				eeprom.sensor_tick = record.tick - 1;
			state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
			eeprom.seen |= seen_sensor;
			eeprom.sensor_tick = record.tick;
			has_accel = true;
			break;
		case AltosLib.AO_LOG_PRESSURE:
			state.pres = record.b;
			state.flight_pres = state.pres;
			if (eeprom.n_pad_samples == 0) {
				eeprom.n_pad_samples++;
				state.ground_pres = state.pres;
			}
			eeprom.seen |= seen_sensor;
			break;
		case AltosLib.AO_LOG_TEMP_VOLT:
			state.v_batt = record.v_batt();
			state.v_pyro = record.v_pbatt();
			for (int i = 0; i < AltosRecordMM.num_sense; i++)
				state.sense[i] = record.sense(i);
			eeprom.seen |= seen_temp_volt;
			break;
//
//		case AltosLib.AO_LOG_DEPLOY:
//			state.drogue = record.a;
//			state.main = record.b;
//			eeprom.seen |= seen_deploy;
//			has_ignite = true;
//			break;

		case AltosLib.AO_LOG_STATE:
			state.state = record.state();
			break;
		case AltosLib.AO_LOG_GPS_TIME:
			eeprom.gps_tick = state.tick;
			AltosGPS old = state.gps;
			state.gps = new AltosGPS();

			/* GPS date doesn't get repeated through the file */
			if (old != null) {
				state.gps.year = old.year;
				state.gps.month = old.month;
				state.gps.day = old.day;
			}
			state.gps.hour = (record.a & 0xff);
			state.gps.minute = (record.a >> 8);
			state.gps.second = (record.b & 0xff);

			int flags = (record.b >> 8);
			state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
			state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
			state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
				AltosLib.AO_GPS_NUM_SAT_SHIFT;
			state.new_gps = true;
			has_gps = true;
			break;
		case AltosLib.AO_LOG_GPS_LAT:
			int lat32 = record.a | (record.b << 16);
			state.gps.lat = (double) lat32 / 1e7;
			break;
		case AltosLib.AO_LOG_GPS_LON:
			int lon32 = record.a | (record.b << 16);
			state.gps.lon = (double) lon32 / 1e7;
			break;
		case AltosLib.AO_LOG_GPS_ALT:
			state.gps.alt = record.a;
			break;
		case AltosLib.AO_LOG_GPS_SAT:
			if (state.tick == eeprom.gps_tick) {
				int svid = record.a;
				int c_n0 = record.b >> 8;
				state.gps.add_sat(svid, c_n0);
			}
			break;
		case AltosLib.AO_LOG_GPS_DATE:
			state.gps.year = (record.a & 0xff) + 2000;
			state.gps.month = record.a >> 8;
			state.gps.day = record.b & 0xff;
			break;

		case AltosLib.AO_LOG_CONFIG_VERSION:
			break;
		case AltosLib.AO_LOG_MAIN_DEPLOY:
			break;
		case AltosLib.AO_LOG_APOGEE_DELAY:
			break;
		case AltosLib.AO_LOG_RADIO_CHANNEL:
			break;
		case AltosLib.AO_LOG_CALLSIGN:
			state.callsign = record.data;
			break;
		case AltosLib.AO_LOG_ACCEL_CAL:
			state.accel_plus_g = record.a;
			state.accel_minus_g = record.b;
			break;
		case AltosLib.AO_LOG_RADIO_CAL:
			break;
		case AltosLib.AO_LOG_MANUFACTURER:
			break;
		case AltosLib.AO_LOG_PRODUCT:
			break;
		case AltosLib.AO_LOG_SERIAL_NUMBER:
			state.serial = record.a;
			break;
		case AltosLib.AO_LOG_SOFTWARE_VERSION:
			break;
		case AltosLib.AO_LOG_BARO_RESERVED:
			baro.reserved = record.a;
			break;
		case AltosLib.AO_LOG_BARO_SENS:
			baro.sens =record.a;
			break;
		case AltosLib.AO_LOG_BARO_OFF:
			baro.off =record.a;
			break;
		case AltosLib.AO_LOG_BARO_TCS:
			baro.tcs =record.a;
			break;
		case AltosLib.AO_LOG_BARO_TCO:
			baro.tco =record.a;
			break;
		case AltosLib.AO_LOG_BARO_TREF:
			baro.tref =record.a;
			break;
		case AltosLib.AO_LOG_BARO_TEMPSENS:
			baro.tempsens =record.a;
			break;
		case AltosLib.AO_LOG_BARO_CRC:
			baro.crc =record.a;
			break;
		}
		state.seen |= eeprom.seen;
	}

	LinkedList<AltosRecord> make_list() {
		LinkedList<AltosRecord>		list = new LinkedList<AltosRecord>();
		Iterator<AltosOrderedMegaRecord>	iterator = records.iterator();
		AltosOrderedMegaRecord		record = null;
		AltosRecordMM			state = new AltosRecordMM();
		boolean				last_reported = false;
		EepromState			eeprom = new EepromState();

		state.state = AltosLib.ao_flight_pad;
		state.accel_plus_g = 15758;
		state.accel_minus_g = 16294;

		/* Pull in static data from the flight and gps_date records */
		if (flight_record != null)
			update_state(state, flight_record, eeprom);
		if (gps_date_record != null)
			update_state(state, gps_date_record, eeprom);

		while (iterator.hasNext()) {
			record = iterator.next();
			if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
				AltosRecordMM r = state.clone();
				r.time = (r.tick - eeprom.boost_tick) / 100.0;
				list.add(r);
			}
			update_state(state, record, eeprom);
		}
		AltosRecordMM r = state.clone();
		r.time = (r.tick - eeprom.boost_tick) / 100.0;
		list.add(r);
		return list;
	}

	public Iterator<AltosRecord> iterator() {
		if (list == null)
			list = make_list();
		return list.iterator();
	}

	public boolean has_gps() { return has_gps; }
	public boolean has_accel() { return has_accel; }
	public boolean has_ignite() { return has_ignite; }

	public void write_comments(PrintStream out) {
		Iterator<AltosOrderedMegaRecord>	iterator = records.iterator();
		out.printf("# Comments\n");
		while (iterator.hasNext()) {
			AltosOrderedMegaRecord	record = iterator.next();
			switch (record.cmd) {
			case AltosLib.AO_LOG_CONFIG_VERSION:
				out.printf("# Config version: %s\n", record.data);
				break;
			case AltosLib.AO_LOG_MAIN_DEPLOY:
				out.printf("# Main deploy: %s\n", record.a);
				break;
			case AltosLib.AO_LOG_APOGEE_DELAY:
				out.printf("# Apogee delay: %s\n", record.a);
				break;
			case AltosLib.AO_LOG_RADIO_CHANNEL:
				out.printf("# Radio channel: %s\n", record.a);
				break;
			case AltosLib.AO_LOG_CALLSIGN:
				out.printf("# Callsign: %s\n", record.data);
				break;
			case AltosLib.AO_LOG_ACCEL_CAL:
				out.printf ("# Accel cal: %d %d\n", record.a, record.b);
				break;
			case AltosLib.AO_LOG_RADIO_CAL:
				out.printf ("# Radio cal: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
				out.printf ("# Max flight log: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_MANUFACTURER:
				out.printf ("# Manufacturer: %s\n", record.data);
				break;
			case AltosLib.AO_LOG_PRODUCT:
				out.printf ("# Product: %s\n", record.data);
				break;
			case AltosLib.AO_LOG_SERIAL_NUMBER:
				out.printf ("# Serial number: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_SOFTWARE_VERSION:
				out.printf ("# Software version: %s\n", record.data);
				break;
			case AltosLib.AO_LOG_BARO_RESERVED:
				out.printf ("# Baro reserved: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_SENS:
				out.printf ("# Baro sens: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_OFF:
				out.printf ("# Baro off: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_TCS:
				out.printf ("# Baro tcs: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_TCO:
				out.printf ("# Baro tco: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_TREF:
				out.printf ("# Baro tref: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_TEMPSENS:
				out.printf ("# Baro tempsens: %d\n", record.a);
				break;
			case AltosLib.AO_LOG_BARO_CRC:
				out.printf ("# Baro crc: %d\n", record.a);
				break;
			}
		}
	}

	/*
	 * Given an AO_LOG_GPS_TIME record with correct time, and one
	 * missing time, rewrite the missing time values with the good
	 * ones, assuming that the difference between them is 'diff' seconds
	 */
	void update_time(AltosOrderedMegaRecord good, AltosOrderedMegaRecord bad) {

		int diff = (bad.tick - good.tick + 50) / 100;

		int hour = (good.a & 0xff);
		int minute = (good.a >> 8);
		int second = (good.b & 0xff);
		int flags = (good.b >> 8);
		int seconds = hour * 3600 + minute * 60 + second;

		/* Make sure this looks like a good GPS value */
		if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
			flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
		flags |= AltosLib.AO_GPS_RUNNING;
		flags |= AltosLib.AO_GPS_VALID;

		int new_seconds = seconds + diff;
		if (new_seconds < 0)
			new_seconds += 24 * 3600;
		int new_second = (new_seconds % 60);
		int new_minutes = (new_seconds / 60);
		int new_minute = (new_minutes % 60);
		int new_hours = (new_minutes / 60);
		int new_hour = (new_hours % 24);

		bad.a = new_hour + (new_minute << 8);
		bad.b = new_second + (flags << 8);
	}

	/*
	 * Read the whole file, dumping records into a RB tree so
	 * we can enumerate them in time order -- the eeprom data
	 * are sometimes out of order with GPS data getting timestamps
	 * matching the first packet out of the GPS unit but not
	 * written until the final GPS packet has been received.
	 */
	public AltosEepromMegaIterable (FileInputStream input) {
		records = new TreeSet<AltosOrderedMegaRecord>();

		AltosOrderedMegaRecord last_gps_time = null;

		baro = new AltosMs5607();

		int index = 0;
		int prev_tick = 0;
		boolean prev_tick_valid = false;
		boolean missing_time = false;

		try {
			for (;;) {
				String line = AltosLib.gets(input);
				if (line == null)
					break;
				AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(line, index++, prev_tick, prev_tick_valid);
				if (record == null)
					break;
				if (record.cmd == AltosLib.AO_LOG_INVALID)
					continue;
				prev_tick = record.tick;
				if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
					prev_tick_valid = true;
				if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
					flight_record = record;
					continue;
				}

				/* Two firmware bugs caused the loss of some GPS data.
				 * The flight date would never be recorded, and often
				 * the flight time would get overwritten by another
				 * record. Detect the loss of the GPS date and fix up the
				 * missing time records
				 */
				if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
					gps_date_record = record;
					continue;
				}

				/* go back and fix up any missing time values */
				if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
					last_gps_time = record;
					if (missing_time) {
						Iterator<AltosOrderedMegaRecord> iterator = records.iterator();
						while (iterator.hasNext()) {
							AltosOrderedMegaRecord old = iterator.next();
							if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
							    old.a == -1 && old.b == -1)
							{
								update_time(record, old);
							}
						}
						missing_time = false;
					}
				}

				if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
					if (last_gps_time == null || last_gps_time.tick != record.tick) {
						AltosOrderedMegaRecord add_gps_time = new AltosOrderedMegaRecord(AltosLib.AO_LOG_GPS_TIME,
													 record.tick,
													 -1, -1, index-1);
						if (last_gps_time != null)
							update_time(last_gps_time, add_gps_time);
						else
							missing_time = true;

						records.add(add_gps_time);
						record.index = index++;
					}
				}
				records.add(record);

				/* Bail after reading the 'landed' record; we're all done */
				if (record.cmd == AltosLib.AO_LOG_STATE &&
				    record.a == AltosLib.ao_flight_landed)
					break;
			}
		} catch (IOException io) {
		} catch (ParseException pe) {
		}
		try {
			input.close();
		} catch (IOException ie) {
		}
	}
}