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