Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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
204                 /* Pull in static data from the flight and gps_date records */
205                 if (flight_record != null)
206                         update_state(state, flight_record, eeprom);
207                 if (gps_date_record != null)
208                         update_state(state, gps_date_record, eeprom);
209
210                 while (iterator.hasNext()) {
211                         record = iterator.next();
212                         if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
213                                 AltosRecordTM r = state.clone();
214                                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
215                                 list.add(r);
216                         }
217                         update_state(state, record, eeprom);
218                 }
219                 AltosRecordTM r = state.clone();
220                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
221                 list.add(r);
222         return list;
223         }
224
225         public Iterator<AltosRecord> iterator() {
226                 if (list == null)
227                         list = make_list();
228                 return list.iterator();
229         }
230
231         public boolean has_gps() { return has_gps; }
232         public boolean has_accel() { return has_accel; }
233         public boolean has_ignite() { return has_ignite; }
234
235         public void write_comments(PrintStream out) {
236                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
237                 out.printf("# Comments\n");
238                 while (iterator.hasNext()) {
239                         AltosOrderedRecord      record = iterator.next();
240                         switch (record.cmd) {
241                         case AltosLib.AO_LOG_CONFIG_VERSION:
242                                 out.printf("# Config version: %s\n", record.data);
243                                 break;
244                         case AltosLib.AO_LOG_MAIN_DEPLOY:
245                                 out.printf("# Main deploy: %s\n", record.a);
246                                 break;
247                         case AltosLib.AO_LOG_APOGEE_DELAY:
248                                 out.printf("# Apogee delay: %s\n", record.a);
249                                 break;
250                         case AltosLib.AO_LOG_RADIO_CHANNEL:
251                                 out.printf("# Radio channel: %s\n", record.a);
252                                 break;
253                         case AltosLib.AO_LOG_CALLSIGN:
254                                 out.printf("# Callsign: %s\n", record.data);
255                                 break;
256                         case AltosLib.AO_LOG_ACCEL_CAL:
257                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
258                                 break;
259                         case AltosLib.AO_LOG_RADIO_CAL:
260                                 out.printf ("# Radio cal: %d\n", record.a);
261                                 break;
262                         case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
263                                 out.printf ("# Max flight log: %d\n", record.a);
264                                 break;
265                         case AltosLib.AO_LOG_MANUFACTURER:
266                                 out.printf ("# Manufacturer: %s\n", record.data);
267                                 break;
268                         case AltosLib.AO_LOG_PRODUCT:
269                                 out.printf ("# Product: %s\n", record.data);
270                                 break;
271                         case AltosLib.AO_LOG_SERIAL_NUMBER:
272                                 out.printf ("# Serial number: %d\n", record.a);
273                                 break;
274                         case AltosLib.AO_LOG_SOFTWARE_VERSION:
275                                 out.printf ("# Software version: %s\n", record.data);
276                                 break;
277                         case AltosLib.AO_LOG_BARO_RESERVED:
278                                 out.printf ("# Baro reserved: %d\n", record.a);
279                                 break;
280                         case AltosLib.AO_LOG_BARO_SENS:
281                                 out.printf ("# Baro sens: %d\n", record.a);
282                                 break;
283                         case AltosLib.AO_LOG_BARO_OFF:
284                                 out.printf ("# Baro off: %d\n", record.a);
285                                 break;
286                         case AltosLib.AO_LOG_BARO_TCS:
287                                 out.printf ("# Baro tcs: %d\n", record.a);
288                                 break;
289                         case AltosLib.AO_LOG_BARO_TCO:
290                                 out.printf ("# Baro tco: %d\n", record.a);
291                                 break;
292                         case AltosLib.AO_LOG_BARO_TREF:
293                                 out.printf ("# Baro tref: %d\n", record.a);
294                                 break;
295                         case AltosLib.AO_LOG_BARO_TEMPSENS:
296                                 out.printf ("# Baro tempsens: %d\n", record.a);
297                                 break;
298                         case AltosLib.AO_LOG_BARO_CRC:
299                                 out.printf ("# Baro crc: %d\n", record.a);
300                                 break;
301                         }
302                 }
303         }
304
305         /*
306          * Given an AO_LOG_GPS_TIME record with correct time, and one
307          * missing time, rewrite the missing time values with the good
308          * ones, assuming that the difference between them is 'diff' seconds
309          */
310         void update_time(AltosOrderedRecord good, AltosOrderedRecord bad) {
311
312                 int diff = (bad.tick - good.tick + 50) / 100;
313
314                 int hour = (good.a & 0xff);
315                 int minute = (good.a >> 8);
316                 int second = (good.b & 0xff);
317                 int flags = (good.b >> 8);
318                 int seconds = hour * 3600 + minute * 60 + second;
319
320                 /* Make sure this looks like a good GPS value */
321                 if ((flags & AltosLib.AO_GPS_NUM_SAT_MASK) >> AltosLib.AO_GPS_NUM_SAT_SHIFT < 4)
322                         flags = (flags & ~AltosLib.AO_GPS_NUM_SAT_MASK) | (4 << AltosLib.AO_GPS_NUM_SAT_SHIFT);
323                 flags |= AltosLib.AO_GPS_RUNNING;
324                 flags |= AltosLib.AO_GPS_VALID;
325
326                 int new_seconds = seconds + diff;
327                 if (new_seconds < 0)
328                         new_seconds += 24 * 3600;
329                 int new_second = (new_seconds % 60);
330                 int new_minutes = (new_seconds / 60);
331                 int new_minute = (new_minutes % 60);
332                 int new_hours = (new_minutes / 60);
333                 int new_hour = (new_hours % 24);
334
335                 bad.a = new_hour + (new_minute << 8);
336                 bad.b = new_second + (flags << 8);
337         }
338
339         /*
340          * Read the whole file, dumping records into a RB tree so
341          * we can enumerate them in time order -- the eeprom data
342          * are sometimes out of order with GPS data getting timestamps
343          * matching the first packet out of the GPS unit but not
344          * written until the final GPS packet has been received.
345          */
346         public AltosEepromIterable (FileInputStream input) {
347                 records = new TreeSet<AltosOrderedRecord>();
348
349                 AltosOrderedRecord last_gps_time = null;
350
351                 int index = 0;
352                 int prev_tick = 0;
353                 boolean prev_tick_valid = false;
354                 boolean missing_time = false;
355
356                 try {
357                         for (;;) {
358                                 String line = AltosLib.gets(input);
359                                 if (line == null)
360                                         break;
361                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, prev_tick, prev_tick_valid);
362                                 if (record.cmd == AltosLib.AO_LOG_INVALID)
363                                         continue;
364                                 prev_tick = record.tick;
365                                 if (record.cmd < AltosLib.AO_LOG_CONFIG_VERSION)
366                                         prev_tick_valid = true;
367                                 if (record.cmd == AltosLib.AO_LOG_FLIGHT) {
368                                         flight_record = record;
369                                         continue;
370                                 }
371
372                                 /* Two firmware bugs caused the loss of some GPS data.
373                                  * The flight date would never be recorded, and often
374                                  * the flight time would get overwritten by another
375                                  * record. Detect the loss of the GPS date and fix up the
376                                  * missing time records
377                                  */
378                                 if (record.cmd == AltosLib.AO_LOG_GPS_DATE) {
379                                         gps_date_record = record;
380                                         continue;
381                                 }
382
383                                 /* go back and fix up any missing time values */
384                                 if (record.cmd == AltosLib.AO_LOG_GPS_TIME) {
385                                         last_gps_time = record;
386                                         if (missing_time) {
387                                                 Iterator<AltosOrderedRecord> iterator = records.iterator();
388                                                 while (iterator.hasNext()) {
389                                                         AltosOrderedRecord old = iterator.next();
390                                                         if (old.cmd == AltosLib.AO_LOG_GPS_TIME &&
391                                                             old.a == -1 && old.b == -1)
392                                                         {
393                                                                 update_time(record, old);
394                                                         }
395                                                 }
396                                                 missing_time = false;
397                                         }
398                                 }
399
400                                 if (record.cmd == AltosLib.AO_LOG_GPS_LAT) {
401                                         if (last_gps_time == null || last_gps_time.tick != record.tick) {
402                                                 AltosOrderedRecord add_gps_time = new AltosOrderedRecord(AltosLib.AO_LOG_GPS_TIME,
403                                                                                                          record.tick,
404                                                                                                          -1, -1, index-1);
405                                                 if (last_gps_time != null)
406                                                         update_time(last_gps_time, add_gps_time);
407                                                 else
408                                                         missing_time = true;
409
410                                                 records.add(add_gps_time);
411                                                 record.index = index++;
412                                         }
413                                 }
414                                 records.add(record);
415
416                                 /* Bail after reading the 'landed' record; we're all done */
417                                 if (record.cmd == AltosLib.AO_LOG_STATE &&
418                                     record.a == AltosLib.ao_flight_landed)
419                                         break;
420                         }
421                 } catch (IOException io) {
422                 } catch (ParseException pe) {
423                 }
424                 try {
425                         input.close();
426                 } catch (IOException ie) {
427                 }
428         }
429 }