altoslib: Move computed state from AltosRecord to AltosState
[fw/altos] / altoslib / AltosRecordMM.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 AltosRecordMM extends AltosRecord {
21
22         /* Sensor values */
23         public int      accel;
24         public int      pres;
25         public int      temp;
26         
27         public int      v_batt;
28         public int      v_pyro;
29         public int      sense[];
30
31         public int      ground_accel;
32         public int      ground_pres;
33         public int      accel_plus_g;
34         public int      accel_minus_g;
35
36         public int      flight_accel;
37         public int      flight_vel;
38         public int      flight_pres;
39
40         public final static int num_sense = 6;
41
42         public AltosIMU imu;
43         public AltosMag mag;
44
45         static double adc(int raw) {
46                 return raw / 4095.0;
47         }
48
49         public double pressure() {
50                 if (pres != MISSING)
51                         return pres;
52                 return MISSING;
53         }
54
55         public double ground_pressure() {
56                 if (ground_pres != MISSING)
57                         return ground_pres;
58                 return MISSING;
59         }
60
61         public double battery_voltage() {
62                 if (v_batt != MISSING)
63                         return 3.3 * adc(v_batt) * (15.0 + 27.0) / 27.0;
64                 return MISSING;
65         }
66
67         static double pyro(int raw) {
68                 if (raw != MISSING)
69                         return 3.3 * adc(raw) * (100.0 + 27.0) / 27.0;
70                 return MISSING;
71         }
72
73         public double main_voltage() {
74                 return pyro(sense[1]);
75         }
76
77         public double drogue_voltage() {
78                 return pyro(sense[0]);
79         }
80
81         public double temperature() {
82                 if (temp != MISSING)
83                         return temp / 100.0;
84                 return MISSING;
85         }
86         
87         public AltosIMU imu() { return imu; }
88
89         public AltosMag mag() { return mag; }
90
91         double accel_counts_per_mss() {
92                 double  counts_per_g = Math.abs(accel_minus_g - accel_plus_g) / 2;
93
94                 return counts_per_g / 9.80665;
95         }
96
97         public double acceleration() {
98                 if (ground_accel == MISSING || accel == MISSING)
99                         return MISSING;
100
101                 if (accel_minus_g == MISSING || accel_plus_g == MISSING)
102                         return MISSING;
103
104                 return (ground_accel - accel) / accel_counts_per_mss();
105         }
106
107         public void copy (AltosRecordMM old) {
108                 super.copy(old);
109
110                 accel = old.accel;
111                 pres = old.pres;
112                 temp = old.temp;
113
114                 v_batt = old.v_batt;
115                 v_pyro = old.v_pyro;
116                 sense = new int[num_sense];
117                 
118                 for (int i = 0; i < num_sense; i++)
119                         sense[i] = old.sense[i];
120
121                 ground_accel = old.ground_accel;
122                 ground_pres = old.ground_pres;
123                 accel_plus_g = old.accel_plus_g;
124                 accel_minus_g = old.accel_minus_g;
125                 
126                 flight_accel = old.flight_accel;
127                 flight_vel = old.flight_vel;
128                 flight_pres = old.flight_pres;
129
130                 imu = old.imu;
131                 mag = old.mag;
132         }
133
134         public AltosRecordMM clone() {
135                 AltosRecordMM n = (AltosRecordMM) super.clone();
136                 n.copy(this);
137                 return n;
138         }
139
140         void make_missing() {
141
142                 accel = MISSING;
143                 pres = MISSING;
144                 temp = MISSING;
145
146                 v_batt = MISSING;
147                 v_pyro = MISSING;
148                 sense = new int[num_sense];
149                 for (int i = 0; i < num_sense; i++)
150                         sense[i] = MISSING;
151
152                 ground_accel = MISSING;
153                 ground_pres = MISSING;
154                 accel_plus_g = MISSING;
155                 accel_minus_g = MISSING;
156
157                 flight_accel = 0;
158                 flight_vel = 0;
159                 flight_pres = 0;
160
161                 imu = new AltosIMU();
162                 mag = new AltosMag();
163         }
164
165         public AltosRecordMM(AltosRecord old) {
166                 super.copy(old);
167                 make_missing();
168         }
169
170         public AltosRecordMM() {
171                 super();
172                 make_missing();
173         }
174 }