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