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