altoslib: Extract a few more values from config to cal_data
[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                         accel_plus_g = plus;
102                         accel_minus_g = minus;
103                 }
104         }
105
106         public void set_ground_accel(double ground_accel) {
107                 if (ground_accel != AltosLib.MISSING)
108                         this.ground_accel = ground_accel;
109         }
110
111         /* Raw acceleration value */
112         public double           accel = AltosLib.MISSING;
113
114         public void set_accel(double accel) {
115                 this.accel = accel;
116         }
117
118         public boolean mma655x_inverted = false;
119
120         public void set_mma655x_inverted(boolean inverted) {
121                 mma655x_inverted = inverted;
122         }
123
124         public int pad_orientation = AltosLib.MISSING;
125
126         public void set_pad_orientation(int orientation) {
127                 if (orientation != AltosLib.MISSING)
128                         pad_orientation = orientation;
129         }
130
131         /* Compute acceleration */
132         public double acceleration(double sensor) {
133                 return AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
134         }
135
136         public AltosMs5607      ms5607 = null;
137
138         public void set_ms5607(AltosMs5607 ms5607) {
139                 this.ms5607 = ms5607;
140         }
141
142         public double           ground_pressure = AltosLib.MISSING;
143         public double           ground_altitude = AltosLib.MISSING;
144
145         public void set_ground_pressure(double ground_pressure) {
146                 if (ground_pressure != AltosLib.MISSING) {
147                         this.ground_pressure = ground_pressure;
148                         this.ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
149                 }
150         }
151
152         public void set_ground_altitude(double ground_altitude) {
153                 if (ground_altitude != AltosLib.MISSING)
154                         this.ground_altitude = ground_altitude;
155         }
156
157         /* Compute pressure */
158
159         public AltosPresTemp pressure_ms5607(int raw_pres, int raw_temp) {
160                 if (ms5607 == null)
161                         return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
162                 return ms5607.pres_temp(raw_pres, raw_temp);
163         }
164
165         public int              tick = AltosLib.MISSING;
166         private int             prev_tick = AltosLib.MISSING;
167
168         public void set_tick(int tick) {
169                 if (tick != AltosLib.MISSING) {
170                         if (prev_tick != AltosLib.MISSING) {
171                                 while (tick < prev_tick - 1000) {
172                                         tick += 65536;
173                                 }
174                         }
175                         this.tick = tick;
176                 }
177         }
178
179         public int              boost_tick = AltosLib.MISSING;
180
181         public void set_boost_tick() {
182                 boost_tick = tick;
183         }
184
185         public double           ticks_per_sec = 100.0;
186
187         public void set_ticks_per_sec(double ticks_per_sec) {
188                 this.ticks_per_sec = ticks_per_sec;
189         }
190
191         public double time() {
192                 if (tick == AltosLib.MISSING)
193                         return AltosLib.MISSING;
194                 if (boost_tick == AltosLib.MISSING)
195                         return AltosLib.MISSING;
196                 return (tick - boost_tick) / ticks_per_sec;
197         }
198
199         public double boost_time() {
200                 if (boost_tick == AltosLib.MISSING)
201                         return AltosLib.MISSING;
202                 return boost_tick / ticks_per_sec;
203         }
204
205         public int              state = AltosLib.MISSING;
206
207         public void set_state(int state) {
208                 if (state >= AltosLib.ao_flight_boost && boost_tick == AltosLib.MISSING)
209                         set_boost_tick();
210                 this.state = state;
211         }
212
213         public AltosGPS         gps_pad = null;
214
215         public double           gps_pad_altitude = AltosLib.MISSING;
216
217         public void set_gps(AltosGPS gps) {
218                 if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
219                         gps_pad = gps;
220                 if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
221                         gps_pad_altitude = gps.alt;
222         }
223
224         /*
225          * While receiving GPS data, we construct a temporary GPS state
226          * object and then deliver the result atomically to the listener
227          */
228         AltosGPS                temp_gps = null;
229         AltosGPS                prev_gps = null;
230         int                     temp_gps_sat_tick = AltosLib.MISSING;
231
232         public AltosGPS temp_gps() {
233                 return temp_gps;
234         }
235
236         public void reset_temp_gps() {
237                 if (temp_gps != null) {
238                         if (temp_gps.locked && temp_gps.nsat >= 4)
239                                 set_gps(temp_gps);
240                         prev_gps = temp_gps;
241                         temp_gps = null;
242                 }
243         }
244
245         public boolean gps_pending() {
246                 return temp_gps != null;
247         }
248
249         public AltosGPS make_temp_gps(int tick, boolean sats) {
250                 if (temp_gps == null) {
251                         if (prev_gps != null)
252                                 temp_gps = prev_gps.clone();
253                         else
254                                 temp_gps = new AltosGPS();
255                 }
256                 if (sats) {
257                         if (tick != temp_gps_sat_tick)
258                                 temp_gps.cc_gps_sat = null;
259                         temp_gps_sat_tick = tick;
260                 }
261                 return temp_gps;
262         }
263
264         public double   accel_zero_along, accel_zero_across, accel_zero_through;
265
266         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
267                 if (zero_along != AltosLib.MISSING) {
268                         accel_zero_along = zero_along;
269                         accel_zero_across = zero_across;
270                         accel_zero_through = zero_through;
271                 }
272         }
273
274         public double accel_along(double counts) {
275                 return AltosIMU.convert_accel(counts - accel_zero_along);
276         }
277
278         public double accel_across(double counts) {
279                 return AltosIMU.convert_accel(counts - accel_zero_across);
280         }
281
282         public double accel_through(double counts) {
283                 return AltosIMU.convert_accel(counts - accel_zero_through);
284         }
285
286         public double   gyro_zero_roll, gyro_zero_pitch, gyro_zero_yaw;
287
288         public void set_gyro_zero(double roll, double pitch, double yaw) {
289                 if (roll != AltosLib.MISSING) {
290                         gyro_zero_roll = roll;
291                         gyro_zero_pitch = pitch;
292                         gyro_zero_yaw = yaw;
293                 }
294         }
295
296         public double gyro_roll(double counts) {
297                 if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
298                         return AltosLib.MISSING;
299                 return AltosIMU.convert_gyro(counts - gyro_zero_roll);
300         }
301
302         public double gyro_pitch(double counts) {
303                 if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
304                         return AltosLib.MISSING;
305                 return AltosIMU.convert_gyro(counts - gyro_zero_pitch);
306         }
307
308         public double gyro_yaw(double counts) {
309                 if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
310                         return AltosLib.MISSING;
311                 return AltosIMU.convert_gyro(counts - gyro_zero_yaw);
312         }
313
314         private double gyro_zero_overflow(double first) {
315                 double v = first / 128.0;
316                 if (v < 0)
317                         v = Math.ceil(v);
318                 else
319                         v = Math.floor(v);
320                 return v * 128.0;
321         }
322
323         public void check_imu_wrap(double roll, double pitch, double yaw) {
324                 gyro_zero_roll += gyro_zero_overflow(roll);
325                 gyro_zero_pitch += gyro_zero_overflow(pitch);
326                 gyro_zero_yaw += gyro_zero_overflow(yaw);
327         }
328
329         public double mag_along(double along) {
330                 if (along == AltosLib.MISSING)
331                         return AltosLib.MISSING;
332                 return AltosMag.convert_gauss(along);
333         }
334
335         public double mag_across(double across) {
336                 if (across == AltosLib.MISSING)
337                         return AltosLib.MISSING;
338                 return AltosMag.convert_gauss(across);
339         }
340
341         public double mag_through(double through) {
342                 if (through == AltosLib.MISSING)
343                         return AltosLib.MISSING;
344                 return AltosMag.convert_gauss(through);
345         }
346
347         public AltosCalData() {
348         }
349
350         public AltosCalData(AltosConfigData config_data) {
351                 set_serial(config_data.serial);
352                 set_ticks_per_sec(100.0);
353                 set_flight(config_data.flight);
354                 set_callsign(config_data.callsign);
355                 set_config(config_data.config_major, config_data.config_minor, config_data.flight_log_max);
356                 set_firmware_version(config_data.version);
357                 set_flight_params(config_data.apogee_delay / ticks_per_sec, config_data.apogee_lockout / ticks_per_sec);
358                 set_pad_orientation(config_data.pad_orientation);
359                 set_product(config_data.product);
360                 set_accel_plus_minus(config_data.accel_cal_plus, config_data.accel_cal_minus);
361                 set_accel_zero(config_data.accel_zero_along, config_data.accel_zero_across, config_data.accel_zero_through);
362                 set_ms5607(config_data.ms5607);
363                 try {
364                         set_mma655x_inverted(config_data.mma655x_inverted());
365                 } catch (AltosUnknownProduct up) {
366                 }
367                 set_pad_orientation(config_data.pad_orientation);
368         }
369 }