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