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