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