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