c29fd90b12391105d6ed76bfb5841d6c7c9e0e1e
[fw/altos] / ao-tools / altosui / AltosEepromReader.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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import altosui.AltosRecord;
32 import altosui.AltosState;
33 import altosui.AltosDeviceDialog;
34 import altosui.AltosPreferences;
35 import altosui.AltosLog;
36 import altosui.AltosVoice;
37 import altosui.AltosEepromMonitor;
38
39 /*
40  * AltosRecords with an index field so they can be sorted by tick while preserving
41  * the original ordering for elements with matching ticks
42  */
43 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
44
45         int     index;
46
47         public AltosOrderedRecord(String line, int in_index, int prev_tick)
48                 throws ParseException {
49                 super(line);
50                 int new_tick = tick | (prev_tick & ~0xffff);
51                 if (new_tick < prev_tick) {
52                         if (prev_tick - new_tick > 0x8000)
53                                 new_tick += 0x10000;
54                 }
55                 tick = new_tick;
56                 index = in_index;
57         }
58
59         public int compareTo(AltosOrderedRecord o) {
60                 int     tick_diff = tick - o.tick;
61                 if (tick_diff != 0)
62                         return tick_diff;
63                 return index - o.index;
64         }
65 }
66
67 public class AltosEepromReader extends AltosReader {
68
69         static final int        seen_flight = 1;
70         static final int        seen_sensor = 2;
71         static final int        seen_temp_volt = 4;
72         static final int        seen_deploy = 8;
73         static final int        seen_gps_time = 16;
74         static final int        seen_gps_lat = 32;
75         static final int        seen_gps_lon = 64;
76
77         static final int        seen_basic = seen_flight|seen_sensor|seen_temp_volt|seen_deploy;
78
79         AltosRecord             state;
80         AltosOrderedRecord      record;
81
82         TreeSet<AltosOrderedRecord>     records;
83
84         Iterator<AltosOrderedRecord>                    record_iterator;
85
86         int                     seen;
87
88         int                     index;
89
90         boolean                 last_reported;
91
92         double                  ground_pres;
93         double                  ground_accel;
94
95         int                     n_pad_samples;
96
97         int                     gps_tick;
98
99         boolean                 saw_boost;
100
101         int                     boost_tick;
102
103         public AltosRecord read() throws IOException, ParseException {
104                 for (;;) {
105                         if (record == null) {
106                                 if (!record_iterator.hasNext()) {
107                                         if (last_reported)
108                                                 return null;
109                                         last_reported = true;
110                                         return state;
111                                 }
112                                 record = record_iterator.next();
113
114                                 if ((seen & seen_basic) == seen_basic && record.tick != state.tick) {
115                                         AltosRecord r = new AltosRecord(state);
116                                         r.time = (r.tick - boost_tick) / 100.0;
117                                         return r;
118                                 }
119                         }
120
121                         state.tick = record.tick;
122                         switch (record.cmd) {
123                         case Altos.AO_LOG_FLIGHT:
124                                 state.ground_accel = record.a;
125                                 state.flight = record.b;
126                                 seen |= seen_flight;
127                                 break;
128                         case Altos.AO_LOG_SENSOR:
129                                 state.accel = record.a;
130                                 state.pres = record.b;
131                                 if (state.state < Altos.ao_flight_boost) {
132                                         n_pad_samples++;
133                                         ground_pres += state.pres;
134                                         state.ground_pres = (int) (ground_pres / n_pad_samples);
135                                         state.flight_pres = state.ground_pres;
136                                         System.out.printf("ground pressure %d altitude %f\n",
137                                                           record.b, state.altitude());
138                                         ground_accel += state.accel;
139                                         state.ground_accel = (int) (ground_accel / n_pad_samples);
140                                         state.flight_accel = state.ground_accel;
141                                 } else {
142                                         state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
143                                         state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
144                                         state.flight_vel += (state.accel_plus_g - state.accel);
145                                 }
146                                 seen |= seen_sensor;
147                                 break;
148                         case Altos.AO_LOG_TEMP_VOLT:
149                                 state.temp = record.a;
150                                 state.batt = record.b;
151                                 seen |= seen_temp_volt;
152                                 break;
153                         case Altos.AO_LOG_DEPLOY:
154                                 state.drogue = record.a;
155                                 state.main = record.b;
156                                 seen |= seen_deploy;
157                                 break;
158                         case Altos.AO_LOG_STATE:
159                                 System.out.printf("state %d\n", record.a);
160                                 state.state = record.a;
161                                 break;
162                         case Altos.AO_LOG_GPS_TIME:
163                                 gps_tick = state.tick;
164                                 state.gps = new AltosGPS();
165                                 state.gps.hour = (record.a & 0xff);
166                                 state.gps.minute = (record.a >> 8);
167                                 state.gps.second = (record.b & 0xff);
168                                 int flags = (record.b >> 8);
169                                 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
170                                 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
171                                 state.gps.date_valid = (flags & Altos.AO_GPS_DATE_VALID) != 0;
172                                 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
173                                         Altos.AO_GPS_NUM_SAT_SHIFT;
174                                 break;
175                         case Altos.AO_LOG_GPS_LAT:
176                                 int lat32 = record.a | (record.b << 16);
177                                 state.gps.lat = (double) lat32 / 1e7;
178                                 break;
179                         case Altos.AO_LOG_GPS_LON:
180                                 int lon32 = record.a | (record.b << 16);
181                                 state.gps.lon = (double) lon32 / 1e7;
182                                 break;
183                         case Altos.AO_LOG_GPS_ALT:
184                                 state.gps.alt = record.a;
185                                 break;
186                         case Altos.AO_LOG_GPS_SAT:
187                                 if (state.tick == gps_tick) {
188                                         int svid = record.a;
189                                         int c_n0 = record.b >> 8;
190                                         state.gps.add_sat(svid, c_n0);
191                                 }
192                                 break;
193                         case Altos.AO_LOG_GPS_DATE:
194                                 state.gps.year = record.a & 0xff;
195                                 state.gps.month = record.a >> 8;
196                                 state.gps.day = record.b & 0xff;
197                                 break;
198
199                         case Altos.AO_LOG_CONFIG_VERSION:
200                                 break;
201                         case Altos.AO_LOG_MAIN_DEPLOY:
202                                 break;
203                         case Altos.AO_LOG_APOGEE_DELAY:
204                                 break;
205                         case Altos.AO_LOG_RADIO_CHANNEL:
206                                 break;
207                         case Altos.AO_LOG_CALLSIGN:
208                                 state.callsign = record.data;
209                                 break;
210                         case Altos.AO_LOG_ACCEL_CAL:
211                                 state.accel_plus_g = record.a;
212                                 state.accel_minus_g = record.b;
213                                 break;
214                         case Altos.AO_LOG_RADIO_CAL:
215                                 break;
216                         case Altos.AO_LOG_MANUFACTURER:
217                                 break;
218                         case Altos.AO_LOG_PRODUCT:
219                                 break;
220                         case Altos.AO_LOG_SERIAL_NUMBER:
221                                 break;
222                         case Altos.AO_LOG_SOFTWARE_VERSION:
223                                 break;
224                         }
225                         record = null;
226                 }
227         }
228
229         public void write_comments(PrintStream out) {
230                 Iterator<AltosOrderedRecord>    iterator = records.iterator();
231                 while (iterator.hasNext()) {
232                         AltosOrderedRecord      record = iterator.next();
233                         switch (record.cmd) {
234                         case Altos.AO_LOG_CONFIG_VERSION:
235                                 out.printf("# Config version: %s\n", record.data);
236                                 break;
237                         case Altos.AO_LOG_MAIN_DEPLOY:
238                                 out.printf("# Main deploy: %s\n", record.a);
239                                 break;
240                         case Altos.AO_LOG_APOGEE_DELAY:
241                                 out.printf("# Apogee delay: %s\n", record.a);
242                                 break;
243                         case Altos.AO_LOG_RADIO_CHANNEL:
244                                 out.printf("# Radio channel: %s\n", record.a);
245                                 break;
246                         case Altos.AO_LOG_CALLSIGN:
247                                 out.printf("# Callsign: %s\n", record.data);
248                                 break;
249                         case Altos.AO_LOG_ACCEL_CAL:
250                                 out.printf ("# Accel cal: %d %d\n", record.a, record.b);
251                                 break;
252                         case Altos.AO_LOG_RADIO_CAL:
253                                 out.printf ("# Radio cal: %d %d\n", record.a);
254                                 break;
255                         case Altos.AO_LOG_MANUFACTURER:
256                                 out.printf ("# Manufacturer: %s\n", record.data);
257                                 break;
258                         case Altos.AO_LOG_PRODUCT:
259                                 out.printf ("# Product: %s\n", record.data);
260                                 break;
261                         case Altos.AO_LOG_SERIAL_NUMBER:
262                                 out.printf ("# Serial number: %d\n", record.a);
263                                 break;
264                         case Altos.AO_LOG_SOFTWARE_VERSION:
265                                 out.printf ("# Software version: %s\n", record.data);
266                                 break;
267                         }
268                 }
269         }
270
271         /*
272          * Read the whole file, dumping records into a RB tree so
273          * we can enumerate them in time order -- the eeprom data
274          * are sometimes out of order with GPS data getting timestamps
275          * matching the first packet out of the GPS unit but not
276          * written until the final GPS packet has been received.
277          */
278         public AltosEepromReader (FileInputStream input) {
279                 state = new AltosRecord();
280                 state.state = Altos.ao_flight_pad;
281                 state.accel_plus_g = 15758;
282                 state.accel_minus_g = 16294;
283                 seen = 0;
284                 records = new TreeSet<AltosOrderedRecord>();
285
286                 int index = 0;
287                 int tick = 0;
288
289                 try {
290                         for (;;) {
291                                 String line = AltosRecord.gets(input);
292                                 if (line == null)
293                                         break;
294                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, tick);
295                                 if (record == null)
296                                         break;
297                                 tick = record.tick;
298                                 if (!saw_boost && record.cmd == Altos.AO_LOG_STATE &&
299                                     record.a == Altos.ao_flight_boost)
300                                 {
301                                         saw_boost = true;
302                                         boost_tick = state.tick;
303                                 }
304                                 records.add(record);
305                         }
306                 } catch (IOException io) {
307                 } catch (ParseException pe) {
308                 }
309                 record_iterator = records.iterator();
310                 try {
311                         input.close();
312                 } catch (IOException ie) {
313                 }
314         }
315 }