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