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