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