Bump java lib versions in preparation for 1.9.2
[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_14;
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                         if (product == null)
71                                 set_product(AltosLib.product_name(device_type));
72                 }
73         }
74
75         public int              log_format = AltosLib.MISSING;
76
77         public void set_log_format(int log_format) {
78                 if (log_format != AltosLib.MISSING)
79                         this.log_format = log_format;
80         }
81
82         public int              config_major = AltosLib.MISSING;
83         public int              config_minor = AltosLib.MISSING;
84         public int              flight_log_max = AltosLib.MISSING;
85
86         public void set_config(int major, int minor, int log_max) {
87                 if (major != AltosLib.MISSING)
88                         config_major = major;
89                 if (minor != AltosLib.MISSING)
90                         config_minor = minor;
91                 if (log_max != AltosLib.MISSING)
92                         flight_log_max = log_max;
93         }
94
95         public double           apogee_delay = AltosLib.MISSING;
96         public double           main_deploy = AltosLib.MISSING;
97
98         public void set_flight_params(double apogee_delay, double main_deploy) {
99                 if (apogee_delay != AltosLib.MISSING)
100                         this.apogee_delay = apogee_delay;
101                 if (main_deploy != AltosLib.MISSING)
102                         this.main_deploy = main_deploy;
103         }
104
105         public double           accel_plus_g = AltosLib.MISSING;
106         public double           accel_minus_g = AltosLib.MISSING;
107         public double           ground_accel = AltosLib.MISSING;
108
109         public void set_accel_plus_minus(double plus, double minus) {
110                 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING) {
111                         if (plus == minus)
112                                 return;
113                         accel_plus_g = plus;
114                         accel_minus_g = minus;
115                 }
116         }
117
118         public void set_ground_accel(double ground_accel) {
119                 if (ground_accel != AltosLib.MISSING)
120                         this.ground_accel = ground_accel;
121         }
122
123         /* Raw acceleration value */
124         public double           accel = AltosLib.MISSING;
125
126         public void set_accel(double accel) {
127                 this.accel = accel;
128         }
129
130         public boolean mma655x_inverted = false;
131
132         public void set_mma655x_inverted(boolean inverted) {
133                 mma655x_inverted = inverted;
134         }
135
136         public boolean adxl375_inverted = false;
137
138         public void set_adxl375_inverted(boolean inverted) {
139                 adxl375_inverted = inverted;
140         }
141
142         public int adxl375_axis = AltosLib.MISSING;
143
144         public void set_adxl375_axis(int axis) {
145                 adxl375_axis = axis;
146         }
147
148         public int pad_orientation = AltosLib.MISSING;
149
150         public void set_pad_orientation(int orientation) {
151                 if (orientation != AltosLib.MISSING)
152                         pad_orientation = orientation;
153         }
154
155         /* Compute acceleration */
156         public double acceleration(double sensor) {
157                 double accel;
158                 accel = AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
159                 return accel;
160         }
161
162         public AltosMs5607      ms5607 = null;
163
164         public void set_ms5607(AltosMs5607 ms5607) {
165                 this.ms5607 = ms5607;
166         }
167
168         public double           ground_pressure = AltosLib.MISSING;
169         public double           ground_altitude = AltosLib.MISSING;
170
171         public void set_ground_pressure(double ground_pressure) {
172                 if (ground_pressure != AltosLib.MISSING) {
173                         this.ground_pressure = ground_pressure;
174                         this.ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
175                 }
176         }
177
178         public void set_ground_altitude(double ground_altitude) {
179                 if (ground_altitude != AltosLib.MISSING)
180                         this.ground_altitude = ground_altitude;
181         }
182
183         /* Compute pressure */
184
185         public AltosPresTemp pressure_ms5607(int raw_pres, int raw_temp) {
186                 if (ms5607 == null)
187                         return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
188                 return ms5607.pres_temp(raw_pres, raw_temp);
189         }
190
191         public int              tick = AltosLib.MISSING;
192         private int             first_tick = AltosLib.MISSING;
193         private int             prev_tick = AltosLib.MISSING;
194
195         public void set_tick(int tick) {
196                 if (tick != AltosLib.MISSING) {
197                         if (prev_tick != AltosLib.MISSING) {
198                                 while (tick < prev_tick - 1000) {
199                                         tick += 65536;
200                                 }
201                         }
202                         if (first_tick == AltosLib.MISSING)
203                                 first_tick = tick;
204                         prev_tick = tick;
205                         this.tick = tick;
206                 }
207         }
208
209         /* Reset all values which change during flight
210          */
211         public void reset() {
212                 state = AltosLib.MISSING;
213                 tick = AltosLib.MISSING;
214                 prev_tick = AltosLib.MISSING;
215                 temp_gps = null;
216                 temp_gps_sat_tick = AltosLib.MISSING;
217                 accel = AltosLib.MISSING;
218         }
219
220         public int              boost_tick = AltosLib.MISSING;
221
222         public void set_boost_tick() {
223                 boost_tick = tick;
224         }
225
226         public double           ticks_per_sec = 100.0;
227
228         public void set_ticks_per_sec(double ticks_per_sec) {
229                 this.ticks_per_sec = ticks_per_sec;
230         }
231
232         public double time() {
233                 if (tick == AltosLib.MISSING)
234                         return AltosLib.MISSING;
235                 if (boost_tick != AltosLib.MISSING)
236                         return (tick - boost_tick) / ticks_per_sec;
237                 if (first_tick != AltosLib.MISSING)
238                         return (tick - first_tick) / ticks_per_sec;
239                 return tick / ticks_per_sec;
240         }
241
242         public double boost_time() {
243                 if (boost_tick == AltosLib.MISSING)
244                         return AltosLib.MISSING;
245                 return boost_tick / ticks_per_sec;
246         }
247
248         public int              state = AltosLib.MISSING;
249
250         public String state_name() {
251                 return AltosLib.state_name(state);
252         }
253
254         public void set_state(int state) {
255                 if (state >= AltosLib.ao_flight_boost && boost_tick == AltosLib.MISSING)
256                         set_boost_tick();
257                 this.state = state;
258         }
259
260         public AltosGPS         gps_pad = null;
261
262         public AltosGPS         prev_gps = null;
263
264         public double           gps_pad_altitude = AltosLib.MISSING;
265
266         public void set_cal_gps(AltosGPS gps) {
267                 if (gps.locked && gps.nsat >= 4) {
268                         if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
269                                 gps_pad = gps;
270                         if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
271                                 gps_pad_altitude = gps.alt;
272                 }
273                 temp_gps = null;
274                 prev_gps = gps;
275         }
276
277         /*
278          * While receiving GPS data, we construct a temporary GPS state
279          * object and then deliver the result atomically to the listener
280          */
281         AltosGPS                temp_gps = null;
282         int                     temp_gps_sat_tick = AltosLib.MISSING;
283
284         public AltosGPS temp_cal_gps() {
285                 return temp_gps;
286         }
287
288         public void reset_temp_cal_gps() {
289                 if (temp_gps != null)
290                         set_cal_gps(temp_gps);
291         }
292
293         public boolean cal_gps_pending() {
294                 return temp_gps != null;
295         }
296
297         public AltosGPS make_temp_cal_gps(int tick, boolean sats) {
298                 if (temp_gps == null)
299                         temp_gps = new AltosGPS(prev_gps);
300                 if (sats) {
301                         if (tick != temp_gps_sat_tick)
302                                 temp_gps.cc_gps_sat = null;
303                         temp_gps_sat_tick = tick;
304                 }
305                 return temp_gps;
306         }
307
308         public int      imu_type = AltosLib.MISSING;;
309
310         public void set_imu_type(int imu_type) {
311                 this.imu_type = imu_type;
312         }
313
314         public double   accel_zero_along, accel_zero_across, accel_zero_through;
315
316         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
317                 if (zero_along != AltosLib.MISSING) {
318                         accel_zero_along = zero_along;
319                         accel_zero_across = zero_across;
320                         accel_zero_through = zero_through;
321                 }
322         }
323
324         public double accel_along(double counts) {
325                 return AltosIMU.convert_accel(counts - accel_zero_along, imu_type);
326         }
327
328         public double accel_across(double counts) {
329                 return AltosIMU.convert_accel(counts - accel_zero_across, imu_type);
330         }
331
332         public double accel_through(double counts) {
333                 return AltosIMU.convert_accel(counts - accel_zero_through, imu_type);
334         }
335
336         public double   gyro_zero_roll, gyro_zero_pitch, gyro_zero_yaw;
337
338         public void set_gyro_zero(double roll, double pitch, double yaw) {
339                 if (roll != AltosLib.MISSING) {
340                         gyro_zero_roll = roll;
341                         gyro_zero_pitch = pitch;
342                         gyro_zero_yaw = yaw;
343                         imu_wrap_checked = false;
344                 }
345         }
346
347         public double gyro_roll(double counts) {
348                 if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
349                         return AltosLib.MISSING;
350
351                 return AltosIMU.gyro_degrees_per_second(counts - gyro_zero_roll, imu_type);
352         }
353
354         public double gyro_pitch(double counts) {
355                 if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
356                         return AltosLib.MISSING;
357                 return AltosIMU.gyro_degrees_per_second(counts - gyro_zero_pitch, imu_type);
358         }
359
360         public double gyro_yaw(double counts) {
361                 if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
362                         return AltosLib.MISSING;
363                 return AltosIMU.gyro_degrees_per_second(counts - gyro_zero_yaw, imu_type);
364         }
365
366         private double gyro_zero_overflow(double first) {
367                 double v = first / 128.0;
368                 if (v < 0)
369                         v = Math.ceil(v);
370                 else
371                         v = Math.floor(v);
372 //              if (v != 0)
373 //                      System.out.printf("Adjusting gyro axis by %g steps\n", v);
374                 return v * 128.0;
375         }
376
377         /* Initial TeleMega log format had only 16 bits for gyro cal, so the top 9 bits got lost as the
378          * cal data are scaled by 512. Use the first sample to adjust the cal value, assuming that it is
379          * from a time of fairly low rotation speed. Fixed in later TeleMega firmware by storing 32 bits
380          * of cal values.
381          */
382         private boolean imu_wrap_checked = false;
383
384         public void check_imu_wrap(double roll, double pitch, double yaw) {
385                 if (!imu_wrap_checked) {
386                         gyro_zero_roll += gyro_zero_overflow(roll);
387                         gyro_zero_pitch += gyro_zero_overflow(pitch);
388                         gyro_zero_yaw += gyro_zero_overflow(yaw);
389                         imu_wrap_checked = true;
390                 }
391         }
392
393         public double mag_along(double along) {
394                 if (along == AltosLib.MISSING)
395                         return AltosLib.MISSING;
396                 return AltosIMU.convert_gauss(along, imu_type, AltosIMU.mag_along_axis(imu_type));
397         }
398
399         public double mag_across(double across) {
400                 if (across == AltosLib.MISSING)
401                         return AltosLib.MISSING;
402                 return AltosIMU.convert_gauss(across, imu_type, AltosIMU.mag_across_axis(imu_type));
403         }
404
405         public double mag_through(double through) {
406                 if (through == AltosLib.MISSING)
407                         return AltosLib.MISSING;
408                 return AltosIMU.convert_gauss(through, imu_type, AltosIMU.mag_through_axis(imu_type));
409         }
410
411         public AltosCalData() {
412         }
413
414         public AltosCalData(AltosConfigData config_data) {
415                 set_serial(config_data.serial);
416                 set_ticks_per_sec(100.0);
417                 set_flight(config_data.flight);
418                 set_callsign(config_data.callsign);
419                 set_config(config_data.config_major, config_data.config_minor, config_data.flight_log_max);
420                 set_firmware_version(config_data.version);
421                 set_flight_params(config_data.apogee_delay / ticks_per_sec, config_data.apogee_lockout / ticks_per_sec);
422                 set_pad_orientation(config_data.pad_orientation);
423                 set_product(config_data.product);
424                 set_accel_plus_minus(config_data.accel_cal_plus(config_data.pad_orientation), config_data.accel_cal_minus(config_data.pad_orientation));
425                 set_accel_zero(config_data.accel_zero_along, config_data.accel_zero_across, config_data.accel_zero_through);
426                 set_ms5607(config_data.ms5607);
427                 try {
428                         set_mma655x_inverted(config_data.mma655x_inverted());
429                 } catch (AltosUnknownProduct up) {
430                 }
431                 try {
432                         set_adxl375_inverted(config_data.adxl375_inverted());
433                 } catch (AltosUnknownProduct up) {
434                 }
435                 try {
436                         set_adxl375_axis(config_data.adxl375_axis());
437                 } catch (AltosUnknownProduct up) {
438                 }
439                 set_pad_orientation(config_data.pad_orientation);
440         }
441 }