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