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