altoslib: Ignore invalid accel cal values
[fw/altos] / altoslib / AltosCalData.java
1 /*
2  * Copyright © 2017 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, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 package org.altusmetrum.altoslib_11;
16
17 /*
18  * Calibration and other data needed to construct 'real' values from various data
19  * sources.
20  */
21
22 public class AltosCalData {
23         public int              flight = AltosLib.MISSING;
24
25         public void set_flight(int flight) {
26                 if (flight != AltosLib.MISSING)
27                         this.flight = flight;
28         }
29
30         public String           callsign = null;
31
32         public void set_callsign(String callsign) {
33                 if (callsign != null)
34                         this.callsign = callsign;
35         }
36
37         public String           firmware_version = null;
38
39         public void set_firmware_version(String firmware_version) {
40                 if (firmware_version != null)
41                         this.firmware_version = firmware_version;
42         }
43
44         public String           product = null;
45
46         public void set_product(String product) {
47                 if (product != null)
48                         this.product = product;
49         }
50
51         public int              serial = AltosLib.MISSING;
52
53         public void set_serial(int serial) {
54                 if (serial != AltosLib.MISSING)
55                         this.serial = serial;
56         }
57
58         public int              receiver_serial = AltosLib.MISSING;
59
60         public void set_receiver_serial(int receiver_serial) {
61                 if (receiver_serial != AltosLib.MISSING)
62                         this.receiver_serial = receiver_serial;
63         }
64
65         public int              device_type = AltosLib.MISSING;
66
67         public void set_device_type(int device_type) {
68                 if (device_type != AltosLib.MISSING)
69                         this.device_type = device_type;
70         }
71
72         public int              config_major = AltosLib.MISSING;
73         public int              config_minor = AltosLib.MISSING;
74         public int              flight_log_max = AltosLib.MISSING;
75
76         public void set_config(int major, int minor, int log_max) {
77                 if (major != AltosLib.MISSING)
78                         config_major = major;
79                 if (minor != AltosLib.MISSING)
80                         config_minor = minor;
81                 if (log_max != AltosLib.MISSING)
82                         flight_log_max = log_max;
83         }
84
85         public double           apogee_delay = AltosLib.MISSING;
86         public double           main_deploy = AltosLib.MISSING;
87
88         public void set_flight_params(double apogee_delay, double main_deploy) {
89                 if (apogee_delay != AltosLib.MISSING)
90                         this.apogee_delay = apogee_delay;
91                 if (main_deploy != AltosLib.MISSING)
92                         this.main_deploy = main_deploy;
93         }
94
95         public double           accel_plus_g = AltosLib.MISSING;
96         public double           accel_minus_g = AltosLib.MISSING;
97         public double           ground_accel = AltosLib.MISSING;
98
99         public void set_accel_plus_minus(double plus, double minus) {
100                 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING) {
101                         if (plus == minus)
102                                 return;
103                         accel_plus_g = plus;
104                         accel_minus_g = minus;
105                 }
106         }
107
108         public void set_ground_accel(double ground_accel) {
109                 if (ground_accel != AltosLib.MISSING)
110                         this.ground_accel = ground_accel;
111         }
112
113         /* Raw acceleration value */
114         public double           accel = AltosLib.MISSING;
115
116         public void set_accel(double accel) {
117                 this.accel = accel;
118         }
119
120         public boolean mma655x_inverted = false;
121
122         public void set_mma655x_inverted(boolean inverted) {
123                 mma655x_inverted = inverted;
124         }
125
126         public int pad_orientation = AltosLib.MISSING;
127
128         public void set_pad_orientation(int orientation) {
129                 if (orientation != AltosLib.MISSING)
130                         pad_orientation = orientation;
131         }
132
133         /* Compute acceleration */
134         public double acceleration(double sensor) {
135                 return AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
136         }
137
138         public AltosMs5607      ms5607 = null;
139
140         public void set_ms5607(AltosMs5607 ms5607) {
141                 this.ms5607 = ms5607;
142         }
143
144         public double           ground_pressure = AltosLib.MISSING;
145         public double           ground_altitude = AltosLib.MISSING;
146
147         public void set_ground_pressure(double ground_pressure) {
148                 if (ground_pressure != AltosLib.MISSING) {
149                         this.ground_pressure = ground_pressure;
150                         this.ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
151                 }
152         }
153
154         public void set_ground_altitude(double ground_altitude) {
155                 if (ground_altitude != AltosLib.MISSING)
156                         this.ground_altitude = ground_altitude;
157         }
158
159         /* Compute pressure */
160
161         public AltosPresTemp pressure_ms5607(int raw_pres, int raw_temp) {
162                 if (ms5607 == null)
163                         return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
164                 return ms5607.pres_temp(raw_pres, raw_temp);
165         }
166
167         public int              tick = AltosLib.MISSING;
168         private int             prev_tick = AltosLib.MISSING;
169
170         public void set_tick(int tick) {
171                 if (tick != AltosLib.MISSING) {
172                         if (prev_tick != AltosLib.MISSING) {
173                                 while (tick < prev_tick - 1000) {
174                                         tick += 65536;
175                                 }
176                         }
177                         prev_tick = tick;
178                         this.tick = tick;
179                 }
180         }
181
182         public int              boost_tick = AltosLib.MISSING;
183
184         public void set_boost_tick() {
185                 boost_tick = tick;
186         }
187
188         public double           ticks_per_sec = 100.0;
189
190         public void set_ticks_per_sec(double ticks_per_sec) {
191                 this.ticks_per_sec = ticks_per_sec;
192         }
193
194         public double time() {
195                 if (tick == AltosLib.MISSING)
196                         return AltosLib.MISSING;
197                 if (boost_tick == AltosLib.MISSING)
198                         return AltosLib.MISSING;
199                 return (tick - boost_tick) / ticks_per_sec;
200         }
201
202         public double boost_time() {
203                 if (boost_tick == AltosLib.MISSING)
204                         return AltosLib.MISSING;
205                 return boost_tick / ticks_per_sec;
206         }
207
208         public int              state = AltosLib.MISSING;
209
210         public void set_state(int state) {
211                 if (state >= AltosLib.ao_flight_boost && boost_tick == AltosLib.MISSING)
212                         set_boost_tick();
213                 this.state = state;
214         }
215
216         public AltosGPS         gps_pad = null;
217
218         public double           gps_pad_altitude = AltosLib.MISSING;
219
220         public void set_gps(AltosGPS gps) {
221                 if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
222                         gps_pad = gps;
223                 if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
224                         gps_pad_altitude = gps.alt;
225         }
226
227         /*
228          * While receiving GPS data, we construct a temporary GPS state
229          * object and then deliver the result atomically to the listener
230          */
231         AltosGPS                temp_gps = null;
232         AltosGPS                prev_gps = null;
233         int                     temp_gps_sat_tick = AltosLib.MISSING;
234
235         public AltosGPS temp_gps() {
236                 return temp_gps;
237         }
238
239         public void reset_temp_gps() {
240                 if (temp_gps != null) {
241                         if (temp_gps.locked && temp_gps.nsat >= 4)
242                                 set_gps(temp_gps);
243                         prev_gps = temp_gps;
244                         temp_gps = null;
245                 }
246         }
247
248         public boolean gps_pending() {
249                 return temp_gps != null;
250         }
251
252         public AltosGPS make_temp_gps(int tick, boolean sats) {
253                 if (temp_gps == null) {
254                         if (prev_gps != null)
255                                 temp_gps = prev_gps.clone();
256                         else
257                                 temp_gps = new AltosGPS();
258                 }
259                 if (sats) {
260                         if (tick != temp_gps_sat_tick)
261                                 temp_gps.cc_gps_sat = null;
262                         temp_gps_sat_tick = tick;
263                 }
264                 return temp_gps;
265         }
266
267         public double   accel_zero_along, accel_zero_across, accel_zero_through;
268
269         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
270                 if (zero_along != AltosLib.MISSING) {
271                         accel_zero_along = zero_along;
272                         accel_zero_across = zero_across;
273                         accel_zero_through = zero_through;
274                 }
275         }
276
277         public double accel_along(double counts) {
278                 return AltosIMU.convert_accel(counts - accel_zero_along);
279         }
280
281         public double accel_across(double counts) {
282                 return AltosIMU.convert_accel(counts - accel_zero_across);
283         }
284
285         public double accel_through(double counts) {
286                 return AltosIMU.convert_accel(counts - accel_zero_through);
287         }
288
289         public double   gyro_zero_roll, gyro_zero_pitch, gyro_zero_yaw;
290
291         public void set_gyro_zero(double roll, double pitch, double yaw) {
292                 if (roll != AltosLib.MISSING) {
293                         gyro_zero_roll = roll;
294                         gyro_zero_pitch = pitch;
295                         gyro_zero_yaw = yaw;
296                 }
297         }
298
299         public double gyro_roll(double counts) {
300                 if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
301                         return AltosLib.MISSING;
302                 return AltosIMU.convert_gyro(counts - gyro_zero_roll);
303         }
304
305         public double gyro_pitch(double counts) {
306                 if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
307                         return AltosLib.MISSING;
308                 return AltosIMU.convert_gyro(counts - gyro_zero_pitch);
309         }
310
311         public double gyro_yaw(double counts) {
312                 if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
313                         return AltosLib.MISSING;
314                 return AltosIMU.convert_gyro(counts - gyro_zero_yaw);
315         }
316
317         private double gyro_zero_overflow(double first) {
318                 double v = first / 128.0;
319                 if (v < 0)
320                         v = Math.ceil(v);
321                 else
322                         v = Math.floor(v);
323                 return v * 128.0;
324         }
325
326         public void check_imu_wrap(double roll, double pitch, double yaw) {
327                 gyro_zero_roll += gyro_zero_overflow(roll);
328                 gyro_zero_pitch += gyro_zero_overflow(pitch);
329                 gyro_zero_yaw += gyro_zero_overflow(yaw);
330         }
331
332         public double mag_along(double along) {
333                 if (along == AltosLib.MISSING)
334                         return AltosLib.MISSING;
335                 return AltosMag.convert_gauss(along);
336         }
337
338         public double mag_across(double across) {
339                 if (across == AltosLib.MISSING)
340                         return AltosLib.MISSING;
341                 return AltosMag.convert_gauss(across);
342         }
343
344         public double mag_through(double through) {
345                 if (through == AltosLib.MISSING)
346                         return AltosLib.MISSING;
347                 return AltosMag.convert_gauss(through);
348         }
349
350         public AltosCalData() {
351         }
352
353         public AltosCalData(AltosConfigData config_data) {
354                 set_serial(config_data.serial);
355                 set_ticks_per_sec(100.0);
356                 set_flight(config_data.flight);
357                 set_callsign(config_data.callsign);
358                 set_config(config_data.config_major, config_data.config_minor, config_data.flight_log_max);
359                 set_firmware_version(config_data.version);
360                 set_flight_params(config_data.apogee_delay / ticks_per_sec, config_data.apogee_lockout / ticks_per_sec);
361                 set_pad_orientation(config_data.pad_orientation);
362                 set_product(config_data.product);
363                 set_accel_plus_minus(config_data.accel_cal_plus, config_data.accel_cal_minus);
364                 set_accel_zero(config_data.accel_zero_along, config_data.accel_zero_across, config_data.accel_zero_through);
365                 set_ms5607(config_data.ms5607);
366                 try {
367                         set_mma655x_inverted(config_data.mma655x_inverted());
368                 } catch (AltosUnknownProduct up) {
369                 }
370                 set_pad_orientation(config_data.pad_orientation);
371         }
372 }