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