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