+ public void set_receiver_serial(int serial) {
+ if (serial != AltosLib.MISSING)
+ receiver_serial = serial;
+ }
+
+ public boolean altitude_32() {
+ return altitude_32 == 1;
+ }
+
+ public void set_altitude_32(int altitude_32) {
+ if (altitude_32 != AltosLib.MISSING)
+ this.altitude_32 = altitude_32;
+ }
+
+ public int rssi() {
+ if (rssi == AltosLib.MISSING)
+ return 0;
+ return rssi;
+ }
+
+ public void set_rssi(int rssi, int status) {
+ if (rssi != AltosLib.MISSING) {
+ this.rssi = rssi;
+ this.status = status;
+ }
+ }
+
+ public void set_received_time(long ms) {
+ received_time = ms;
+ }
+
+ public void set_gps(AltosGPS gps, int sequence) {
+ if (gps != null) {
+ this.gps = gps.clone();
+ gps_sequence = sequence;
+ update_gps();
+ set |= set_gps;
+ }
+ }
+
+
+ public double accel_zero_along;
+ public double accel_zero_across;
+ public double accel_zero_through;
+
+ public AltosRotation rotation;
+ public AltosRotation ground_rotation;
+
+ public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
+ if (zero_along != AltosLib.MISSING) {
+ accel_zero_along = zero_along;
+ accel_zero_across = zero_across;
+ accel_zero_through = zero_through;
+ }
+ }
+
+ public int pad_orientation;
+
+ public double accel_ground_along, accel_ground_across, accel_ground_through;
+
+ void update_pad_rotation() {
+ if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
+ rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
+ AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
+ AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
+ pad_orientation);
+ ground_rotation = rotation;
+ orient.set_computed(rotation.tilt(), time);
+ }
+ }
+
+ public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
+ accel_ground_along = ground_along;
+ accel_ground_across = ground_across;
+ accel_ground_through = ground_through;
+ update_pad_rotation();
+ }
+
+ public void set_pad_orientation(int pad_orientation) {
+ this.pad_orientation = pad_orientation;
+ update_pad_rotation();
+ }
+
+ public double gyro_zero_roll;
+ public double gyro_zero_pitch;
+ public double gyro_zero_yaw;
+
+ public void set_gyro_zero(double roll, double pitch, double yaw) {
+ if (roll != AltosLib.MISSING) {
+ gyro_zero_roll = roll;
+ gyro_zero_pitch = pitch;
+ gyro_zero_yaw = yaw;
+ }
+ }
+
+ public double last_imu_time;
+
+ private double radians(double degrees) {
+ if (degrees == AltosLib.MISSING)
+ return AltosLib.MISSING;
+ return degrees * Math.PI / 180.0;
+ }
+
+ private void update_orient() {
+ if (last_imu_time != AltosLib.MISSING) {
+ double t = time - last_imu_time;
+
+ double pitch = radians(gyro_pitch());
+ double yaw = radians(gyro_yaw());
+ double roll = radians(gyro_roll());
+
+ if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
+ rotation.rotate(t, pitch, yaw, roll);
+ orient.set_computed(rotation.tilt(), time);
+ }
+ }
+ last_imu_time = time;
+ }
+
+ public void set_imu(AltosIMU imu) {
+ if (imu != null)
+ imu = imu.clone();
+ this.imu = imu;
+ update_orient();
+ }
+
+ private double gyro_zero_overflow(double first) {
+ double v = first / 128.0;
+ if (v < 0)
+ v = Math.ceil(v);
+ else
+ v = Math.floor(v);
+ return v * 128.0;
+ }
+
+ public void check_imu_wrap(AltosIMU imu) {
+ if (this.imu == null) {
+ gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
+ gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
+ gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
+ }
+ }
+
+ public double accel_along() {
+ if (imu != null && accel_zero_along != AltosLib.MISSING)
+ return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
+ return AltosLib.MISSING;
+ }
+
+ public double accel_across() {
+ if (imu != null && accel_zero_across != AltosLib.MISSING)
+ return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
+ return AltosLib.MISSING;
+ }
+
+ public double accel_through() {
+ if (imu != null && accel_zero_through != AltosLib.MISSING)
+ return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
+ return AltosLib.MISSING;
+ }
+
+ public double gyro_roll() {
+ if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
+ return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
+ }
+ return AltosLib.MISSING;
+ }
+
+ public double gyro_pitch() {
+ if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
+ return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
+ }
+ return AltosLib.MISSING;
+ }
+
+ public double gyro_yaw() {
+ if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
+ return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
+ }
+ return AltosLib.MISSING;
+ }
+
+ public void set_mag(AltosMag mag) {
+ this.mag = mag.clone();
+ }
+
+ public double mag_along() {
+ if (mag != null)
+ return AltosMag.convert_gauss(mag.along);
+ return AltosLib.MISSING;
+ }
+
+ public double mag_across() {
+ if (mag != null)
+ return AltosMag.convert_gauss(mag.across);
+ return AltosLib.MISSING;
+ }
+
+ public double mag_through() {
+ if (mag != null)
+ return AltosMag.convert_gauss(mag.through);
+ return AltosLib.MISSING;
+ }
+
+ public AltosMs5607 make_baro() {
+ if (baro == null)
+ baro = new AltosMs5607();
+ return baro;
+ }
+
+ public void set_ms5607(AltosMs5607 ms5607) {
+ baro = ms5607;
+
+ if (baro != null && baro.pa != AltosLib.MISSING && baro.cc != AltosLib.MISSING) {
+ set_pressure(baro.pa);
+ set_temperature(baro.cc / 100.0);
+ }
+ }
+
+ public void set_ms5607(int pres, int temp) {
+ if (baro != null) {
+ baro.set(pres, temp);
+
+ set_pressure(baro.pa);
+ set_temperature(baro.cc / 100.0);
+ }
+ }
+
+ public void set_companion(AltosCompanion companion) {
+ this.companion = companion;
+ }
+
+ void update_accel() {
+ if (accel == AltosLib.MISSING)
+ return;
+ if (accel_plus_g == AltosLib.MISSING)
+ return;
+ if (accel_minus_g == AltosLib.MISSING)
+ return;
+
+ double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
+ double counts_per_mss = counts_per_g / 9.80665;
+ acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
+ }
+
+ public void set_accel_g(double accel_plus_g, double accel_minus_g) {
+ if (accel_plus_g != AltosLib.MISSING) {
+ this.accel_plus_g = accel_plus_g;
+ this.accel_minus_g = accel_minus_g;
+ update_accel();
+ }
+ }
+
+ public void set_ground_accel(double ground_accel) {
+ if (ground_accel != AltosLib.MISSING)
+ this.ground_accel = ground_accel;
+ }
+
+ public void set_accel(double accel) {
+ if (accel != AltosLib.MISSING) {
+ this.accel = accel;
+ if (state == AltosLib.ao_flight_pad) {
+ if (ground_accel_avg == AltosLib.MISSING)
+ ground_accel_avg = accel;
+ else
+ ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
+ }
+ }
+ update_accel();
+ }
+
+ public void set_temperature(double temperature) {
+ if (temperature != AltosLib.MISSING) {
+ this.temperature = temperature;
+ set |= set_data;
+ }
+ }
+
+ public void set_battery_voltage(double battery_voltage) {
+ if (battery_voltage != AltosLib.MISSING) {
+ this.battery_voltage = battery_voltage;
+ set |= set_data;
+ }
+ }
+
+ public void set_pyro_voltage(double pyro_voltage) {
+ if (pyro_voltage != AltosLib.MISSING) {
+ this.pyro_voltage = pyro_voltage;
+ set |= set_data;
+ }
+ }
+
+ public void set_apogee_voltage(double apogee_voltage) {
+ if (apogee_voltage != AltosLib.MISSING) {
+ this.apogee_voltage = apogee_voltage;
+ set |= set_data;
+ }
+ }
+
+ public void set_main_voltage(double main_voltage) {
+ if (main_voltage != AltosLib.MISSING) {
+ this.main_voltage = main_voltage;
+ set |= set_data;
+ }
+ }
+
+ public void set_ignitor_voltage(double[] voltage) {
+ this.ignitor_voltage = voltage;
+ }
+
+ public void set_pyro_fired(int fired) {
+ this.pyro_fired = fired;
+ }
+
+ public double time_since_boost() {
+ if (tick == AltosLib.MISSING)
+ return 0.0;
+
+ if (boost_tick == AltosLib.MISSING)
+ return tick / 100.0;
+ return (tick - boost_tick) / 100.0;
+ }
+
+ public boolean valid() {
+ return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
+ }
+
+ public void set_temp_gps() {
+ set_gps(temp_gps, gps_sequence + 1);
+ gps_pending = false;
+ super.set_temp_gps();
+ }
+
+ public void set_config_data(AltosConfigData config_data) {
+ if (config_data.callsign != null)
+ set_callsign(config_data.callsign);
+ if (config_data.accel_cal_plus != AltosLib.MISSING &&
+ config_data.accel_cal_minus != AltosLib.MISSING)
+ set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
+ if (config_data.product != null)
+ set_product(config_data.product);
+ if (config_data.log_format != AltosLib.MISSING)
+ set_log_format(config_data.log_format);
+ if (config_data.serial != AltosLib.MISSING)
+ set_serial(config_data.serial);
+ AltosMs5607 ms5607 = new AltosMs5607(config_data);
+ if (ms5607.valid_config())
+ set_ms5607(ms5607);
+ }
+
+ public AltosState clone() {
+ AltosState s = new AltosState();
+ s.copy(this);
+
+ /* Code to test state save/restore. Enable only for that purpose
+ */
+ if (false) {
+ AltosJson json = new AltosJson(this);
+ String onetrip = json.toPrettyString();
+ AltosJson back = AltosJson.fromString(onetrip);
+ AltosState tripstate = (AltosState) back.make(this.getClass());
+ AltosJson tripjson = new AltosJson(tripstate);
+ String twotrip = tripjson.toPrettyString();
+
+ if (!onetrip.equals(twotrip)) {
+ try {
+ FileWriter one_file = new FileWriter("one.json", true);
+ one_file.write(onetrip);
+ one_file.flush();
+ FileWriter two_file = new FileWriter("two.json", true);
+ two_file.write(twotrip);
+ two_file.flush();
+ } catch (Exception e) {
+ }
+ System.out.printf("json error\n");
+ System.exit(1);
+ }
+ }
+ return s;