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