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