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