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