+ public int set;
+
+ static final double filter_len = 2.0;
+ static final double ascent_filter_len = 0.5;
+ static final double descent_filter_len = 5.0;
+
+ /* derived data */
+
+ public long received_time;
+
+ public double time;
+ public double prev_time;
+ public double time_change;
+ public int tick;
+ private int prev_tick;
+ public int boost_tick;
+
+ class AltosValue implements AltosHashable {
+ double value;
+ double prev_value;
+ private double max_value;
+ private double set_time;
+ private double prev_set_time;
+
+ boolean can_max() { return true; }
+
+ void set(double new_value, double time) {
+ if (new_value != AltosLib.MISSING) {
+ value = new_value;
+ if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
+ max_value = value;
+ set_time = time;
+ }
+ }
+
+ void set_filtered(double new_value, double time) {
+ if (prev_value != AltosLib.MISSING) {
+ double f = 1/Math.exp((time - prev_set_time) / filter_len);
+ new_value = f * new_value + (1-f) * prev_value;
+ }
+ set(new_value, time);
+ }
+
+ double value() {
+ return value;
+ }
+
+ double max() {
+ return max_value;
+ }
+
+ double prev() {
+ return prev_value;
+ }
+
+ double change() {
+ if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
+ return value - prev_value;
+ return AltosLib.MISSING;
+ }
+
+ double rate() {
+ double c = change();
+ double t = set_time - prev_set_time;
+
+ if (c != AltosLib.MISSING && t != 0)
+ return c / t;
+ return AltosLib.MISSING;
+ }
+
+ double integrate() {
+ if (value == AltosLib.MISSING)
+ return AltosLib.MISSING;
+ if (prev_value == AltosLib.MISSING)
+ return AltosLib.MISSING;
+
+ return (value + prev_value) / 2 * (set_time - prev_set_time);
+ }
+
+ double time() {
+ return set_time;
+ }
+
+ void set_derivative(AltosValue in) {
+ double n = in.rate();
+
+ if (n == AltosLib.MISSING)
+ return;
+
+ double p = prev_value;
+ double pt = prev_set_time;
+
+ if (p == AltosLib.MISSING) {
+ p = 0;
+ pt = in.time() - 0.01;
+ }
+
+ /* Clip changes to reduce noise */
+ double ddt = in.time() - pt;
+ double ddv = (n - p) / ddt;
+
+ final double max = 100000;
+
+ /* 100gs */
+ if (Math.abs(ddv) > max) {
+ if (n > p)
+ n = p + ddt * max;
+ else
+ n = p - ddt * max;
+ }
+
+ double filter_len;
+
+ if (ascent)
+ filter_len = ascent_filter_len;
+ else
+ filter_len = descent_filter_len;
+
+ double f = 1/Math.exp(ddt/ filter_len);
+ n = p * f + n * (1-f);
+
+ set(n, in.time());
+ }
+
+ void set_integral(AltosValue in) {
+ double change = in.integrate();
+
+ if (change != AltosLib.MISSING) {
+ double prev = prev_value;
+ if (prev == AltosLib.MISSING)
+ prev = 0;
+ set(prev + change, in.time());
+ }
+ }
+
+ void copy(AltosValue old) {
+ value = old.value;
+ set_time = old.set_time;
+ prev_value = old.value;
+ prev_set_time = old.set_time;
+ max_value = old.max_value;
+ }
+
+ void finish_update() {
+ prev_value = value;
+ prev_set_time = set_time;
+ }
+
+ public AltosHashSet hashSet() {
+ AltosHashSet h = new AltosHashSet();
+
+ h.putDouble("value", value);
+ h.putDouble("prev_value", prev_value);
+ h.putDouble("max_value", max_value);
+ h.putDouble("set_time", set_time);
+ h.putDouble("prev_set_time", prev_set_time);
+ return h;
+ }
+
+ AltosValue(AltosHashSet h) {
+ this();
+ if (h != null) {
+ value = h.getDouble("value", value);
+ prev_value = h.getDouble("prev_value", prev_value);
+ max_value = h.getDouble("max_value", max_value);
+ set_time = h.getDouble("set_time", 0);
+ prev_set_time = h.getDouble("prev_set_time", 0);
+ }
+ }
+
+ AltosValue() {
+ value = AltosLib.MISSING;
+ prev_value = AltosLib.MISSING;
+ max_value = AltosLib.MISSING;
+ }
+
+ }
+
+ AltosValue AltosValue_fromHashSet(AltosHashSet h, AltosValue def) {
+ if (h == null)
+ return def;
+ return new AltosValue(h);
+ }
+
+ class AltosCValue implements AltosHashable {
+
+ class AltosIValue extends AltosValue implements AltosHashable {
+ boolean can_max() {
+ return c_can_max();
+ }
+
+ AltosIValue() {
+ super();
+ }
+
+ AltosIValue(AltosHashSet h) {
+ super(h);
+ }
+ };
+
+ public AltosIValue measured;
+ public AltosIValue computed;
+
+ boolean can_max() { return true; }
+
+ boolean c_can_max() { return can_max(); }
+
+ double value() {
+ double v = measured.value();
+ if (v != AltosLib.MISSING)
+ return v;
+ return computed.value();
+ }
+
+ boolean is_measured() {
+ return measured.value() != AltosLib.MISSING;
+ }
+
+ double max() {
+ double m = measured.max();
+
+ if (m != AltosLib.MISSING)
+ return m;
+ return computed.max();
+ }
+
+ double prev_value() {
+ if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
+ return measured.prev_value;
+ return computed.prev_value;
+ }
+
+ AltosValue altos_value() {
+ if (measured.value() != AltosLib.MISSING)
+ return measured;
+ return computed;
+ }
+
+ double change() {
+ double c = measured.change();
+ if (c == AltosLib.MISSING)
+ c = computed.change();
+ return c;
+ }
+
+ double rate() {
+ double r = measured.rate();
+ if (r == AltosLib.MISSING)
+ r = computed.rate();
+ return r;
+ }
+
+ void set_measured(double new_value, double time) {
+ measured.set(new_value, time);
+ }
+
+ void set_computed(double new_value, double time) {
+ computed.set(new_value, time);
+ }
+
+ void set_derivative(AltosValue in) {
+ computed.set_derivative(in);
+ }
+
+ void set_derivative(AltosCValue in) {
+ set_derivative(in.altos_value());
+ }
+
+ void set_integral(AltosValue in) {
+ computed.set_integral(in);
+ }
+
+ void set_integral(AltosCValue in) {
+ set_integral(in.altos_value());
+ }
+
+ void copy(AltosCValue old) {
+ measured.copy(old.measured);
+ computed.copy(old.computed);
+ }
+
+ void finish_update() {
+ measured.finish_update();
+ computed.finish_update();
+ }
+
+ AltosCValue() {
+ measured = new AltosIValue();
+ computed = new AltosIValue();
+ }
+
+
+ public AltosHashSet hashSet() {
+ AltosHashSet h = new AltosHashSet();
+
+ h.putHashable("measured", measured);
+ h.putHashable("computed", computed);
+ return h;
+ }
+
+ AltosCValue(AltosHashSet h) {
+ measured = new AltosIValue(h.getHash("measured"));
+ computed = new AltosIValue(h.getHash("computed"));
+ }
+ }
+
+ AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) {
+ if (h == null)
+ return def;
+ return new AltosCValue(h);
+ }
+
+ private int state;
+ public int flight;
+ public int serial;
+ public int altitude_32;
+ public int receiver_serial;
+ public boolean landed;
+ public boolean ascent; /* going up? */
+ public boolean boost; /* under power */
+ public int rssi;
+ public int status;
+ public int device_type;
+ public int config_major;
+ public int config_minor;
+ public int apogee_delay;
+ public int main_deploy;
+ public int flight_log_max;
+
+ private double pressure_to_altitude(double p) {
+ if (p == AltosLib.MISSING)
+ return AltosLib.MISSING;
+ return AltosConvert.pressure_to_altitude(p);
+ }
+
+ private AltosCValue ground_altitude;
+
+ public double ground_altitude() {
+ return ground_altitude.value();
+ }
+
+ public void set_ground_altitude(double a) {
+ ground_altitude.set_measured(a, time);
+ }
+
+ class AltosGpsGroundAltitude extends AltosValue {
+ void set(double a, double t) {
+ super.set(a, t);
+ pad_alt = value();
+ gps_altitude.set_gps_height();
+ }
+
+ void set_filtered(double a, double t) {
+ super.set_filtered(a, t);
+ pad_alt = value();
+ gps_altitude.set_gps_height();
+ }
+
+ AltosGpsGroundAltitude() {
+ super();
+ }
+
+ AltosGpsGroundAltitude (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) {
+ if (h == null) return def;
+ return new AltosGpsGroundAltitude(h);
+ }
+
+ private AltosGpsGroundAltitude gps_ground_altitude;
+
+ public double gps_ground_altitude() {
+ return gps_ground_altitude.value();
+ }
+
+ public void set_gps_ground_altitude(double a) {
+ gps_ground_altitude.set(a, time);
+ }
+
+ class AltosGroundPressure extends AltosCValue {
+ void set_filtered(double p, double time) {
+ computed.set_filtered(p, time);
+ if (!is_measured())
+ ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
+ }
+
+ void set_measured(double p, double time) {
+ super.set_measured(p, time);
+ ground_altitude.set_computed(pressure_to_altitude(p), time);
+ }
+
+ AltosGroundPressure () {
+ super();
+ }
+
+ AltosGroundPressure (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) {
+ if (h == null) return def;
+ return new AltosGroundPressure(h);
+ }
+
+ private AltosGroundPressure ground_pressure;
+
+ public double ground_pressure() {
+ return ground_pressure.value();
+ }
+
+ public void set_ground_pressure (double pressure) {
+ ground_pressure.set_measured(pressure, time);
+ }
+
+ class AltosAltitude extends AltosCValue implements AltosHashable {
+
+ private void set_speed(AltosValue v) {
+ if (!acceleration.is_measured() || !ascent)
+ speed.set_derivative(this);
+ }
+
+ void set_computed(double a, double time) {
+ super.set_computed(a,time);
+ set_speed(computed);
+ set |= set_position;
+ }
+
+ void set_measured(double a, double time) {
+ super.set_measured(a,time);
+ set_speed(measured);
+ set |= set_position;
+ }
+
+ AltosAltitude() {
+ super();
+ }
+
+ AltosAltitude (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) {
+ if (h == null) return def;
+ return new AltosAltitude(h);
+ }
+
+ private AltosAltitude altitude;
+
+ class AltosGpsAltitude extends AltosValue implements AltosHashable {
+
+ private void set_gps_height() {
+ double a = value();
+ double g = gps_ground_altitude.value();
+
+ if (a != AltosLib.MISSING && g != AltosLib.MISSING)
+ gps_height = a - g;
+ else
+ gps_height = AltosLib.MISSING;
+ }
+
+ void set(double a, double t) {
+ super.set(a, t);
+ set_gps_height();
+ }
+
+ AltosGpsAltitude() {
+ super();
+ }
+
+ AltosGpsAltitude (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) {
+ if (h == null) return def;
+ return new AltosGpsAltitude(h);
+ }
+
+ private AltosGpsAltitude gps_altitude;
+
+ private AltosValue gps_ground_speed;
+ private AltosValue gps_ascent_rate;
+ private AltosValue gps_course;
+ private AltosValue gps_speed;
+
+ public double altitude() {
+ double a = altitude.value();
+ if (a != AltosLib.MISSING)
+ return a;
+ return gps_altitude.value();
+ }
+
+ public double max_altitude() {
+ double a = altitude.max();
+ if (a != AltosLib.MISSING)
+ return a;
+ return gps_altitude.max();
+ }
+
+ public void set_altitude(double new_altitude) {
+ altitude.set_measured(new_altitude, time);
+ }
+
+ public double gps_altitude() {
+ return gps_altitude.value();
+ }
+
+ public double max_gps_altitude() {
+ return gps_altitude.max();
+ }
+
+ public void set_gps_altitude(double new_gps_altitude) {
+ gps_altitude.set(new_gps_altitude, time);
+ }
+
+ public double gps_ground_speed() {
+ return gps_ground_speed.value();
+ }
+
+ public double max_gps_ground_speed() {
+ return gps_ground_speed.max();
+ }
+
+ public double gps_ascent_rate() {
+ return gps_ascent_rate.value();
+ }
+
+ public double max_gps_ascent_rate() {
+ return gps_ascent_rate.max();
+ }
+
+ public double gps_course() {
+ return gps_course.value();
+ }
+
+ public double gps_speed() {
+ return gps_speed.value();
+ }
+
+ public double max_gps_speed() {
+ return gps_speed.max();
+ }
+
+ class AltosPressure extends AltosValue {
+ void set(double p, double time) {
+ super.set(p, time);
+ if (state == AltosLib.ao_flight_pad)
+ ground_pressure.set_filtered(p, time);
+ double a = pressure_to_altitude(p);
+ altitude.set_computed(a, time);
+ }
+
+ AltosPressure() {
+ super();
+ }
+
+ AltosPressure (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) {
+ if (h == null) return def;
+ return new AltosPressure(h);
+ }
+
+ private AltosPressure pressure;
+
+ public double pressure() {
+ return pressure.value();
+ }
+
+ public void set_pressure(double p) {
+ pressure.set(p, time);
+ }
+
+ public double baro_height() {
+ double a = altitude();
+ double g = ground_altitude();
+ if (a != AltosLib.MISSING && g != AltosLib.MISSING)
+ return a - g;
+ return AltosLib.MISSING;
+ }
+
+ public double height() {
+ double k = kalman_height.value();
+ if (k != AltosLib.MISSING)
+ return k;
+
+ double b = baro_height();
+ if (b != AltosLib.MISSING)
+ return b;
+
+ return gps_height();
+ }
+
+ public double max_height() {
+ double k = kalman_height.max();
+ if (k != AltosLib.MISSING)
+ return k;
+
+ double a = altitude.max();
+ double g = ground_altitude();
+ if (a != AltosLib.MISSING && g != AltosLib.MISSING)
+ return a - g;
+ return max_gps_height();
+ }
+
+ public double gps_height() {
+ double a = gps_altitude();
+ double g = gps_ground_altitude();
+
+ if (a != AltosLib.MISSING && g != AltosLib.MISSING)
+ return a - g;
+ return AltosLib.MISSING;
+ }
+
+ public double max_gps_height() {
+ double a = gps_altitude.max();
+ double g = gps_ground_altitude();
+
+ if (a != AltosLib.MISSING && g != AltosLib.MISSING)
+ return a - g;
+ return AltosLib.MISSING;
+ }
+
+ class AltosSpeed extends AltosCValue implements AltosHashable {
+
+ boolean can_max() {
+ return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
+ }
+
+ void set_accel() {
+ acceleration.set_derivative(this);
+ }
+
+ void set_derivative(AltosCValue in) {
+ super.set_derivative(in);
+ set_accel();
+ }
+
+ void set_computed(double new_value, double time) {
+ super.set_computed(new_value, time);
+ set_accel();
+ }
+
+ void set_measured(double new_value, double time) {
+ super.set_measured(new_value, time);
+ set_accel();
+ }
+
+ AltosSpeed() {
+ super();
+ }
+
+ AltosSpeed (AltosHashSet h) {
+ super(h);
+ }
+ }
+
+ AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) {
+ if (h == null) return def;
+ return new AltosSpeed(h);
+ }
+
+ private AltosSpeed speed;
+
+ public double speed() {
+ double v = kalman_speed.value();
+ if (v != AltosLib.MISSING)
+ return v;
+ v = speed.value();
+ if (v != AltosLib.MISSING)
+ return v;
+ v = gps_speed();
+ if (v != AltosLib.MISSING)
+ return v;
+ return AltosLib.MISSING;
+ }
+
+ public double max_speed() {
+ double v = kalman_speed.max();
+ if (v != AltosLib.MISSING)
+ return v;
+ v = speed.max();
+ if (v != AltosLib.MISSING)
+ return v;
+ v = max_gps_speed();
+ if (v != AltosLib.MISSING)
+ return v;
+ return AltosLib.MISSING;
+ }
+
+ class AltosAccel extends AltosCValue implements AltosHashable {
+
+ boolean can_max() {
+ return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
+ }
+
+ void set_measured(double a, double time) {
+ super.set_measured(a, time);
+ if (ascent)
+ speed.set_integral(this.measured);
+ }
+
+ AltosAccel() {
+ super();
+ }
+
+ AltosAccel (AltosHashSet h) {
+ super(h);
+ }
+ }