altosui: Make sure packet mode is turned off when the connection fails
[fw/altos] / ao-tools / 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         int     version;
27         String  callsign;
28         int     serial;
29         int     flight;
30         int     rssi;
31         int     status;
32         int     state;
33         int     tick;
34         int     accel;
35         int     pres;
36         int     temp;
37         int     batt;
38         int     drogue;
39         int     main;
40         int     flight_accel;
41         int     ground_accel;
42         int     flight_vel;
43         int     flight_pres;
44         int     ground_pres;
45         int     accel_plus_g;
46         int     accel_minus_g;
47         AltosGPS        gps;
48
49         double  time;   /* seconds since boost */
50
51         /*
52          * Values for our MP3H6115A pressure sensor
53          *
54          * From the data sheet:
55          *
56          * Pressure range: 15-115 kPa
57          * Voltage at 115kPa: 2.82
58          * Output scale: 27mV/kPa
59          *
60          *
61          * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
62          * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
63          */
64
65         static final double counts_per_kPa = 27 * 2047 / 3300;
66         static final double counts_at_101_3kPa = 1674.0;
67
68         static double
69         barometer_to_pressure(double count)
70         {
71                 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
72         }
73
74         public double raw_pressure() {
75                 return barometer_to_pressure(pres);
76         }
77
78         public double filtered_pressure() {
79                 return barometer_to_pressure(flight_pres);
80         }
81
82         public double ground_pressure() {
83                 return barometer_to_pressure(ground_pres);
84         }
85
86         public double filtered_altitude() {
87                 return AltosConvert.pressure_to_altitude(filtered_pressure());
88         }
89
90         public double raw_altitude() {
91                 return AltosConvert.pressure_to_altitude(raw_pressure());
92         }
93
94         public double ground_altitude() {
95                 return AltosConvert.pressure_to_altitude(ground_pressure());
96         }
97
98         public double filtered_height() {
99                 return filtered_altitude() - ground_altitude();
100         }
101
102         public double raw_height() {
103                 return raw_altitude() - ground_altitude();
104         }
105
106         public double battery_voltage() {
107                 return AltosConvert.cc_battery_to_voltage(batt);
108         }
109
110         public double main_voltage() {
111                 return AltosConvert.cc_ignitor_to_voltage(main);
112         }
113
114         public double drogue_voltage() {
115                 return AltosConvert.cc_ignitor_to_voltage(drogue);
116         }
117
118         /* Value for the CC1111 built-in temperature sensor
119          * Output voltage at 0°C = 0.755V
120          * Coefficient = 0.00247V/°C
121          * Reference voltage = 1.25V
122          *
123          * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
124          *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
125          */
126
127         static double
128         thermometer_to_temperature(double thermo)
129         {
130                 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
131         }
132
133         public double temperature() {
134                 return thermometer_to_temperature(temp);
135         }
136
137         double accel_counts_per_mss() {
138                 double  counts_per_g = Math.abs(accel_minus_g - accel_plus_g) / 2;
139
140                 return counts_per_g / 9.80665;
141         }
142         public double acceleration() {
143                 return (ground_accel - accel) / accel_counts_per_mss();
144         }
145
146         public double accel_speed() {
147                 double speed = flight_vel / (accel_counts_per_mss() * 100.0);
148                 return speed;
149         }
150
151         public String state() {
152                 return Altos.state_name(state);
153         }
154
155         public static String gets(FileInputStream s) throws IOException {
156                 int c;
157                 String  line = "";
158
159                 while ((c = s.read()) != -1) {
160                         if (c == '\r')
161                                 continue;
162                         if (c == '\n') {
163                                 return line;
164                         }
165                         line = line + (char) c;
166                 }
167                 return null;
168         }
169
170         public AltosRecord(AltosRecord old) {
171                 version = old.version;
172                 callsign = old.callsign;
173                 serial = old.serial;
174                 flight = old.flight;
175                 rssi = old.rssi;
176                 status = old.status;
177                 state = old.state;
178                 tick = old.tick;
179                 accel = old.accel;
180                 pres = old.pres;
181                 temp = old.temp;
182                 batt = old.batt;
183                 drogue = old.drogue;
184                 main = old.main;
185                 flight_accel = old.flight_accel;
186                 ground_accel = old.ground_accel;
187                 flight_vel = old.flight_vel;
188                 flight_pres = old.flight_pres;
189                 ground_pres = old.ground_pres;
190                 accel_plus_g = old.accel_plus_g;
191                 accel_minus_g = old.accel_minus_g;
192                 gps = new AltosGPS(old.gps);
193         }
194
195         public AltosRecord() {
196                 version = 0;
197                 callsign = "N0CALL";
198                 serial = 0;
199                 flight = 0;
200                 rssi = 0;
201                 status = 0;
202                 state = Altos.ao_flight_startup;
203                 tick = 0;
204                 accel = 0;
205                 pres = 0;
206                 temp = 0;
207                 batt = 0;
208                 drogue = 0;
209                 main = 0;
210                 flight_accel = 0;
211                 ground_accel = 0;
212                 flight_vel = 0;
213                 flight_pres = 0;
214                 ground_pres = 0;
215                 accel_plus_g = 0;
216                 accel_minus_g = 0;
217                 gps = new AltosGPS();
218         }
219 }