Merge branch 'prefs_interface' into altosdroid
[fw/altos] / altoslib / AltosRecordTM.java
1 /*
2  * Copyright © 2012 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 org.altusmetrum.AltosLib;
19
20 public class AltosRecordTM extends AltosRecord {
21         public int      accel;
22         public int      pres;
23         public int      temp;
24         public int      batt;
25         public int      drogue;
26         public int      main;
27
28         public int      ground_accel;
29         public int      ground_pres;
30         public int      accel_plus_g;
31         public int      accel_minus_g;
32
33         public int      flight_accel;
34         public int      flight_vel;
35         public int      flight_pres;
36
37         /*
38          * Values for our MP3H6115A pressure sensor
39          *
40          * From the data sheet:
41          *
42          * Pressure range: 15-115 kPa
43          * Voltage at 115kPa: 2.82
44          * Output scale: 27mV/kPa
45          *
46          *
47          * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
48          * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
49          */
50
51         static final double counts_per_kPa = 27 * 2047 / 3300;
52         static final double counts_at_101_3kPa = 1674.0;
53
54         static double
55         barometer_to_pressure(double count)
56         {
57                 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
58         }
59
60         public double raw_pressure() {
61                 if (pres == MISSING)
62                         return MISSING;
63                 return barometer_to_pressure(pres);
64         }
65
66         public double filtered_pressure() {
67                 if (flight_pres == MISSING)
68                         return MISSING;
69                 return barometer_to_pressure(flight_pres);
70         }
71
72         public double ground_pressure() {
73                 if (ground_pres == MISSING)
74                         return MISSING;
75                 return barometer_to_pressure(ground_pres);
76         }
77
78         public double battery_voltage() {
79                 if (batt == MISSING)
80                         return MISSING;
81                 return AltosConvert.cc_battery_to_voltage(batt);
82         }
83
84         public double main_voltage() {
85                 if (main == MISSING)
86                         return MISSING;
87                 return AltosConvert.cc_ignitor_to_voltage(main);
88         }
89
90         public double drogue_voltage() {
91                 if (drogue == MISSING)
92                         return MISSING;
93                 return AltosConvert.cc_ignitor_to_voltage(drogue);
94         }
95
96         /* Value for the CC1111 built-in temperature sensor
97          * Output voltage at 0°C = 0.755V
98          * Coefficient = 0.00247V/°C
99          * Reference voltage = 1.25V
100          *
101          * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
102          *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
103          */
104
105         static double
106         thermometer_to_temperature(double thermo)
107         {
108                 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
109         }
110
111         public double temperature() {
112                 if (temp == MISSING)
113                         return MISSING;
114                 return thermometer_to_temperature(temp);
115         }
116
117         double accel_counts_per_mss() {
118                 double  counts_per_g = Math.abs(accel_minus_g - accel_plus_g) / 2;
119
120                 return counts_per_g / 9.80665;
121         }
122
123         public double acceleration() {
124                 if (acceleration != MISSING)
125                         return acceleration;
126
127                 if (ground_accel == MISSING || accel == MISSING)
128                         return MISSING;
129                 return (ground_accel - accel) / accel_counts_per_mss();
130         }
131
132         public double accel_speed() {
133                 if (speed != MISSING)
134                         return speed;
135                 if (flight_vel == MISSING)
136                         return MISSING;
137                 return flight_vel / (accel_counts_per_mss() * 100.0);
138         }
139
140         public void copy(AltosRecordTM old) {
141                 super.copy(old);
142
143                 version = old.version;
144                 callsign = old.callsign;
145                 serial = old.serial;
146                 flight = old.flight;
147                 rssi = old.rssi;
148                 status = old.status;
149                 state = old.state;
150                 tick = old.tick;
151                 accel = old.accel;
152                 pres = old.pres;
153                 temp = old.temp;
154                 batt = old.batt;
155                 drogue = old.drogue;
156                 main = old.main;
157                 flight_accel = old.flight_accel;
158                 ground_accel = old.ground_accel;
159                 flight_vel = old.flight_vel;
160                 flight_pres = old.flight_pres;
161                 ground_pres = old.ground_pres;
162                 accel_plus_g = old.accel_plus_g;
163                 accel_minus_g = old.accel_minus_g;
164         }
165
166         public AltosRecordTM clone() {
167                 AltosRecordTM   n = (AltosRecordTM) super.clone();
168                 n.copy(this);
169                 return n;
170         }
171
172         void make_missing() {
173                 accel = MISSING;
174                 pres = MISSING;
175                 temp = MISSING;
176                 batt = MISSING;
177                 drogue = MISSING;
178                 main = MISSING;
179
180                 flight_accel = MISSING;
181                 flight_vel = MISSING;
182                 flight_pres = MISSING;
183
184                 ground_accel = MISSING;
185                 ground_pres = MISSING;
186                 accel_plus_g = MISSING;
187                 accel_minus_g = MISSING;
188         }
189
190         public AltosRecordTM(AltosRecord old) {
191                 super.copy(old);
192                 make_missing();
193         }
194
195         public AltosRecordTM() {
196                 super();
197                 make_missing();
198         }
199 }