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