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