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