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