altoslib: Move computed state from AltosRecord to AltosState
[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_TEMP_VOLT:
110                         state.v_batt = record.v_batt();
111                         state.v_pyro = record.v_pbatt();
112                         for (int i = 0; i < AltosRecordMM.num_sense; i++)
113                                 state.sense[i] = record.sense(i);
114                         eeprom.seen |= seen_temp_volt;
115                         break;
116                 case AltosLib.AO_LOG_STATE:
117                         state.state = record.state();
118                         break;
119                 case AltosLib.AO_LOG_GPS_TIME:
120                         eeprom.gps_tick = state.tick;
121                         AltosGPS old = state.gps;
122                         state.gps = new AltosGPS();
123
124                         /* GPS date doesn't get repeated through the file */
125                         if (old != null) {
126                                 state.gps.year = old.year;
127                                 state.gps.month = old.month;
128                                 state.gps.day = old.day;
129                         }
130                         state.gps.hour = (record.a & 0xff);
131                         state.gps.minute = (record.a >> 8);
132                         state.gps.second = (record.b & 0xff);
133
134                         int flags = (record.b >> 8);
135                         state.gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
136                         state.gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
137                         state.gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
138                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
139                         state.new_gps = true;
140                         has_gps = true;
141                         break;
142                 case AltosLib.AO_LOG_GPS_LAT:
143                         int lat32 = record.a | (record.b << 16);
144                         state.gps.lat = (double) lat32 / 1e7;
145                         break;
146                 case AltosLib.AO_LOG_GPS_LON:
147                         int lon32 = record.a | (record.b << 16);
148                         state.gps.lon = (double) lon32 / 1e7;
149                         break;
150                 case AltosLib.AO_LOG_GPS_ALT:
151                         state.gps.alt = record.a;
152                         break;
153                 case AltosLib.AO_LOG_GPS_SAT:
154                         if (state.tick == eeprom.gps_tick) {
155                                 int svid = record.a;
156                                 int c_n0 = record.b >> 8;
157                                 state.gps.add_sat(svid, c_n0);
158                         }
159                         break;
160                 case AltosLib.AO_LOG_GPS_DATE:
161                         state.gps.year = (record.a & 0xff) + 2000;
162                         state.gps.month = record.a >> 8;
163                         state.gps.day = record.b & 0xff;
164                         break;
165
166                 case AltosLib.AO_LOG_CONFIG_VERSION:
167                         break;
168                 case AltosLib.AO_LOG_MAIN_DEPLOY:
169                         break;
170                 case AltosLib.AO_LOG_APOGEE_DELAY:
171                         break;
172                 case AltosLib.AO_LOG_RADIO_CHANNEL:
173                         break;
174                 case AltosLib.AO_LOG_CALLSIGN:
175                         state.callsign = record.data;
176                         break;
177                 case AltosLib.AO_LOG_ACCEL_CAL:
178                         state.accel_plus_g = record.a;
179                         state.accel_minus_g = record.b;
180                         break;
181                 case AltosLib.AO_LOG_RADIO_CAL:
182                         break;
183                 case AltosLib.AO_LOG_MANUFACTURER:
184                         break;
185                 case AltosLib.AO_LOG_PRODUCT:
186                         break;
187                 case AltosLib.AO_LOG_SERIAL_NUMBER:
188                         state.serial = record.a;
189                         break;
190                 case AltosLib.AO_LOG_SOFTWARE_VERSION:
191                         break;
192                 case AltosLib.AO_LOG_BARO_RESERVED:
193                         baro.reserved = record.a;
194                         break;
195                 case AltosLib.AO_LOG_BARO_SENS:
196                         baro.sens =record.a;
197                         break;
198                 case AltosLib.AO_LOG_BARO_OFF:
199                         baro.off =record.a;
200                         break;
201                 case AltosLib.AO_LOG_BARO_TCS:
202                         baro.tcs =record.a;
203                         break;
204                 case AltosLib.AO_LOG_BARO_TCO:
205                         baro.tco =record.a;
206                         break;
207                 case AltosLib.AO_LOG_BARO_TREF:
208                         baro.tref =record.a;
209                         break;
210                 case AltosLib.AO_LOG_BARO_TEMPSENS:
211                         baro.tempsens =record.a;
212                         break;
213                 case AltosLib.AO_LOG_BARO_CRC:
214                         baro.crc =record.a;
215                         break;
216                 }
217                 state.seen |= eeprom.seen;
218         }
219
220         LinkedList<AltosRecord> make_list() {
221                 LinkedList<AltosRecord>         list = new LinkedList<AltosRecord>();
222                 Iterator<AltosOrderedMegaRecord>        iterator = records.iterator();
223                 AltosOrderedMegaRecord          record = null;
224                 AltosRecordMM                   state = new AltosRecordMM();
225                 //boolean                               last_reported = false;
226                 EepromState                     eeprom = new EepromState();
227
228                 state.state = AltosLib.ao_flight_pad;
229                 state.accel_plus_g = 15758;
230                 state.accel_minus_g = 16294;
231
232                 /* Pull in static data from the flight and gps_date records */
233                 if (flight_record != null)
234                         update_state(state, flight_record, eeprom);
235                 if (gps_date_record != null)
236                         update_state(state, gps_date_record, eeprom);
237
238                 while (iterator.hasNext()) {
239                         record = iterator.next();
240                         if ((eeprom.seen & seen_basic) == seen_basic && record.tick != state.tick) {
241                                 AltosRecordMM r = state.clone();
242                                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
243                                 list.add(r);
244                         }
245                         update_state(state, record, eeprom);
246                 }
247                 AltosRecordMM r = state.clone();
248                 r.time = (r.tick - eeprom.boost_tick) / 100.0;
249                 list.add(r);
250                 return list;
251         }
252
253         public Iterator<AltosRecord> iterator() {
254                 if (list == null)
255                         list = make_list();
256                 return list.iterator();
257         }
258
259         public boolean has_gps() { return has_gps; }
260         public boolean has_accel() { return has_accel; }
261         public boolean has_ignite() { return has_ignite; }
262
263         public void write_comments(PrintStream out) {
264                 Iterator<AltosOrderedMegaRecord>        iterator = records.iterator();
265                 out.printf("# Comments\n");
266                 while (iterator.hasNext()) {
267                         AltosOrderedMegaRecord  record = iterator.next();
268                         switch (record.cmd) {
269                         case AltosLib.AO_LOG_CONFIG_VERSION:
270                                 out.printf("# Config version: %s\n", record.data);
271                                 break;
272                         case AltosLib.AO_LOG_MAIN_DEPLOY:
273                                 out.printf("# Main deploy: %s\n", record.a);
274                                 break;
275                         case AltosLib.AO_LOG_APOGEE_DELAY:
276                                 out.printf("# Apogee delay: %s\n", record.a);
277                                 break;
278                         case AltosLib.AO_LOG_RADIO_CHANNEL:
279                                 out.printf("# Radio channel: %s\n", record.a);
280                                 break;
281                         case AltosLib.AO_LOG_CALLSIGN:
282                                 out.printf("# Callsign: %s\n", record.data);
283                                 break;
284                         case AltosLib.AO_LOG_ACCEL_CAL:
285                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
286                                 break;
287                         case AltosLib.AO_LOG_RADIO_CAL:
288                                 out.printf ("# Radio cal: %d\n", record.a);
289                                 break;
290                         case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
291                                 out.printf ("# Max flight log: %d\n", record.a);
292                                 break;
293                         case AltosLib.AO_LOG_MANUFACTURER:
294                                 out.printf ("# Manufacturer: %s\n", record.data);
295                                 break;
296                         case AltosLib.AO_LOG_PRODUCT:
297                                 out.printf ("# Product: %s\n", record.data);
298                                 break;
299                         case AltosLib.AO_LOG_SERIAL_NUMBER:
300                                 out.printf ("# Serial number: %d\n", record.a);
301                                 break;
302                         case AltosLib.AO_LOG_SOFTWARE_VERSION:
303                                 out.printf ("# Software version: %s\n", record.data);
304                                 break;
305                         case AltosLib.AO_LOG_BARO_RESERVED:
306                                 out.printf ("# Baro reserved: %d\n", record.a);
307                                 break;
308                         case AltosLib.AO_LOG_BARO_SENS:
309                                 out.printf ("# Baro sens: %d\n", record.a);
310                                 break;
311                         case AltosLib.AO_LOG_BARO_OFF:
312                                 out.printf ("# Baro off: %d\n", record.a);
313                                 break;
314                         case AltosLib.AO_LOG_BARO_TCS:
315                                 out.printf ("# Baro tcs: %d\n", record.a);
316                                 break;
317                         case AltosLib.AO_LOG_BARO_TCO:
318                                 out.printf ("# Baro tco: %d\n", record.a);
319                                 break;
320                         case AltosLib.AO_LOG_BARO_TREF:
321                                 out.printf ("# Baro tref: %d\n", record.a);
322                                 break;
323                         case AltosLib.AO_LOG_BARO_TEMPSENS:
324                                 out.printf ("# Baro tempsens: %d\n", record.a);
325                                 break;
326                         case AltosLib.AO_LOG_BARO_CRC:
327                                 out.printf ("# Baro crc: %d\n", record.a);
328                                 break;
329                         }
330                 }
331         }
332
333         /*
334          * Read the whole file, dumping records into a RB tree so
335          * we can enumerate them in time order -- the eeprom data
336          * are sometimes out of order with GPS data getting timestamps
337          * matching the first packet out of the GPS unit but not
338          * written until the final GPS packet has been received.
339          */
340         public AltosEepromMegaIterable (FileInputStream input) {
341                 records = new TreeSet<AltosOrderedMegaRecord>();
342
343                 AltosOrderedMegaRecord last_gps_time = null;
344
345                 baro = new AltosMs5607();
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                                 AltosOrderedMegaRecord record = new AltosOrderedMegaRecord(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                                 records.add(record);
369
370                                 /* Bail after reading the 'landed' record; we're all done */
371                                 if (record.cmd == AltosLib.AO_LOG_STATE &&
372                                     record.a == AltosLib.ao_flight_landed)
373                                         break;
374                         }
375                 } catch (IOException io) {
376                 } catch (ParseException pe) {
377                 }
378                 try {
379                         input.close();
380                 } catch (IOException ie) {
381                 }
382         }
383 }