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