altosui: Support MM telemetry packets
[fw/altos] / altoslib / AltosEepromIterable.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.prefs.*;
24 import java.util.concurrent.LinkedBlockingQueue;
25
26 /*
27  * AltosRecords with an index field so they can be sorted by tick while preserving
28  * the original ordering for elements with matching ticks
29  */
30 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
31
32         public int      index;
33
34         public AltosOrderedRecord(String line, int in_index, int prev_tick, boolean prev_tick_valid)
35                 throws ParseException {
36                 super(line);
37                 if (prev_tick_valid) {
38                         tick |= (prev_tick & ~0xffff);
39                         if (tick < prev_tick) {
40                                 if (prev_tick - tick > 0x8000)
41                                         tick += 0x10000;
42                         } else {
43                                 if (tick - prev_tick > 0x8000)
44                                         tick -= 0x10000;
45                         }
46                 }
47                 index = in_index;
48         }
49
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);
52                 index = in_index;
53         }
54
55         public String toString() {
56                 return String.format("%d.%d %04x %04x %04x",
57                                      cmd, index, tick, a, b);
58         }
59
60         public int compareTo(AltosOrderedRecord o) {
61                 int     tick_diff = tick - o.tick;
62                 if (tick_diff != 0)
63                         return tick_diff;
64                 return index - o.index;
65         }
66 }
67
68 public class AltosEepromIterable extends AltosRecordIterable {
69
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;
77
78         static final int        seen_basic = seen_flight|seen_sensor;
79
80         boolean                 has_accel;
81         boolean                 has_gps;
82         boolean                 has_ignite;
83
84         AltosEepromRecord       flight_record;
85         AltosEepromRecord       gps_date_record;
86
87         TreeSet<AltosOrderedRecord>     records;
88
89         LinkedList<AltosRecord> list;
90
91         class EepromState {
92                 int     seen;
93                 int     n_pad_samples;
94                 double  ground_pres;
95                 int     gps_tick;
96                 int     boost_tick;
97                 int     sensor_tick;
98
99                 EepromState() {
100                         seen = 0;
101                         n_pad_samples = 0;
102                         ground_pres = 0.0;
103                         gps_tick = 0;
104                 }
105         }
106
107         void update_state(AltosRecordTM state, AltosEepromRecord record, EepromState eeprom) {
108                 state.tick = record.tick;
109                 switch (record.cmd) {
110                 case AltosLib.AO_LOG_FLIGHT:
111                         eeprom.seen |= seen_flight;
112                         state.ground_accel = record.a;
113                         state.flight_accel = record.a;
114                         state.flight = record.b;
115                         eeprom.boost_tick = record.tick;
116                         break;
117                 case AltosLib.AO_LOG_SENSOR:
118                         state.accel = record.a;
119                         state.pres = record.b;
120                         if (state.state < AltosLib.ao_flight_boost) {
121                                 eeprom.n_pad_samples++;
122                                 eeprom.ground_pres += state.pres;
123                                 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
124                                 state.flight_pres = state.ground_pres;
125                         } else {
126                                 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
127                         }
128                         state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
129                         if ((eeprom.seen & seen_sensor) == 0)
130                                 eeprom.sensor_tick = record.tick - 1;
131                         state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
132                         eeprom.seen |= seen_sensor;
133                         eeprom.sensor_tick = record.tick;
134                         has_accel = true;
135                         break;
136                 case AltosLib.AO_LOG_PRESSURE:
137                         state.pres = record.b;
138                         state.flight_pres = state.pres;
139                         if (eeprom.n_pad_samples == 0) {
140                                 eeprom.n_pad_samples++;
141                                 state.ground_pres = state.pres;
142                         }
143                         eeprom.seen |= seen_sensor;
144                         break;
145                 case AltosLib.AO_LOG_TEMP_VOLT:
146                         state.temp = record.a;
147                         state.batt = record.b;
148                         eeprom.seen |= seen_temp_volt;
149                         break;
150                 case AltosLib.AO_LOG_DEPLOY:
151                         state.drogue = record.a;
152                         state.main = record.b;
153                         eeprom.seen |= seen_deploy;
154                         has_ignite = true;
155                         break;
156                 case AltosLib.AO_LOG_STATE:
157                         state.state = record.a;
158                         break;
159                 case AltosLib.AO_LOG_GPS_TIME:
160                         eeprom.gps_tick = state.tick;
161                         AltosGPS old = state.gps;
162                         state.gps = new AltosGPS();
163
164                         /* GPS date doesn't get repeated through the file */
165                         if (old != null) {
166                                 state.gps.year = old.year;
167                                 state.gps.month = old.month;
168                                 state.gps.day = old.day;
169                         }
170                         state.gps.hour = (record.a & 0xff);
171                         state.gps.minute = (record.a >> 8);
172                         state.gps.second = (record.b & 0xff);
173
174                         int flags = (record.b >> 8);
175                         state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
176                         state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
177                         state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
178                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
179                         state.new_gps = true;
180                         has_gps = true;
181                         break;
182                 case AltosLib.AO_LOG_GPS_LAT:
183                         int lat32 = record.a | (record.b << 16);
184                         state.gps.lat = (double) lat32 / 1e7;
185                         break;
186                 case AltosLib.AO_LOG_GPS_LON:
187                         int lon32 = record.a | (record.b << 16);
188                         state.gps.lon = (double) lon32 / 1e7;
189                         break;
190                 case AltosLib.AO_LOG_GPS_ALT:
191                         state.gps.alt = record.a;
192                         break;
193                 case AltosLib.AO_LOG_GPS_SAT:
194                         if (state.tick == eeprom.gps_tick) {
195                                 int svid = record.a;
196                                 int c_n0 = record.b >> 8;
197                                 state.gps.add_sat(svid, c_n0);
198                         }
199                         break;
200                 case AltosLib.AO_LOG_GPS_DATE:
201                         state.gps.year = (record.a & 0xff) + 2000;
202                         state.gps.month = record.a >> 8;
203                         state.gps.day = record.b & 0xff;
204                         break;
205
206                 case AltosLib.AO_LOG_CONFIG_VERSION:
207                         break;
208                 case AltosLib.AO_LOG_MAIN_DEPLOY:
209                         break;
210                 case AltosLib.AO_LOG_APOGEE_DELAY:
211                         break;
212                 case AltosLib.AO_LOG_RADIO_CHANNEL:
213                         break;
214                 case AltosLib.AO_LOG_CALLSIGN:
215                         state.callsign = record.data;
216                         break;
217                 case AltosLib.AO_LOG_ACCEL_CAL:
218                         state.accel_plus_g = record.a;
219                         state.accel_minus_g = record.b;
220                         break;
221                 case AltosLib.AO_LOG_RADIO_CAL:
222                         break;
223                 case AltosLib.AO_LOG_MANUFACTURER:
224                         break;
225                 case AltosLib.AO_LOG_PRODUCT:
226                         break;
227                 case AltosLib.AO_LOG_SERIAL_NUMBER:
228                         state.serial = record.a;
229                         break;
230                 case AltosLib.AO_LOG_SOFTWARE_VERSION:
231                         break;
232                 }
233                 state.seen |= eeprom.seen;
234         }
235
236         LinkedList<AltosRecord> make_list() {
237                 LinkedList<AltosRecord>         list = new LinkedList<AltosRecord>();
238                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
239                 AltosOrderedRecord              record = null;
240                 AltosRecordTM                   state = new AltosRecordTM();
241                 boolean                         last_reported = false;
242                 EepromState                     eeprom = new EepromState();
243
244                 state.state = AltosLib.ao_flight_pad;
245                 state.accel_plus_g = 15758;
246                 state.accel_minus_g = 16294;
247
248                 /* Pull in static data from the flight and gps_date records */
249                 if (flight_record != null)
250                         update_state(state, flight_record, eeprom);
251                 if (gps_date_record != null)
252                         update_state(state, gps_date_record, eeprom);
253
254                 while (iterator.hasNext()) {
255                         record = iterator.next();
256                         if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
257                                 AltosRecordTM r = state.clone();
258                                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
259                                 list.add(r);
260                         }
261                         update_state(state, record, eeprom);
262                 }
263                 AltosRecordTM r = state.clone();
264                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
265                 list.add(r);
266         return list;
267         }
268
269         public Iterator<AltosRecord> iterator() {
270                 if (list == null)
271                         list = make_list();
272                 return list.iterator();
273         }
274
275         public boolean has_gps() { return has_gps; }
276         public boolean has_accel() { return has_accel; }
277         public boolean has_ignite() { return has_ignite; }
278
279         public void write_comments(PrintStream out) {
280                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
281                 out.printf("# Comments\n");
282                 while (iterator.hasNext()) {
283                         AltosOrderedRecord      record = iterator.next();
284                         switch (record.cmd) {
285                         case AltosLib.AO_LOG_CONFIG_VERSION:
286                                 out.printf("# Config version: %s\n", record.data);
287                                 break;
288                         case AltosLib.AO_LOG_MAIN_DEPLOY:
289                                 out.printf("# Main deploy: %s\n", record.a);
290                                 break;
291                         case AltosLib.AO_LOG_APOGEE_DELAY:
292                                 out.printf("# Apogee delay: %s\n", record.a);
293                                 break;
294                         case AltosLib.AO_LOG_RADIO_CHANNEL:
295                                 out.printf("# Radio channel: %s\n", record.a);
296                                 break;
297                         case AltosLib.AO_LOG_CALLSIGN:
298                                 out.printf("# Callsign: %s\n", record.data);
299                                 break;
300                         case AltosLib.AO_LOG_ACCEL_CAL:
301                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
302                                 break;
303                         case AltosLib.AO_LOG_RADIO_CAL:
304                                 out.printf ("# Radio cal: %d\n", record.a);
305                                 break;
306                         case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
307                                 out.printf ("# Max flight log: %d\n", record.a);
308                                 break;
309                         case AltosLib.AO_LOG_MANUFACTURER:
310                                 out.printf ("# Manufacturer: %s\n", record.data);
311                                 break;
312                         case AltosLib.AO_LOG_PRODUCT:
313                                 out.printf ("# Product: %s\n", record.data);
314                                 break;
315                         case AltosLib.AO_LOG_SERIAL_NUMBER:
316                                 out.printf ("# Serial number: %d\n", record.a);
317                                 break;
318                         case AltosLib.AO_LOG_SOFTWARE_VERSION:
319                                 out.printf ("# Software version: %s\n", record.data);
320                                 break;
321                         case AltosLib.AO_LOG_BARO_RESERVED:
322                                 out.printf ("# Baro reserved: %d\n", record.a);
323                                 break;
324                         case AltosLib.AO_LOG_BARO_SENS:
325                                 out.printf ("# Baro sens: %d\n", record.a);
326                                 break;
327                         case AltosLib.AO_LOG_BARO_OFF:
328                                 out.printf ("# Baro off: %d\n", record.a);
329                                 break;
330                         case AltosLib.AO_LOG_BARO_TCS:
331                                 out.printf ("# Baro tcs: %d\n", record.a);
332                                 break;
333                         case AltosLib.AO_LOG_BARO_TCO:
334                                 out.printf ("# Baro tco: %d\n", record.a);
335                                 break;
336                         case AltosLib.AO_LOG_BARO_TREF:
337                                 out.printf ("# Baro tref: %d\n", record.a);
338                                 break;
339                         case AltosLib.AO_LOG_BARO_TEMPSENS:
340                                 out.printf ("# Baro tempsens: %d\n", record.a);
341                                 break;
342                         case AltosLib.AO_LOG_BARO_CRC:
343                                 out.printf ("# Baro crc: %d\n", record.a);
344                                 break;
345                         }
346                 }
347         }
348
349         /*
350          * Given an AO_LOG_GPS_TIME record with correct time, and one
351          * missing time, rewrite the missing time values with the good
352          * ones, assuming that the difference between them is 'diff' seconds
353          */
354         void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
355
356                 int diff = (bad.tick - good.tick + 50) / 100;
357
358                 int hour = (good.a & 0xff);
359                 int minute = (good.a >> 8);
360                 int second = (good.b & 0xff);
361                 int flags = (good.b >> 8);
362                 int seconds = hour * 3600 + minute * 60 + second;
363
364                 /* Make sure this looks like a good GPS value */
365                 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
366                         flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
367                 flags |= AltosLib.AO_GPS_RUNNING;
368                 flags |= AltosLib.AO_GPS_VALID;
369
370                 int new_seconds = seconds + diff;
371                 if (new_seconds < 0)
372                         new_seconds += 24 * 3600;
373                 int new_second = (new_seconds % 60);
374                 int new_minutes = (new_seconds / 60);
375                 int new_minute = (new_minutes % 60);
376                 int new_hours = (new_minutes / 60);
377                 int new_hour = (new_hours % 24);
378
379                 bad.a = new_hour + (new_minute << 8);
380                 bad.b = new_second + (flags << 8);
381         }
382
383         /*
384          * Read the whole file, dumping records into a RB tree so
385          * we can enumerate them in time order -- the eeprom data
386          * are sometimes out of order with GPS data getting timestamps
387          * matching the first packet out of the GPS unit but not
388          * written until the final GPS packet has been received.
389          */
390         public AltosEepromIterable (FileInputStream input) {
391                 records = new TreeSet<AltosOrderedRecord>();
392
393                 AltosOrderedRecord last_gps_time = null;
394
395                 int index = 0;
396                 int prev_tick = 0;
397                 boolean prev_tick_valid = false;
398                 boolean missing_time = false;
399
400                 try {
401                         for (;;) {
402                                 String line = AltosLib.gets(input);
403                                 if (line == null)
404                                         break;
405                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
406                                 if (record == null)
407                                         break;
408                                 if (record.cmd == AltosLib.AO_LOG_INVALID)
409                                         continue;
410                                 prev_tick = record.tick;
411                                 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
412                                         prev_tick_valid = true;
413                                 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
414                                         flight_record = record;
415                                         continue;
416                                 }
417
418                                 /* Two firmware bugs caused the loss of some GPS data.
419                                  * The flight date would never be recorded, and often
420                                  * the flight time would get overwritten by another
421                                  * record. Detect the loss of the GPS date and fix up the
422                                  * missing time records
423                                  */
424                                 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
425                                         gps_date_record = record;
426                                         continue;
427                                 }
428
429                                 /* go back and fix up any missing time values */
430                                 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
431                                         last_gps_time = record;
432                                         if (missing_time) {
433                                                 Iterator<AltosOrderedRecord> iterator = records.iterator();
434                                                 while (iterator.hasNext()) {
435                                                         AltosOrderedRecord old = iterator.next();
436                                                         if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
437                                                             old.a == -1 && old.b == -1)
438                                                         {
439                                                                 update_time(record, old);
440                                                         }
441                                                 }
442                                                 missing_time = false;
443                                         }
444                                 }
445
446                                 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
447                                         if (last_gps_time == null || last_gps_time.tick != record.tick) {
448                                                 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
449                                                                                                          record.tick,
450                                                                                                          -1, -1, index-1);
451                                                 if (last_gps_time != null)
452                                                         update_time(last_gps_time, add_gps_time);
453                                                 else
454                                                         missing_time = true;
455
456                                                 records.add(add_gps_time);
457                                                 record.index = index++;
458                                         }
459                                 }
460                                 records.add(record);
461
462                                 /* Bail after reading the 'landed' record; we're all done */
463                                 if (record.cmd == AltosLib.AO_LOG_STATE &&
464                                     record.a == AltosLib.ao_flight_landed)
465                                         break;
466                         }
467                 } catch (IOException io) {
468                 } catch (ParseException pe) {
469                 }
470                 try {
471                         input.close();
472                 } catch (IOException ie) {
473                 }
474         }
475 }