2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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.
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.
15 package org.altusmetrum.altoslib_11;
18 * Calibration and other data needed to construct 'real' values from various data
22 public class AltosCalData {
23 public int flight = AltosLib.MISSING;
25 public void set_flight(int flight) {
26 if (flight != AltosLib.MISSING)
30 public String callsign = null;
32 public void set_callsign(String callsign) {
34 this.callsign = callsign;
37 public String firmware_version = null;
39 public void set_firmware_version(String firmware_version) {
40 if (firmware_version != null)
41 this.firmware_version = firmware_version;
44 public String product = null;
46 public void set_product(String product) {
48 this.product = product;
51 public int serial = AltosLib.MISSING;
53 public void set_serial(int serial) {
54 if (serial != AltosLib.MISSING)
58 public int receiver_serial = AltosLib.MISSING;
60 public void set_receiver_serial(int receiver_serial) {
61 if (receiver_serial != AltosLib.MISSING)
62 this.receiver_serial = receiver_serial;
65 public int device_type = AltosLib.MISSING;
67 public void set_device_type(int device_type) {
68 if (device_type != AltosLib.MISSING)
69 this.device_type = device_type;
72 public int config_major = AltosLib.MISSING;
73 public int config_minor = AltosLib.MISSING;
74 public int flight_log_max = AltosLib.MISSING;
76 public void set_config(int major, int minor, int log_max) {
77 if (major != AltosLib.MISSING)
79 if (minor != AltosLib.MISSING)
81 if (log_max != AltosLib.MISSING)
82 flight_log_max = log_max;
85 public double apogee_delay = AltosLib.MISSING;
86 public double main_deploy = AltosLib.MISSING;
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;
95 public double accel_plus_g = AltosLib.MISSING;
96 public double accel_minus_g = AltosLib.MISSING;
97 public double ground_accel = AltosLib.MISSING;
99 public void set_accel_plus_minus(double plus, double minus) {
100 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING) {
102 accel_minus_g = minus;
106 public void set_ground_accel(double ground_accel) {
107 if (ground_accel != AltosLib.MISSING)
108 this.ground_accel = ground_accel;
111 /* Raw acceleration value */
112 public double accel = AltosLib.MISSING;
114 public void set_accel(double accel) {
118 public boolean mma655x_inverted = false;
120 public void set_mma655x_inverted(boolean inverted) {
121 mma655x_inverted = inverted;
124 public int pad_orientation = AltosLib.MISSING;
126 public void set_pad_orientation(int orientation) {
127 if (orientation != AltosLib.MISSING)
128 pad_orientation = orientation;
131 /* Compute acceleration */
132 public double acceleration(double sensor) {
133 return AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
136 public AltosMs5607 ms5607 = null;
138 public void set_ms5607(AltosMs5607 ms5607) {
139 this.ms5607 = ms5607;
142 public double ground_pressure = AltosLib.MISSING;
143 public double ground_altitude = AltosLib.MISSING;
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);
152 public void set_ground_altitude(double ground_altitude) {
153 if (ground_altitude != AltosLib.MISSING)
154 this.ground_altitude = ground_altitude;
157 /* Compute pressure */
159 public AltosPresTemp pressure_ms5607(int raw_pres, int raw_temp) {
161 return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
162 return ms5607.pres_temp(raw_pres, raw_temp);
165 public int tick = AltosLib.MISSING;
166 private int prev_tick = AltosLib.MISSING;
168 public void set_tick(int tick) {
169 if (tick != AltosLib.MISSING) {
170 if (prev_tick != AltosLib.MISSING) {
171 while (tick < prev_tick - 1000) {
179 public int boost_tick = AltosLib.MISSING;
181 public void set_boost_tick() {
185 public double time() {
186 if (tick == AltosLib.MISSING)
187 return AltosLib.MISSING;
188 if (boost_tick == AltosLib.MISSING)
189 return AltosLib.MISSING;
190 return (tick - boost_tick) / 100.0;
193 public double boost_time() {
194 if (boost_tick == AltosLib.MISSING)
195 return AltosLib.MISSING;
196 return boost_tick / 100.0;
199 public int state = AltosLib.MISSING;
201 public void set_state(int state) {
202 if (state >= AltosLib.ao_flight_boost && boost_tick == AltosLib.MISSING)
207 public AltosGPS gps_pad = null;
209 public double gps_pad_altitude = AltosLib.MISSING;
211 public void set_gps(AltosGPS gps) {
212 if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
214 if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
215 gps_pad_altitude = gps.alt;
219 * While receiving GPS data, we construct a temporary GPS state
220 * object and then deliver the result atomically to the listener
222 AltosGPS temp_gps = null;
223 AltosGPS prev_gps = null;
224 int temp_gps_sat_tick = AltosLib.MISSING;
226 public AltosGPS temp_gps() {
230 public void reset_temp_gps() {
231 if (temp_gps != null) {
232 if (temp_gps.locked && temp_gps.nsat >= 4)
239 public boolean gps_pending() {
240 return temp_gps != null;
243 public AltosGPS make_temp_gps(int tick, boolean sats) {
244 if (temp_gps == null) {
245 if (prev_gps != null)
246 temp_gps = prev_gps.clone();
248 temp_gps = new AltosGPS();
251 if (tick != temp_gps_sat_tick)
252 temp_gps.cc_gps_sat = null;
253 temp_gps_sat_tick = tick;
258 public double accel_zero_along, accel_zero_across, accel_zero_through;
260 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
261 if (zero_along != AltosLib.MISSING) {
262 accel_zero_along = zero_along;
263 accel_zero_across = zero_across;
264 accel_zero_through = zero_through;
268 public double accel_along(double counts) {
269 return AltosIMU.convert_accel(counts - accel_zero_along);
272 public double accel_across(double counts) {
273 return AltosIMU.convert_accel(counts - accel_zero_across);
276 public double accel_through(double counts) {
277 return AltosIMU.convert_accel(counts - accel_zero_through);
280 public double gyro_zero_roll, gyro_zero_pitch, gyro_zero_yaw;
282 public void set_gyro_zero(double roll, double pitch, double yaw) {
283 if (roll != AltosLib.MISSING) {
284 gyro_zero_roll = roll;
285 gyro_zero_pitch = pitch;
290 public double gyro_roll(double counts) {
291 if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
292 return AltosLib.MISSING;
293 return AltosIMU.convert_gyro(counts - gyro_zero_roll);
296 public double gyro_pitch(double counts) {
297 if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
298 return AltosLib.MISSING;
299 return AltosIMU.convert_gyro(counts - gyro_zero_pitch);
302 public double gyro_yaw(double counts) {
303 if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
304 return AltosLib.MISSING;
305 return AltosIMU.convert_gyro(counts - gyro_zero_yaw);
308 private double gyro_zero_overflow(double first) {
309 double v = first / 128.0;
317 public void check_imu_wrap(double roll, double pitch, double yaw) {
318 gyro_zero_roll += gyro_zero_overflow(roll);
319 gyro_zero_pitch += gyro_zero_overflow(pitch);
320 gyro_zero_yaw += gyro_zero_overflow(yaw);
323 public double mag_along(double along) {
324 if (along == AltosLib.MISSING)
325 return AltosLib.MISSING;
326 return AltosMag.convert_gauss(along);
329 public double mag_across(double across) {
330 if (across == AltosLib.MISSING)
331 return AltosLib.MISSING;
332 return AltosMag.convert_gauss(across);
335 public double mag_through(double through) {
336 if (through == AltosLib.MISSING)
337 return AltosLib.MISSING;
338 return AltosMag.convert_gauss(through);
341 public AltosCalData() {
344 public AltosCalData(AltosConfigData config_data) {
345 set_serial(config_data.serial);
346 set_flight(config_data.flight);
347 set_callsign(config_data.callsign);
348 set_accel_plus_minus(config_data.accel_cal_plus, config_data.accel_cal_minus);
349 set_ms5607(config_data.ms5607);
351 set_mma655x_inverted(config_data.mma655x_inverted());
352 } catch (AltosUnknownProduct up) {
354 set_pad_orientation(config_data.pad_orientation);