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