altosui: Initialize flight velocity to zero when reading eeprom files
[fw/altos] / altoslib / AltosEepromIterable.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromIterable 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         AltosEepromRecord       flight_record;
41         AltosEepromRecord       gps_date_record;
42
43         TreeSet<AltosOrderedRecord>     records;
44
45         LinkedList<AltosRecord> list;
46
47         class EepromState {
48                 int     seen;
49                 int     n_pad_samples;
50                 double  ground_pres;
51                 int     gps_tick;
52                 int     boost_tick;
53                 int     sensor_tick;
54
55                 EepromState() {
56                         seen = 0;
57                         n_pad_samples = 0;
58                         ground_pres = 0.0;
59                         gps_tick = 0;
60                 }
61         }
62
63         void update_state(AltosRecordTM state, AltosEepromRecord record, EepromState eeprom) {
64                 state.tick = record.tick;
65                 switch (record.cmd) {
66                 case AltosLib.AO_LOG_FLIGHT:
67                         eeprom.seen |= seen_flight;
68                         state.ground_accel = record.a;
69                         state.flight_accel = record.a;
70                         state.flight = record.b;
71                         eeprom.boost_tick = record.tick;
72                         break;
73                 case AltosLib.AO_LOG_SENSOR:
74                         state.accel = record.a;
75                         state.pres = record.b;
76                         if (state.state < AltosLib.ao_flight_boost) {
77                                 eeprom.n_pad_samples++;
78                                 eeprom.ground_pres += state.pres;
79                                 state.ground_pres = (int) (eeprom.ground_pres / eeprom.n_pad_samples);
80                                 state.flight_pres = state.ground_pres;
81                         } else {
82                                 state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
83                         }
84                         state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
85                         if ((eeprom.seen & seen_sensor) == 0)
86                                 eeprom.sensor_tick = record.tick - 1;
87                         state.flight_vel += (state.accel_plus_g - state.accel) * (record.tick - eeprom.sensor_tick);
88                         eeprom.seen |= seen_sensor;
89                         eeprom.sensor_tick = record.tick;
90                         has_accel = true;
91                         break;
92                 case AltosLib.AO_LOG_PRESSURE:
93                         state.pres = record.b;
94                         state.flight_pres = state.pres;
95                         if (eeprom.n_pad_samples == 0) {
96                                 eeprom.n_pad_samples++;
97                                 state.ground_pres = state.pres;
98                         }
99                         eeprom.seen |= seen_sensor;
100                         break;
101                 case AltosLib.AO_LOG_TEMP_VOLT:
102                         state.temp = record.a;
103                         state.batt = record.b;
104                         eeprom.seen |= seen_temp_volt;
105                         break;
106                 case AltosLib.AO_LOG_DEPLOY:
107                         state.drogue = record.a;
108                         state.main = record.b;
109                         eeprom.seen |= seen_deploy;
110                         has_ignite = true;
111                         break;
112                 case AltosLib.AO_LOG_STATE:
113                         state.state = record.a;
114                         break;
115                 case AltosLib.AO_LOG_GPS_TIME:
116                         eeprom.gps_tick = state.tick;
117                         AltosGPS old = state.gps;
118                         state.gps = new AltosGPS();
119
120                         /* GPS date doesn't get repeated through the file */
121                         if (old != null) {
122                                 state.gps.year = old.year;
123                                 state.gps.month = old.month;
124                                 state.gps.day = old.day;
125                         }
126                         state.gps.hour = (record.a & 0xff);
127                         state.gps.minute = (record.a >> 8);
128                         state.gps.second = (record.b & 0xff);
129
130                         int flags = (record.b >> 8);
131                         state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
132                         state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
133                         state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
134                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
135                         state.new_gps = true;
136                         has_gps = true;
137                         break;
138                 case AltosLib.AO_LOG_GPS_LAT:
139                         int lat32 = record.a | (record.b << 16);
140                         state.gps.lat = (double) lat32 / 1e7;
141                         break;
142                 case AltosLib.AO_LOG_GPS_LON:
143                         int lon32 = record.a | (record.b << 16);
144                         state.gps.lon = (double) lon32 / 1e7;
145                         break;
146                 case AltosLib.AO_LOG_GPS_ALT:
147                         state.gps.alt = record.a;
148                         break;
149                 case AltosLib.AO_LOG_GPS_SAT:
150                         if (state.tick == eeprom.gps_tick) {
151                                 int svid = record.a;
152                                 int c_n0 = record.b >> 8;
153                                 state.gps.add_sat(svid, c_n0);
154                         }
155                         break;
156                 case AltosLib.AO_LOG_GPS_DATE:
157                         state.gps.year = (record.a & 0xff) + 2000;
158                         state.gps.month = record.a >> 8;
159                         state.gps.day = record.b & 0xff;
160                         break;
161
162                 case AltosLib.AO_LOG_CONFIG_VERSION:
163                         break;
164                 case AltosLib.AO_LOG_MAIN_DEPLOY:
165                         break;
166                 case AltosLib.AO_LOG_APOGEE_DELAY:
167                         break;
168                 case AltosLib.AO_LOG_RADIO_CHANNEL:
169                         break;
170                 case AltosLib.AO_LOG_CALLSIGN:
171                         state.callsign = record.data;
172                         break;
173                 case AltosLib.AO_LOG_ACCEL_CAL:
174                         state.accel_plus_g = record.a;
175                         state.accel_minus_g = record.b;
176                         break;
177                 case AltosLib.AO_LOG_RADIO_CAL:
178                         break;
179                 case AltosLib.AO_LOG_MANUFACTURER:
180                         break;
181                 case AltosLib.AO_LOG_PRODUCT:
182                         break;
183                 case AltosLib.AO_LOG_SERIAL_NUMBER:
184                         state.serial = record.a;
185                         break;
186                 case AltosLib.AO_LOG_SOFTWARE_VERSION:
187                         break;
188                 }
189                 state.seen |= eeprom.seen;
190         }
191
192         LinkedList<AltosRecord> make_list() {
193                 LinkedList<AltosRecord>         list = new LinkedList<AltosRecord>();
194                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
195                 AltosOrderedRecord              record = null;
196                 AltosRecordTM                   state = new AltosRecordTM();
197                 //boolean                               last_reported = false;
198                 EepromState                     eeprom = new EepromState();
199
200                 state.state = AltosLib.ao_flight_pad;
201                 state.accel_plus_g = 15758;
202                 state.accel_minus_g = 16294;
203                 state.flight_vel = 0;
204
205                 /* Pull in static data from the flight and gps_date records */
206                 if (flight_record != null)
207                         update_state(state, flight_record, eeprom);
208                 if (gps_date_record != null)
209                         update_state(state, gps_date_record, eeprom);
210
211                 while (iterator.hasNext()) {
212                         record = iterator.next();
213                         if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
214                                 AltosRecordTM r = state.clone();
215                                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
216                                 list.add(r);
217                         }
218                         update_state(state, record, eeprom);
219                 }
220                 AltosRecordTM r = state.clone();
221                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
222                 list.add(r);
223         return list;
224         }
225
226         public Iterator<AltosRecord> iterator() {
227                 if (list == null)
228                         list = make_list();
229                 return list.iterator();
230         }
231
232         public boolean has_gps() { return has_gps; }
233         public boolean has_accel() { return has_accel; }
234         public boolean has_ignite() { return has_ignite; }
235
236         public void write_comments(PrintStream out) {
237                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
238                 out.printf("# Comments\n");
239                 while (iterator.hasNext()) {
240                         AltosOrderedRecord      record = iterator.next();
241                         switch (record.cmd) {
242                         case AltosLib.AO_LOG_CONFIG_VERSION:
243                                 out.printf("# Config version: %s\n", record.data);
244                                 break;
245                         case AltosLib.AO_LOG_MAIN_DEPLOY:
246                                 out.printf("# Main deploy: %s\n", record.a);
247                                 break;
248                         case AltosLib.AO_LOG_APOGEE_DELAY:
249                                 out.printf("# Apogee delay: %s\n", record.a);
250                                 break;
251                         case AltosLib.AO_LOG_RADIO_CHANNEL:
252                                 out.printf("# Radio channel: %s\n", record.a);
253                                 break;
254                         case AltosLib.AO_LOG_CALLSIGN:
255                                 out.printf("# Callsign: %s\n", record.data);
256                                 break;
257                         case AltosLib.AO_LOG_ACCEL_CAL:
258                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
259                                 break;
260                         case AltosLib.AO_LOG_RADIO_CAL:
261                                 out.printf ("# Radio cal: %d\n", record.a);
262                                 break;
263                         case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
264                                 out.printf ("# Max flight log: %d\n", record.a);
265                                 break;
266                         case AltosLib.AO_LOG_MANUFACTURER:
267                                 out.printf ("# Manufacturer: %s\n", record.data);
268                                 break;
269                         case AltosLib.AO_LOG_PRODUCT:
270                                 out.printf ("# Product: %s\n", record.data);
271                                 break;
272                         case AltosLib.AO_LOG_SERIAL_NUMBER:
273                                 out.printf ("# Serial number: %d\n", record.a);
274                                 break;
275                         case AltosLib.AO_LOG_SOFTWARE_VERSION:
276                                 out.printf ("# Software version: %s\n", record.data);
277                                 break;
278                         case AltosLib.AO_LOG_BARO_RESERVED:
279                                 out.printf ("# Baro reserved: %d\n", record.a);
280                                 break;
281                         case AltosLib.AO_LOG_BARO_SENS:
282                                 out.printf ("# Baro sens: %d\n", record.a);
283                                 break;
284                         case AltosLib.AO_LOG_BARO_OFF:
285                                 out.printf ("# Baro off: %d\n", record.a);
286                                 break;
287                         case AltosLib.AO_LOG_BARO_TCS:
288                                 out.printf ("# Baro tcs: %d\n", record.a);
289                                 break;
290                         case AltosLib.AO_LOG_BARO_TCO:
291                                 out.printf ("# Baro tco: %d\n", record.a);
292                                 break;
293                         case AltosLib.AO_LOG_BARO_TREF:
294                                 out.printf ("# Baro tref: %d\n", record.a);
295                                 break;
296                         case AltosLib.AO_LOG_BARO_TEMPSENS:
297                                 out.printf ("# Baro tempsens: %d\n", record.a);
298                                 break;
299                         case AltosLib.AO_LOG_BARO_CRC:
300                                 out.printf ("# Baro crc: %d\n", record.a);
301                                 break;
302                         }
303                 }
304         }
305
306         /*
307          * Given an AO_LOG_GPS_TIME record with correct time, and one
308          * missing time, rewrite the missing time values with the good
309          * ones, assuming that the difference between them is 'diff' seconds
310          */
311         void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
312
313                 int diff = (bad.tick - good.tick + 50) / 100;
314
315                 int hour = (good.a & 0xff);
316                 int minute = (good.a >> 8);
317                 int second = (good.b & 0xff);
318                 int flags = (good.b >> 8);
319                 int seconds = hour * 3600 + minute * 60 + second;
320
321                 /* Make sure this looks like a good GPS value */
322                 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
323                         flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
324                 flags |= AltosLib.AO_GPS_RUNNING;
325                 flags |= AltosLib.AO_GPS_VALID;
326
327                 int new_seconds = seconds + diff;
328                 if (new_seconds < 0)
329                         new_seconds += 24 * 3600;
330                 int new_second = (new_seconds % 60);
331                 int new_minutes = (new_seconds / 60);
332                 int new_minute = (new_minutes % 60);
333                 int new_hours = (new_minutes / 60);
334                 int new_hour = (new_hours % 24);
335
336                 bad.a = new_hour + (new_minute << 8);
337                 bad.b = new_second + (flags << 8);
338         }
339
340         /*
341          * Read the whole file, dumping records into a RB tree so
342          * we can enumerate them in time order -- the eeprom data
343          * are sometimes out of order with GPS data getting timestamps
344          * matching the first packet out of the GPS unit but not
345          * written until the final GPS packet has been received.
346          */
347         public AltosEepromIterable (FileInputStream input) {
348                 records = new TreeSet<AltosOrderedRecord>();
349
350                 AltosOrderedRecord last_gps_time = null;
351
352                 int index = 0;
353                 int prev_tick = 0;
354                 boolean prev_tick_valid = false;
355                 boolean missing_time = false;
356
357                 try {
358                         for (;;) {
359                                 String line = AltosLib.gets(input);
360                                 if (line == null)
361                                         break;
362                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
363                                 if (record.cmd == AltosLib.AO_LOG_INVALID)
364                                         continue;
365                                 prev_tick = record.tick;
366                                 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
367                                         prev_tick_valid = true;
368                                 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
369                                         flight_record = record;
370                                         continue;
371                                 }
372
373                                 /* Two firmware bugs caused the loss of some GPS data.
374                                  * The flight date would never be recorded, and often
375                                  * the flight time would get overwritten by another
376                                  * record. Detect the loss of the GPS date and fix up the
377                                  * missing time records
378                                  */
379                                 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
380                                         gps_date_record = record;
381                                         continue;
382                                 }
383
384                                 /* go back and fix up any missing time values */
385                                 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
386                                         last_gps_time = record;
387                                         if (missing_time) {
388                                                 Iterator<AltosOrderedRecord> iterator = records.iterator();
389                                                 while (iterator.hasNext()) {
390                                                         AltosOrderedRecord old = iterator.next();
391                                                         if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
392                                                             old.a == -1 && old.b == -1)
393                                                         {
394                                                                 update_time(record, old);
395                                                         }
396                                                 }
397                                                 missing_time = false;
398                                         }
399                                 }
400
401                                 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
402                                         if (last_gps_time == null || last_gps_time.tick != record.tick) {
403                                                 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
404                                                                                                          record.tick,
405                                                                                                          -1, -1, index-1);
406                                                 if (last_gps_time != null)
407                                                         update_time(last_gps_time, add_gps_time);
408                                                 else
409                                                         missing_time = true;
410
411                                                 records.add(add_gps_time);
412                                                 record.index = index++;
413                                         }
414                                 }
415                                 records.add(record);
416
417                                 /* Bail after reading the 'landed' record; we're all done */
418                                 if (record.cmd == AltosLib.AO_LOG_STATE &&
419                                     record.a == AltosLib.ao_flight_landed)
420                                         break;
421                         }
422                 } catch (IOException io) {
423                 } catch (ParseException pe) {
424                 }
425                 try {
426                         input.close();
427                 } catch (IOException ie) {
428                 }
429         }
430 }