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