altosui: Parse and export Max flight log value
[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 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|seen_temp_volt|seen_deploy;
79
80         AltosEepromRecord       flight_record;
81         AltosEepromRecord       gps_date_record;
82
83         TreeSet<AltosOrderedRecord>     records;
84
85         LinkedList<AltosRecord> list;
86
87         class EepromState {
88                 int     seen;
89                 int     n_pad_samples;
90                 double  ground_pres;
91                 int     gps_tick;
92                 int     boost_tick;
93
94                 EepromState() {
95                         seen = 0;
96                         n_pad_samples = 0;
97                         ground_pres = 0.0;
98                         gps_tick = 0;
99                 }
100         }
101
102         void update_state(AltosRecord state, AltosEepromRecord record, EepromState eeprom) {
103                 state.tick = record.tick;
104                 switch (record.cmd) {
105                 case Altos.AO_LOG_FLIGHT:
106                         eeprom.seen |= seen_flight;
107                         state.ground_accel = record.a;
108                         state.flight_accel = record.a;
109                         state.flight = record.b;
110                         eeprom.boost_tick = record.tick;
111                         break;
112                 case Altos.AO_LOG_SENSOR:
113                         state.accel = record.a;
114                         state.pres = record.b;
115                         if (state.state < Altos.ao_flight_boost) {
116                                 eeprom.n_pad_samples++;
117                                 eeprom.ground_pres += state.pres;
118                                 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
119                                 state.flight_pres = state.ground_pres;
120                         } else {
121                                 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
122                                 state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
123                                 state.flight_vel += (state.accel_plus_g - state.accel);
124                         }
125                         eeprom.seen |= seen_sensor;
126                         break;
127                 case Altos.AO_LOG_HEIGHT:
128                         state.height = record.a;
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.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
163                                 Altos.AO_GPS_NUM_SAT_SHIFT;
164                         break;
165                 case Altos.AO_LOG_GPS_LAT:
166                         int lat32 = record.a | (record.b << 16);
167                         state.gps.lat = (double) lat32 / 1e7;
168                         break;
169                 case Altos.AO_LOG_GPS_LON:
170                         int lon32 = record.a | (record.b << 16);
171                         state.gps.lon = (double) lon32 / 1e7;
172                         break;
173                 case Altos.AO_LOG_GPS_ALT:
174                         state.gps.alt = record.a;
175                         break;
176                 case Altos.AO_LOG_GPS_SAT:
177                         if (state.tick == eeprom.gps_tick) {
178                                 int svid = record.a;
179                                 int c_n0 = record.b >> 8;
180                                 state.gps.add_sat(svid, c_n0);
181                         }
182                         break;
183                 case Altos.AO_LOG_GPS_DATE:
184                         state.gps.year = (record.a & 0xff) + 2000;
185                         state.gps.month = record.a >> 8;
186                         state.gps.day = record.b & 0xff;
187                         break;
188
189                 case Altos.AO_LOG_CONFIG_VERSION:
190                         break;
191                 case Altos.AO_LOG_MAIN_DEPLOY:
192                         break;
193                 case Altos.AO_LOG_APOGEE_DELAY:
194                         break;
195                 case Altos.AO_LOG_RADIO_CHANNEL:
196                         break;
197                 case Altos.AO_LOG_CALLSIGN:
198                         state.callsign = record.data;
199                         break;
200                 case Altos.AO_LOG_ACCEL_CAL:
201                         state.accel_plus_g = record.a;
202                         state.accel_minus_g = record.b;
203                         break;
204                 case Altos.AO_LOG_RADIO_CAL:
205                         break;
206                 case Altos.AO_LOG_MANUFACTURER:
207                         break;
208                 case Altos.AO_LOG_PRODUCT:
209                         break;
210                 case Altos.AO_LOG_SERIAL_NUMBER:
211                         state.serial = record.a;
212                         break;
213                 case Altos.AO_LOG_SOFTWARE_VERSION:
214                         break;
215                 }
216         }
217
218         LinkedList<AltosRecord> make_list() {
219                 LinkedList<AltosRecord>         list = new LinkedList<AltosRecord>();
220                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
221                 AltosOrderedRecord              record = null;
222                 AltosRecord                     state = new AltosRecord();
223                 boolean                         last_reported = false;
224                 EepromState                     eeprom = new EepromState();
225
226                 state.state = Altos.ao_flight_pad;
227                 state.accel_plus_g = 15758;
228                 state.accel_minus_g = 16294;
229
230                 /* Pull in static data from the flight and gps_date records */
231                 if (flight_record != null)
232                         update_state(state, flight_record, eeprom);
233                 if (gps_date_record != null)
234                         update_state(state, gps_date_record, eeprom);
235
236                 while (iterator.hasNext()) {
237                         record = iterator.next();
238                         if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
239                                 AltosRecord r = new AltosRecord(state);
240                                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
241                                 list.add(r);
242                         }
243                         update_state(state, record, eeprom);
244                 }
245                 AltosRecord r = new AltosRecord(state);
246                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
247                 list.add(r);
248                 return list;
249         }
250
251         public Iterator<AltosRecord> iterator() {
252                 if (list == null)
253                         list = make_list();
254                 return list.iterator();
255         }
256
257         public void write_comments(PrintStream out) {
258                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
259                 out.printf("# Comments\n");
260                 while (iterator.hasNext()) {
261                         AltosOrderedRecord      record = iterator.next();
262                         switch (record.cmd) {
263                         case Altos.AO_LOG_CONFIG_VERSION:
264                                 out.printf("# Config version: %s\n", record.data);
265                                 break;
266                         case Altos.AO_LOG_MAIN_DEPLOY:
267                                 out.printf("# Main deploy: %s\n", record.a);
268                                 break;
269                         case Altos.AO_LOG_APOGEE_DELAY:
270                                 out.printf("# Apogee delay: %s\n", record.a);
271                                 break;
272                         case Altos.AO_LOG_RADIO_CHANNEL:
273                                 out.printf("# Radio channel: %s\n", record.a);
274                                 break;
275                         case Altos.AO_LOG_CALLSIGN:
276                                 out.printf("# Callsign: %s\n", record.data);
277                                 break;
278                         case Altos.AO_LOG_ACCEL_CAL:
279                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
280                                 break;
281                         case Altos.AO_LOG_RADIO_CAL:
282                                 out.printf ("# Radio cal: %d\n", record.a);
283                                 break;
284                         case Altos.AO_LOG_MAX_FLIGHT_LOG:
285                                 out.printf ("# Max flight log: %d\n", record.a);
286                                 break;
287                         case Altos.AO_LOG_MANUFACTURER:
288                                 out.printf ("# Manufacturer: %s\n", record.data);
289                                 break;
290                         case Altos.AO_LOG_PRODUCT:
291                                 out.printf ("# Product: %s\n", record.data);
292                                 break;
293                         case Altos.AO_LOG_SERIAL_NUMBER:
294                                 out.printf ("# Serial number: %d\n", record.a);
295                                 break;
296                         case Altos.AO_LOG_SOFTWARE_VERSION:
297                                 out.printf ("# Software version: %s\n", record.data);
298                                 break;
299                         }
300                 }
301         }
302
303         /*
304          * Given an AO_LOG_GPS_TIME record with correct time, and one
305          * missing time, rewrite the missing time values with the good
306          * ones, assuming that the difference between them is 'diff' seconds
307          */
308         void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
309
310                 int diff = (bad.tick - good.tick + 50) / 100;
311
312                 int hour = (good.a & 0xff);
313                 int minute = (good.a >> 8);
314                 int second = (good.b & 0xff);
315                 int flags = (good.b >> 8);
316                 int seconds = hour * 3600 + minute * 60 + second;
317
318                 /* Make sure this looks like a good GPS value */
319                 if ((flags & Altos.AO_GPS_NUM_SAT_MASK) >> Altos.AO_GPS_NUM_SAT_SHIFT < 4)
320                         flags = (flags & ~Altos.AO_GPS_NUM_SAT_MASK) | (4 << Altos.AO_GPS_NUM_SAT_SHIFT);
321                 flags |= Altos.AO_GPS_RUNNING;
322                 flags |= Altos.AO_GPS_VALID;
323
324                 int new_seconds = seconds + diff;
325                 if (new_seconds < 0)
326                         new_seconds += 24 * 3600;
327                 int new_second = (new_seconds % 60);
328                 int new_minutes = (new_seconds / 60);
329                 int new_minute = (new_minutes % 60);
330                 int new_hours = (new_minutes / 60);
331                 int new_hour = (new_hours % 24);
332
333                 bad.a = new_hour + (new_minute << 8);
334                 bad.b = new_second + (flags << 8);
335         }
336
337         /*
338          * Read the whole file, dumping records into a RB tree so
339          * we can enumerate them in time order -- the eeprom data
340          * are sometimes out of order with GPS data getting timestamps
341          * matching the first packet out of the GPS unit but not
342          * written until the final GPS packet has been received.
343          */
344         public AltosEepromIterable (FileInputStream input) {
345                 records = new TreeSet<AltosOrderedRecord>();
346
347                 AltosOrderedRecord last_gps_time = null;
348
349                 int index = 0;
350                 int prev_tick = 0;
351                 boolean prev_tick_valid = false;
352                 boolean missing_time = false;
353
354                 try {
355                         for (;;) {
356                                 String line = AltosRecord.gets(input);
357                                 if (line == null)
358                                         break;
359                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
360                                 if (record == null)
361                                         break;
362                                 if (record.cmd == Altos.AO_LOG_INVALID)
363                                         continue;
364                                 prev_tick = record.tick;
365                                 if (record.cmd < Altos.AO_LOG_CONFIG_VERSION)
366                                         prev_tick_valid = true;
367                                 if (record.cmd == Altos.AO_LOG_FLIGHT) {
368                                         flight_record = record;
369                                         continue;
370                                 }
371
372                                 /* Two firmware bugs caused the loss of some GPS data.
373                                  * The flight date would never be recorded, and often
374                                  * the flight time would get overwritten by another
375                                  * record. Detect the loss of the GPS date and fix up the
376                                  * missing time records
377                                  */
378                                 if (record.cmd == Altos.AO_LOG_GPS_DATE) {
379                                         gps_date_record = record;
380                                         continue;
381                                 }
382
383                                 /* go back and fix up any missing time values */
384                                 if (record.cmd == Altos.AO_LOG_GPS_TIME) {
385                                         last_gps_time = record;
386                                         if (missing_time) {
387                                                 Iterator<AltosOrderedRecord> iterator = records.iterator();
388                                                 while (iterator.hasNext()) {
389                                                         AltosOrderedRecord old = iterator.next();
390                                                         if (old.cmd == Altos.AO_LOG_GPS_TIME &&
391                                                             old.a == -1 && old.b == -1)
392                                                         {
393                                                                 update_time(record, old);
394                                                         }
395                                                 }
396                                                 missing_time = false;
397                                         }
398                                 }
399
400                                 if (record.cmd == Altos.AO_LOG_GPS_LAT) {
401                                         if (last_gps_time == null || last_gps_time.tick != record.tick) {
402                                                 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(Altos.AO_LOG_GPS_TIME,
403                                                                                                          record.tick,
404                                                                                                          -1, -1, index-1);
405                                                 if (last_gps_time != null)
406                                                         update_time(last_gps_time, add_gps_time);
407                                                 else
408                                                         missing_time = true;
409
410                                                 records.add(add_gps_time);
411                                                 record.index = index++;
412                                         }
413                                 }
414                                 records.add(record);
415
416                                 /* Bail after reading the 'landed' record; we're all done */
417                                 if (record.cmd == Altos.AO_LOG_STATE &&
418                                     record.a == Altos.ao_flight_landed)
419                                         break;
420                         }
421                 } catch (IOException io) {
422                 } catch (ParseException pe) {
423                 }
424                 try {
425                         input.close();
426                 } catch (IOException ie) {
427                 }
428         }
429 }