altosui: Compute flight state from eeprom data
[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 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
40
41         int     index;
42
43         public AltosOrderedRecord(String line, int in_index, int prev_tick)
44                 throws ParseException {
45                 super(line);
46                 int new_tick = tick | (prev_tick & ~0xffff);
47                 if (new_tick < prev_tick) {
48                         if (prev_tick - new_tick > 0x8000)
49                                 new_tick += 0x10000;
50                 }
51                 tick = new_tick;
52                 index = in_index;
53         }
54
55         public int compareTo(AltosOrderedRecord o) {
56                 int     tick_diff = tick - o.tick;
57                 if (tick_diff != 0)
58                         return tick_diff;
59                 return index - o.index;
60         }
61 }
62
63 public class AltosEepromReader {
64
65         static final int        seen_flight = 1;
66         static final int        seen_sensor = 2;
67         static final int        seen_temp_volt = 4;
68         static final int        seen_deploy = 8;
69         static final int        seen_gps_time = 16;
70         static final int        seen_gps_lat = 32;
71         static final int        seen_gps_lon = 64;
72
73         static final int        seen_basic = seen_flight|seen_sensor|seen_temp_volt|seen_deploy;
74
75         AltosRecord             state;
76         AltosOrderedRecord      record;
77
78         TreeSet<AltosOrderedRecord>     records;
79
80         Iterator<AltosOrderedRecord>                    record_iterator;
81
82         int                     seen;
83
84         int                     index;
85
86         boolean                 last_reported;
87
88         double                  ground_pres;
89         double                  ground_accel;
90
91         int                     n_pad_samples;
92
93         int                     gps_tick;
94
95         FileInputStream input;
96
97         public AltosRecord read() throws IOException, ParseException {
98                 for (;;) {
99                         if (record == null) {
100                                 if (!record_iterator.hasNext()) {
101                                         if (last_reported)
102                                                 return null;
103                                         last_reported = true;
104                                         return state;
105                                 }
106                                 record = record_iterator.next();
107
108                                 if ((seen & seen_basic) == seen_basic && record.tick != state.tick)
109                                         return new AltosRecord(state);
110                         }
111
112                         state.tick = record.tick;
113                         switch (record.cmd) {
114                         case Altos.AO_LOG_FLIGHT:
115                                 state.ground_accel = record.a;
116                                 state.flight = record.b;
117                                 seen |= seen_flight;
118                                 break;
119                         case Altos.AO_LOG_SENSOR:
120                                 state.accel = record.a;
121                                 state.pres = record.b;
122                                 if (state.state < Altos.ao_flight_boost) {
123                                         n_pad_samples++;
124                                         ground_pres += state.pres;
125                                         state.ground_pres = (int) (ground_pres / n_pad_samples);
126                                         state.flight_pres = state.ground_pres;
127                                         System.out.printf("ground pressure %d altitude %f\n",
128                                                           record.b, state.altitude());
129                                         ground_accel += state.accel;
130                                         state.ground_accel = (int) (ground_accel / n_pad_samples);
131                                         state.flight_accel = state.ground_accel;
132                                 } else {
133                                         state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
134                                         state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
135                                         state.flight_vel += (state.accel_plus_g - state.accel);
136                                 }
137                                 seen |= seen_sensor;
138                                 break;
139                         case Altos.AO_LOG_TEMP_VOLT:
140                                 state.temp = record.a;
141                                 state.batt = record.b;
142                                 seen |= seen_temp_volt;
143                                 break;
144                         case Altos.AO_LOG_DEPLOY:
145                                 state.drogue = record.a;
146                                 state.main = record.b;
147                                 seen |= seen_deploy;
148                                 break;
149                         case Altos.AO_LOG_STATE:
150                                 System.out.printf("state %d\n", record.a);
151                                 state.state = record.a;
152                                 break;
153                         case Altos.AO_LOG_GPS_TIME:
154                                 gps_tick = state.tick;
155                                 state.gps = new AltosGPS();
156                                 state.gps.hour = (record.a & 0xff);
157                                 state.gps.minute = (record.a >> 8);
158                                 state.gps.second = (record.b & 0xff);
159                                 int flags = (record.b >> 8);
160                                 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
161                                 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
162                                 state.gps.date_valid = (flags & Altos.AO_GPS_DATE_VALID) != 0;
163                                 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
164                                         Altos.AO_GPS_NUM_SAT_SHIFT;
165                                 break;
166                         case Altos.AO_LOG_GPS_LAT:
167                                 int lat32 = record.a | (record.b << 16);
168                                 state.gps.lat = (double) lat32 / 1e7;
169                                 break;
170                         case Altos.AO_LOG_GPS_LON:
171                                 int lon32 = record.a | (record.b << 16);
172                                 state.gps.lon = (double) lon32 / 1e7;
173                                 break;
174                         case Altos.AO_LOG_GPS_ALT:
175                                 state.gps.alt = record.a;
176                                 break;
177                         case Altos.AO_LOG_GPS_SAT:
178                                 if (state.tick == gps_tick) {
179                                         int svid = record.a;
180                                         int c_n0 = record.b >> 8;
181                                         state.gps.add_sat(svid, c_n0);
182                                 }
183                                 break;
184                         case Altos.AO_LOG_GPS_DATE:
185                                 state.gps.year = record.a & 0xff;
186                                 state.gps.month = record.a >> 8;
187                                 state.gps.day = record.b & 0xff;
188                                 break;
189
190                         case Altos.AO_LOG_CONFIG_VERSION:
191                                 break;
192                         case Altos.AO_LOG_MAIN_DEPLOY:
193                                 break;
194                         case Altos.AO_LOG_APOGEE_DELAY:
195                                 break;
196                         case Altos.AO_LOG_RADIO_CHANNEL:
197                                 break;
198                         case Altos.AO_LOG_CALLSIGN:
199                                 state.callsign = record.data;
200                                 break;
201                         case Altos.AO_LOG_ACCEL_CAL:
202                                 state.accel_plus_g = record.a;
203                                 state.accel_minus_g = record.b;
204                                 break;
205                         case Altos.AO_LOG_RADIO_CAL:
206                                 break;
207                         case Altos.AO_LOG_MANUFACTURER:
208                                 break;
209                         case Altos.AO_LOG_PRODUCT:
210                                 break;
211                         case Altos.AO_LOG_SERIAL_NUMBER:
212                                 break;
213                         case Altos.AO_LOG_SOFTWARE_VERSION:
214                                 break;
215                         }
216                         record = null;
217                 }
218         }
219
220         public AltosEepromReader (FileInputStream in_input) {
221                 state = new AltosRecord();
222                 state.state = Altos.ao_flight_pad;
223                 state.accel_plus_g = 15758;
224                 state.accel_minus_g = 16294;
225                 input = in_input;
226                 seen = 0;
227                 records = new TreeSet<AltosOrderedRecord>();
228
229                 int index = 0;
230                 int tick = 0;
231
232                 try {
233                         for (;;) {
234                                 String line = AltosRecord.gets(input);
235                                 if (line == null)
236                                         break;
237                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, tick);
238                                 if (record == null)
239                                         break;
240                                 tick = record.tick;
241                                 records.add(record);
242                         }
243                 } catch (IOException io) {
244                 } catch (ParseException pe) {
245                 }
246                 record_iterator = records.iterator();
247         }
248 }