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_13;
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;
71 set_product(AltosLib.product_name(device_type));
75 public int log_format = AltosLib.MISSING;
77 public void set_log_format(int log_format) {
78 if (log_format != AltosLib.MISSING)
79 this.log_format = log_format;
82 public int config_major = AltosLib.MISSING;
83 public int config_minor = AltosLib.MISSING;
84 public int flight_log_max = AltosLib.MISSING;
86 public void set_config(int major, int minor, int log_max) {
87 if (major != AltosLib.MISSING)
89 if (minor != AltosLib.MISSING)
91 if (log_max != AltosLib.MISSING)
92 flight_log_max = log_max;
95 public double apogee_delay = AltosLib.MISSING;
96 public double main_deploy = AltosLib.MISSING;
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;
105 public double accel_plus_g = AltosLib.MISSING;
106 public double accel_minus_g = AltosLib.MISSING;
107 public double ground_accel = AltosLib.MISSING;
109 public void set_accel_plus_minus(double plus, double minus) {
110 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING) {
114 accel_minus_g = minus;
118 public void set_ground_accel(double ground_accel) {
119 if (ground_accel != AltosLib.MISSING)
120 this.ground_accel = ground_accel;
123 /* Raw acceleration value */
124 public double accel = AltosLib.MISSING;
126 public void set_accel(double accel) {
130 public boolean mma655x_inverted = false;
132 public void set_mma655x_inverted(boolean inverted) {
133 mma655x_inverted = inverted;
136 public boolean adxl375_inverted = false;
138 public void set_adxl375_inverted(boolean inverted) {
139 adxl375_inverted = inverted;
142 public int adxl375_axis = AltosLib.MISSING;
144 public void set_adxl375_axis(int axis) {
148 public int pad_orientation = AltosLib.MISSING;
150 public void set_pad_orientation(int orientation) {
151 if (orientation != AltosLib.MISSING)
152 pad_orientation = orientation;
155 /* Compute acceleration */
156 public double acceleration(double sensor) {
158 accel = AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
159 System.out.printf("acceleration %g (+ %g - %g g %g) -> %g\n",
160 sensor, accel_plus_g, accel_minus_g, ground_accel, accel);
164 public AltosMs5607 ms5607 = null;
166 public void set_ms5607(AltosMs5607 ms5607) {
167 this.ms5607 = ms5607;
170 public double ground_pressure = AltosLib.MISSING;
171 public double ground_altitude = AltosLib.MISSING;
173 public void set_ground_pressure(double ground_pressure) {
174 if (ground_pressure != AltosLib.MISSING) {
175 this.ground_pressure = ground_pressure;
176 this.ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
180 public void set_ground_altitude(double ground_altitude) {
181 if (ground_altitude != AltosLib.MISSING)
182 this.ground_altitude = ground_altitude;
185 /* Compute pressure */
187 public AltosPresTemp pressure_ms5607(int raw_pres, int raw_temp) {
189 return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
190 return ms5607.pres_temp(raw_pres, raw_temp);
193 public int tick = AltosLib.MISSING;
194 private int first_tick = AltosLib.MISSING;
195 private int prev_tick = AltosLib.MISSING;
197 public void set_tick(int tick) {
198 if (tick != AltosLib.MISSING) {
199 if (prev_tick != AltosLib.MISSING) {
200 while (tick < prev_tick - 1000) {
204 if (first_tick == AltosLib.MISSING)
211 /* Reset all values which change during flight
213 public void reset() {
214 state = AltosLib.MISSING;
215 tick = AltosLib.MISSING;
216 prev_tick = AltosLib.MISSING;
218 temp_gps_sat_tick = AltosLib.MISSING;
219 accel = AltosLib.MISSING;
222 public int boost_tick = AltosLib.MISSING;
224 public void set_boost_tick() {
228 public double ticks_per_sec = 100.0;
230 public void set_ticks_per_sec(double ticks_per_sec) {
231 this.ticks_per_sec = ticks_per_sec;
234 public double time() {
235 if (tick == AltosLib.MISSING)
236 return AltosLib.MISSING;
237 if (boost_tick != AltosLib.MISSING)
238 return (tick - boost_tick) / ticks_per_sec;
239 if (first_tick != AltosLib.MISSING)
240 return (tick - first_tick) / ticks_per_sec;
241 return tick / ticks_per_sec;
244 public double boost_time() {
245 if (boost_tick == AltosLib.MISSING)
246 return AltosLib.MISSING;
247 return boost_tick / ticks_per_sec;
250 public int state = AltosLib.MISSING;
252 public void set_state(int state) {
253 if (state >= AltosLib.ao_flight_boost && boost_tick == AltosLib.MISSING)
258 public AltosGPS gps_pad = null;
260 public AltosGPS prev_gps = null;
262 public double gps_pad_altitude = AltosLib.MISSING;
264 public void set_cal_gps(AltosGPS gps) {
265 if (gps.locked && gps.nsat >= 4) {
266 if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
268 if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
269 gps_pad_altitude = gps.alt;
276 * While receiving GPS data, we construct a temporary GPS state
277 * object and then deliver the result atomically to the listener
279 AltosGPS temp_gps = null;
280 int temp_gps_sat_tick = AltosLib.MISSING;
282 public AltosGPS temp_cal_gps() {
286 public void reset_temp_cal_gps() {
287 if (temp_gps != null)
288 set_cal_gps(temp_gps);
291 public boolean cal_gps_pending() {
292 return temp_gps != null;
295 public AltosGPS make_temp_cal_gps(int tick, boolean sats) {
296 if (temp_gps == null)
297 temp_gps = new AltosGPS(prev_gps);
299 if (tick != temp_gps_sat_tick)
300 temp_gps.cc_gps_sat = null;
301 temp_gps_sat_tick = tick;
306 public double accel_zero_along, accel_zero_across, accel_zero_through;
308 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
309 if (zero_along != AltosLib.MISSING) {
310 accel_zero_along = zero_along;
311 accel_zero_across = zero_across;
312 accel_zero_through = zero_through;
316 public double accel_along(double counts) {
317 return AltosIMU.convert_accel(counts - accel_zero_along);
320 public double accel_across(double counts) {
321 return AltosIMU.convert_accel(counts - accel_zero_across);
324 public double accel_through(double counts) {
325 return AltosIMU.convert_accel(counts - accel_zero_through);
328 public double gyro_zero_roll, gyro_zero_pitch, gyro_zero_yaw;
330 public void set_gyro_zero(double roll, double pitch, double yaw) {
331 if (roll != AltosLib.MISSING) {
332 gyro_zero_roll = roll;
333 gyro_zero_pitch = pitch;
335 imu_wrap_checked = false;
339 public double gyro_roll(double counts) {
340 if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
341 return AltosLib.MISSING;
343 return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_roll);
346 public double gyro_pitch(double counts) {
347 if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
348 return AltosLib.MISSING;
349 return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_pitch);
352 public double gyro_yaw(double counts) {
353 if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
354 return AltosLib.MISSING;
355 return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_yaw);
358 private double gyro_zero_overflow(double first) {
359 double v = first / 128.0;
365 // System.out.printf("Adjusting gyro axis by %g steps\n", v);
369 /* Initial TeleMega log format had only 16 bits for gyro cal, so the top 9 bits got lost as the
370 * cal data are scaled by 512. Use the first sample to adjust the cal value, assuming that it is
371 * from a time of fairly low rotation speed. Fixed in later TeleMega firmware by storing 32 bits
374 private boolean imu_wrap_checked = false;
376 public void check_imu_wrap(double roll, double pitch, double yaw) {
377 if (!imu_wrap_checked) {
378 gyro_zero_roll += gyro_zero_overflow(roll);
379 gyro_zero_pitch += gyro_zero_overflow(pitch);
380 gyro_zero_yaw += gyro_zero_overflow(yaw);
381 imu_wrap_checked = true;
385 public double mag_along(double along) {
386 if (along == AltosLib.MISSING)
387 return AltosLib.MISSING;
388 return AltosMag.convert_gauss(along);
391 public double mag_across(double across) {
392 if (across == AltosLib.MISSING)
393 return AltosLib.MISSING;
394 return AltosMag.convert_gauss(across);
397 public double mag_through(double through) {
398 if (through == AltosLib.MISSING)
399 return AltosLib.MISSING;
400 return AltosMag.convert_gauss(through);
403 public AltosCalData() {
406 public AltosCalData(AltosConfigData config_data) {
407 set_serial(config_data.serial);
408 set_ticks_per_sec(100.0);
409 set_flight(config_data.flight);
410 set_callsign(config_data.callsign);
411 set_config(config_data.config_major, config_data.config_minor, config_data.flight_log_max);
412 set_firmware_version(config_data.version);
413 set_flight_params(config_data.apogee_delay / ticks_per_sec, config_data.apogee_lockout / ticks_per_sec);
414 set_pad_orientation(config_data.pad_orientation);
415 set_product(config_data.product);
416 set_accel_plus_minus(config_data.accel_cal_plus(config_data.pad_orientation), config_data.accel_cal_minus(config_data.pad_orientation));
417 set_accel_zero(config_data.accel_zero_along, config_data.accel_zero_across, config_data.accel_zero_through);
418 set_ms5607(config_data.ms5607);
420 set_mma655x_inverted(config_data.mma655x_inverted());
421 } catch (AltosUnknownProduct up) {
424 set_adxl375_inverted(config_data.adxl375_inverted());
425 } catch (AltosUnknownProduct up) {
428 set_adxl375_axis(config_data.adxl375_axis());
429 } catch (AltosUnknownProduct up) {
431 set_pad_orientation(config_data.pad_orientation);