8b3d745a8341d5e1f15e6eaf57b5a20301f55351
[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 / 100.0;
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 / 100.0;
61                 return MISSING;
62         }
63
64         public double battery_voltage() {
65                 if (v_batt != MISSING)
66                         return 3.3 * adc(v_batt) * 27.0 / (15.0 + 27.0);
67                 return MISSING;
68         }
69
70         static double pyro(int raw) {
71                 if (raw != MISSING)
72                         return 3.3 * adc(raw) * 27.0 / (100.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                 System.out.printf("MM record acceleration %g ground_accel %d accel %d accel_minus_g %d accel_plus_g %d\n",
102                                   acceleration, ground_accel, accel, accel_minus_g, accel_plus_g);
103                 if (acceleration != MISSING)
104                         return acceleration;
105
106                 if (ground_accel == MISSING || accel == MISSING)
107                         return MISSING;
108
109                 if (accel_minus_g == MISSING || accel_plus_g == MISSING)
110                         return MISSING;
111
112                 return (ground_accel - accel) / accel_counts_per_mss();
113         }
114
115         public double accel_speed() {
116                 return speed;
117         }
118
119         public void copy (AltosRecordMM old) {
120                 super.copy(old);
121
122                 accel = old.accel;
123                 pres = old.pres;
124                 temp = old.temp;
125
126                 v_batt = old.v_batt;
127                 v_pyro = old.v_pyro;
128                 sense = new int[num_sense];
129                 
130                 for (int i = 0; i < num_sense; i++)
131                         sense[i] = old.sense[i];
132
133                 ground_accel = old.ground_accel;
134                 ground_pres = old.ground_pres;
135                 accel_plus_g = old.accel_plus_g;
136                 accel_minus_g = old.accel_minus_g;
137                 
138                 flight_accel = old.flight_accel;
139                 flight_vel = old.flight_vel;
140                 flight_pres = old.flight_pres;
141
142                 imu = old.imu;
143                 mag = old.mag;
144         }
145
146         public AltosRecordMM clone() {
147                 AltosRecordMM n = (AltosRecordMM) super.clone();
148                 n.copy(this);
149                 return n;
150         }
151
152         void make_missing() {
153
154                 accel = MISSING;
155                 pres = MISSING;
156                 temp = MISSING;
157
158                 v_batt = MISSING;
159                 v_pyro = MISSING;
160                 sense = new int[num_sense];
161                 for (int i = 0; i < num_sense; i++)
162                         sense[i] = MISSING;
163
164                 ground_accel = MISSING;
165                 ground_pres = MISSING;
166                 accel_plus_g = MISSING;
167                 accel_minus_g = MISSING;
168
169                 flight_accel = 0;
170                 flight_vel = 0;
171                 flight_pres = 0;
172
173                 imu = new AltosIMU();
174                 mag = new AltosMag();
175         }
176
177         public AltosRecordMM(AltosRecord old) {
178                 super.copy(old);
179                 make_missing();
180         }
181
182         public AltosRecordMM() {
183                 super();
184                 make_missing();
185         }
186 }