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