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