altosui: Start adding code to write csv files from eeprom/telem files
[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 pressure() {
77                 return barometer_to_pressure(flight_pres);
78         }
79
80         public double ground_pressure() {
81                 return barometer_to_pressure(ground_pres);
82         }
83
84         public double altitude() {
85                 return AltosConvert.pressure_to_altitude(pressure());
86         }
87
88         public double ground_altitude() {
89                 return AltosConvert.pressure_to_altitude(ground_pressure());
90         }
91
92         public double height() {
93                 return altitude() - ground_altitude();
94         }
95
96         public double battery_voltage() {
97                 return AltosConvert.cc_battery_to_voltage(batt);
98         }
99
100         public double main_voltage() {
101                 return AltosConvert.cc_ignitor_to_voltage(main);
102         }
103
104         public double drogue_voltage() {
105                 return AltosConvert.cc_ignitor_to_voltage(drogue);
106         }
107
108         /* Value for the CC1111 built-in temperature sensor
109          * Output voltage at 0°C = 0.755V
110          * Coefficient = 0.00247V/°C
111          * Reference voltage = 1.25V
112          *
113          * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
114          *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
115          */
116
117         static double
118         thermometer_to_temperature(double thermo)
119         {
120                 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
121         }
122
123         public double temperature() {
124                 return thermometer_to_temperature(temp);
125         }
126
127         double accel_counts_per_mss() {
128                 double  counts_per_g = Math.abs(accel_minus_g - accel_plus_g) / 2;
129
130                 return counts_per_g / 9.80665;
131         }
132         public double acceleration() {
133                 return (accel_plus_g - accel) / accel_counts_per_mss();
134         }
135
136         public double accel_speed() {
137                 double speed = flight_vel / (accel_counts_per_mss() * 100.0);
138                 return speed;
139         }
140
141         public String state() {
142                 return Altos.state_name(state);
143         }
144
145         public static String gets(FileInputStream s) throws IOException {
146                 int c;
147                 String  line = "";
148
149                 while ((c = s.read()) != -1) {
150                         if (c == '\r')
151                                 continue;
152                         if (c == '\n') {
153                                 return line;
154                         }
155                         line = line + (char) c;
156                 }
157                 return null;
158         }
159
160         public AltosRecord(AltosRecord old) {
161                 version = old.version;
162                 callsign = old.callsign;
163                 serial = old.serial;
164                 flight = old.flight;
165                 rssi = old.rssi;
166                 status = old.status;
167                 state = old.state;
168                 tick = old.tick;
169                 accel = old.accel;
170                 pres = old.pres;
171                 temp = old.temp;
172                 batt = old.batt;
173                 drogue = old.drogue;
174                 main = old.main;
175                 flight_accel = old.flight_accel;
176                 ground_accel = old.ground_accel;
177                 flight_vel = old.flight_vel;
178                 flight_pres = old.flight_pres;
179                 ground_pres = old.ground_pres;
180                 accel_plus_g = old.accel_plus_g;
181                 accel_minus_g = old.accel_minus_g;
182                 gps = new AltosGPS(old.gps);
183         }
184
185         public AltosRecord() {
186                 version = 0;
187                 callsign = "N0CALL";
188                 serial = 0;
189                 flight = 0;
190                 rssi = 0;
191                 status = 0;
192                 state = Altos.ao_flight_startup;
193                 tick = 0;
194                 accel = 0;
195                 pres = 0;
196                 temp = 0;
197                 batt = 0;
198                 drogue = 0;
199                 main = 0;
200                 flight_accel = 0;
201                 ground_accel = 0;
202                 flight_vel = 0;
203                 flight_pres = 0;
204                 ground_pres = 0;
205                 accel_plus_g = 0;
206                 accel_minus_g = 0;
207                 gps = new AltosGPS();
208         }
209 }