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