altosui: Deal with serial port exceptions a bit better
[fw/altos] / altosui / AltosRecord.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.lang.*;
21 import java.text.*;
22 import java.util.HashMap;
23 import java.io.*;
24
25 public class AltosRecord implements Comparable <AltosRecord> {
26         final static int        MISSING = 0x7fffffff;
27
28         static final int        seen_flight = 1;
29         static final int        seen_sensor = 2;
30         static final int        seen_temp_volt = 4;
31         static final int        seen_deploy = 8;
32         static final int        seen_gps_time = 16;
33         static final int        seen_gps_lat = 32;
34         static final int        seen_gps_lon = 64;
35         static final int        seen_companion = 128;
36         int                     seen;
37
38         int     version;
39         String  callsign;
40         int     serial;
41         int     flight;
42         int     rssi;
43         int     status;
44         int     state;
45         int     tick;
46
47         int     accel;
48         int     pres;
49         int     temp;
50         int     batt;
51         int     drogue;
52         int     main;
53
54         int     ground_accel;
55         int     ground_pres;
56         int     accel_plus_g;
57         int     accel_minus_g;
58
59         double  acceleration;
60         double  speed;
61         double  height;
62
63         int     flight_accel;
64         int     flight_vel;
65         int     flight_pres;
66
67         AltosGPS        gps;
68         boolean         new_gps;
69
70         double  time;   /* seconds since boost */
71
72         int     device_type;
73         int     config_major;
74         int     config_minor;
75         int     apogee_delay;
76         int     main_deploy;
77         int     flight_log_max;
78         String  firmware_version;
79
80         AltosRecordCompanion companion;
81         /*
82          * Values for our MP3H6115A pressure sensor
83          *
84          * From the data sheet:
85          *
86          * Pressure range: 15-115 kPa
87          * Voltage at 115kPa: 2.82
88          * Output scale: 27mV/kPa
89          *
90          *
91          * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
92          * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
93          */
94
95         static final double counts_per_kPa = 27 * 2047 / 3300;
96         static final double counts_at_101_3kPa = 1674.0;
97
98         static double
99         barometer_to_pressure(double count)
100         {
101                 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
102         }
103
104         public double raw_pressure() {
105                 if (pres == MISSING)
106                         return MISSING;
107                 return barometer_to_pressure(pres);
108         }
109
110         public double filtered_pressure() {
111                 if (flight_pres == MISSING)
112                         return MISSING;
113                 return barometer_to_pressure(flight_pres);
114         }
115
116         public double ground_pressure() {
117                 if (ground_pres == MISSING)
118                         return MISSING;
119                 return barometer_to_pressure(ground_pres);
120         }
121
122         public double raw_altitude() {
123                 double p = raw_pressure();
124                 if (p == MISSING)
125                         return MISSING;
126                 return AltosConvert.pressure_to_altitude(p);
127         }
128
129         public double ground_altitude() {
130                 double p = ground_pressure();
131                 if (p == MISSING)
132                         return MISSING;
133                 return AltosConvert.pressure_to_altitude(p);
134         }
135
136         public double filtered_altitude() {
137                 if (height != MISSING && ground_pres != MISSING)
138                         return height + ground_altitude();
139
140                 double  p = filtered_pressure();
141                 if (p == MISSING)
142                         return MISSING;
143                 return AltosConvert.pressure_to_altitude(p);
144         }
145
146         public double filtered_height() {
147                 if (height != MISSING)
148                         return height;
149
150                 double f = filtered_altitude();
151                 double g = ground_altitude();
152                 if (f == MISSING || g == MISSING)
153                         return MISSING;
154                 return f - g;
155         }
156
157         public double raw_height() {
158                 double r = raw_altitude();
159                 double g = ground_altitude();
160
161                 if (r == MISSING || g == MISSING)
162                         return height;
163                 return r - g;
164         }
165
166         public double battery_voltage() {
167                 if (batt == MISSING)
168                         return MISSING;
169                 return AltosConvert.cc_battery_to_voltage(batt);
170         }
171
172         public double main_voltage() {
173                 if (main == MISSING)
174                         return MISSING;
175                 return AltosConvert.cc_ignitor_to_voltage(main);
176         }
177
178         public double drogue_voltage() {
179                 if (drogue == MISSING)
180                         return MISSING;
181                 return AltosConvert.cc_ignitor_to_voltage(drogue);
182         }
183
184         /* Value for the CC1111 built-in temperature sensor
185          * Output voltage at 0°C = 0.755V
186          * Coefficient = 0.00247V/°C
187          * Reference voltage = 1.25V
188          *
189          * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
190          *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
191          */
192
193         static double
194         thermometer_to_temperature(double thermo)
195         {
196                 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
197         }
198
199         public double temperature() {
200                 if (temp == MISSING)
201                         return MISSING;
202                 return thermometer_to_temperature(temp);
203         }
204
205         double accel_counts_per_mss() {
206                 double  counts_per_g = Math.abs(accel_minus_g - accel_plus_g) / 2;
207
208                 return counts_per_g / 9.80665;
209         }
210
211         public double acceleration() {
212                 if (acceleration != MISSING)
213                         return acceleration;
214
215                 if (ground_accel == MISSING || accel == MISSING)
216                         return MISSING;
217                 return (ground_accel - accel) / accel_counts_per_mss();
218         }
219
220         public double accel_speed() {
221                 if (speed != MISSING)
222                         return speed;
223                 if (flight_vel == MISSING)
224                         return MISSING;
225                 return flight_vel / (accel_counts_per_mss() * 100.0);
226         }
227
228         public String state() {
229                 return Altos.state_name(state);
230         }
231
232         public static String gets(FileInputStream s) throws IOException {
233                 int c;
234                 String  line = "";
235
236                 while ((c = s.read()) != -1) {
237                         if (c == '\r')
238                                 continue;
239                         if (c == '\n') {
240                                 return line;
241                         }
242                         line = line + (char) c;
243                 }
244                 return null;
245         }
246
247         public int compareTo(AltosRecord o) {
248                 return tick - o.tick;
249         }
250
251         public AltosRecord(AltosRecord old) {
252                 version = old.version;
253                 seen = old.seen;
254                 callsign = old.callsign;
255                 serial = old.serial;
256                 flight = old.flight;
257                 rssi = old.rssi;
258                 status = old.status;
259                 state = old.state;
260                 tick = old.tick;
261                 accel = old.accel;
262                 pres = old.pres;
263                 temp = old.temp;
264                 batt = old.batt;
265                 drogue = old.drogue;
266                 main = old.main;
267                 flight_accel = old.flight_accel;
268                 ground_accel = old.ground_accel;
269                 flight_vel = old.flight_vel;
270                 flight_pres = old.flight_pres;
271                 ground_pres = old.ground_pres;
272                 accel_plus_g = old.accel_plus_g;
273                 accel_minus_g = old.accel_minus_g;
274                 acceleration = old.acceleration;
275                 speed = old.speed;
276                 height = old.height;
277                 gps = new AltosGPS(old.gps);
278                 new_gps = false;
279                 companion = old.companion;
280         }
281
282         public AltosRecord() {
283                 version = 0;
284                 seen = 0;
285                 callsign = "N0CALL";
286                 serial = 0;
287                 flight = 0;
288                 rssi = 0;
289                 status = 0;
290                 state = Altos.ao_flight_startup;
291                 tick = 0;
292                 accel = MISSING;
293                 pres = MISSING;
294                 temp = MISSING;
295                 batt = MISSING;
296                 drogue = MISSING;
297                 main = MISSING;
298                 flight_accel = 0;
299                 ground_accel = 0;
300                 flight_vel = 0;
301                 flight_pres = 0;
302                 ground_pres = 0;
303                 accel_plus_g = 0;
304                 accel_minus_g = 0;
305                 acceleration = MISSING;
306                 speed = MISSING;
307                 height = MISSING;
308                 gps = new AltosGPS();
309                 new_gps = false;
310                 companion = null;
311         }
312 }