altoslib: Store saved state in version-independent format
[fw/altos] / altoslib / AltosState.java
index 2d75f72e676881b45cbc63b9ec9e13dfb90308b4..0970a88e60063d581bae6d474ae95c6dcc8c4ff0 100644 (file)
  * Track flight state from telemetry or eeprom data stream
  */
 
-package org.altusmetrum.altoslib_4;
+package org.altusmetrum.altoslib_11;
 
-public class AltosState implements Cloneable {
+import java.io.*;
+
+public class AltosState implements Cloneable, AltosHashable {
 
        public static final int set_position = 1;
        public static final int set_gps = 2;
@@ -29,8 +31,9 @@ public class AltosState implements Cloneable {
 
        public int set;
 
+       static final double filter_len = 2.0;
        static final double ascent_filter_len = 0.5;
-       static final double descent_filter_len = 0.5;
+       static final double descent_filter_len = 5.0;
 
        /* derived data */
 
@@ -43,9 +46,9 @@ public class AltosState implements Cloneable {
        private int     prev_tick;
        public int      boost_tick;
 
-       class AltosValue {
-               private double  value;
-               private double  prev_value;
+       class AltosValue implements AltosHashable {
+               double  value;
+               double  prev_value;
                private double  max_value;
                private double  set_time;
                private double  prev_set_time;
@@ -62,8 +65,10 @@ public class AltosState implements Cloneable {
                }
 
                void set_filtered(double new_value, double time) {
-                       if (prev_value != AltosLib.MISSING)
-                               new_value = (prev_value * 15.0 + new_value) / 16.0;
+                       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);
                }
 
@@ -172,16 +177,64 @@ public class AltosState implements Cloneable {
                        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 {
-               AltosValue      measured;
-               AltosValue      computed;
+       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();
@@ -263,14 +316,35 @@ public class AltosState implements Cloneable {
                }
 
                AltosCValue() {
-                       measured = new AltosValue();
-                       computed = new AltosValue();
+                       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);
        }
 
-       public int      state;
+       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? */
@@ -312,6 +386,19 @@ public class AltosState implements Cloneable {
                        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;
@@ -335,6 +422,19 @@ public class AltosState implements Cloneable {
                        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;
@@ -347,7 +447,7 @@ public class AltosState implements Cloneable {
                ground_pressure.set_measured(pressure, time);
        }
 
-       class AltosAltitude extends AltosCValue {
+       class AltosAltitude extends AltosCValue implements AltosHashable {
 
                private void set_speed(AltosValue v) {
                        if (!acceleration.is_measured() || !ascent)
@@ -365,11 +465,24 @@ public class AltosState implements Cloneable {
                        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 {
+       class AltosGpsAltitude extends AltosValue implements AltosHashable {
 
                private void set_gps_height() {
                        double  a = value();
@@ -385,6 +498,19 @@ public class AltosState implements Cloneable {
                        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;
@@ -392,6 +518,7 @@ public class AltosState implements Cloneable {
        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();
@@ -427,14 +554,30 @@ public class AltosState implements Cloneable {
                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);
@@ -443,6 +586,19 @@ public class AltosState implements Cloneable {
                        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;
@@ -455,11 +611,7 @@ public class AltosState implements Cloneable {
                pressure.set(p, time);
        }
 
-       public double height() {
-               double k = kalman_height.value();
-               if (k != AltosLib.MISSING)
-                       return k;
-
+       public double baro_height() {
                double a = altitude();
                double g = ground_altitude();
                if (a != AltosLib.MISSING && g != AltosLib.MISSING)
@@ -467,6 +619,18 @@ public class AltosState implements Cloneable {
                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)
@@ -476,7 +640,7 @@ public class AltosState implements Cloneable {
                double g = ground_altitude();
                if (a != AltosLib.MISSING && g != AltosLib.MISSING)
                        return a - g;
-               return AltosLib.MISSING;
+               return max_gps_height();
        }
 
        public double gps_height() {
@@ -497,7 +661,7 @@ public class AltosState implements Cloneable {
                return AltosLib.MISSING;
        }
 
-       class AltosSpeed extends AltosCValue {
+       class AltosSpeed extends AltosCValue implements AltosHashable {
 
                boolean can_max() {
                        return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
@@ -521,6 +685,19 @@ public class AltosState implements Cloneable {
                        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;
@@ -529,17 +706,29 @@ public class AltosState implements Cloneable {
                double v = kalman_speed.value();
                if (v != AltosLib.MISSING)
                        return v;
-               return speed.value();
+               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;
-               return speed.max();
+               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 {
+       class AltosAccel extends AltosCValue implements AltosHashable {
 
                boolean can_max() {
                        return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
@@ -550,6 +739,19 @@ public class AltosState implements Cloneable {
                        if (ascent)
                                speed.set_integral(this.measured);
                }
+
+               AltosAccel() {
+                       super();
+               }
+
+               AltosAccel (AltosHashSet h) {
+                       super(h);
+               }
+       }
+
+       AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) {
+               if (h == null) return def;
+               return new AltosAccel(h);
        }
 
        AltosAccel acceleration;
@@ -562,10 +764,10 @@ public class AltosState implements Cloneable {
                return acceleration.max();
        }
 
-       public AltosValue       orient;
+       public AltosCValue      orient;
 
        public void set_orient(double new_orient) {
-               orient.set(new_orient, time);
+               orient.set_measured(new_orient, time);
        }
 
        public double orient() {
@@ -675,7 +877,7 @@ public class AltosState implements Cloneable {
                pressure = new AltosPressure();
                speed = new AltosSpeed();
                acceleration = new AltosAccel();
-               orient = new AltosValue();
+               orient = new AltosCValue();
 
                temperature = AltosLib.MISSING;
                battery_voltage = AltosLib.MISSING;
@@ -695,7 +897,24 @@ public class AltosState implements Cloneable {
                gps_pending = false;
 
                imu = null;
+               last_imu_time = AltosLib.MISSING;
+               rotation = null;
+               ground_rotation = null;
+
                mag = null;
+               accel_zero_along = AltosLib.MISSING;
+               accel_zero_across = AltosLib.MISSING;
+               accel_zero_through = AltosLib.MISSING;
+
+               accel_ground_along = AltosLib.MISSING;
+               accel_ground_across = AltosLib.MISSING;
+               accel_ground_through = AltosLib.MISSING;
+
+               pad_orientation = AltosLib.MISSING;
+
+               gyro_zero_roll = AltosLib.MISSING;
+               gyro_zero_pitch = AltosLib.MISSING;
+               gyro_zero_yaw = AltosLib.MISSING;
 
                set_npad(0);
                ngps = 0;
@@ -712,6 +931,7 @@ public class AltosState implements Cloneable {
                gps_altitude = new AltosGpsAltitude();
                gps_ground_altitude = new AltosGpsGroundAltitude();
                gps_ground_speed = new AltosValue();
+               gps_speed = new AltosValue();
                gps_ascent_rate = new AltosValue();
                gps_course = new AltosValue();
 
@@ -719,6 +939,7 @@ public class AltosState implements Cloneable {
                speak_altitude = AltosLib.MISSING;
 
                callsign = null;
+               firmware_version = null;
 
                accel_plus_g = AltosLib.MISSING;
                accel_minus_g = AltosLib.MISSING;
@@ -731,6 +952,7 @@ public class AltosState implements Cloneable {
                product = null;
                serial = AltosLib.MISSING;
                receiver_serial = AltosLib.MISSING;
+               altitude_32 = AltosLib.MISSING;
 
                baro = null;
                companion = null;
@@ -820,6 +1042,27 @@ public class AltosState implements Cloneable {
                        imu = old.imu.clone();
                else
                        imu = null;
+               last_imu_time = old.last_imu_time;
+
+               if (old.rotation != null)
+                       rotation = new AltosRotation (old.rotation);
+
+               if (old.ground_rotation != null) {
+                       ground_rotation = new AltosRotation(old.ground_rotation);
+               }
+
+               accel_zero_along = old.accel_zero_along;
+               accel_zero_across = old.accel_zero_across;
+               accel_zero_through = old.accel_zero_through;
+
+               accel_ground_along = old.accel_ground_along;
+               accel_ground_across = old.accel_ground_across;
+               accel_ground_through = old.accel_ground_through;
+               pad_orientation = old.pad_orientation;
+
+               gyro_zero_roll = old.gyro_zero_roll;
+               gyro_zero_pitch = old.gyro_zero_pitch;
+               gyro_zero_yaw = old.gyro_zero_yaw;
 
                if (old.mag != null)
                        mag = old.mag.clone();
@@ -846,6 +1089,7 @@ public class AltosState implements Cloneable {
                gps_ground_speed.copy(old.gps_ground_speed);
                gps_ascent_rate.copy(old.gps_ascent_rate);
                gps_course.copy(old.gps_course);
+               gps_speed.copy(old.gps_speed);
 
                pad_lat = old.pad_lat;
                pad_lon = old.pad_lon;
@@ -855,6 +1099,7 @@ public class AltosState implements Cloneable {
                speak_altitude = old.speak_altitude;
 
                callsign = old.callsign;
+               firmware_version = old.firmware_version;
 
                accel_plus_g = old.accel_plus_g;
                accel_minus_g = old.accel_minus_g;
@@ -866,6 +1111,7 @@ public class AltosState implements Cloneable {
                product = old.product;
                serial = old.serial;
                receiver_serial = old.receiver_serial;
+               altitude_32 = old.altitude_32;
 
                baro = old.baro;
                companion = old.companion;
@@ -877,8 +1123,8 @@ public class AltosState implements Cloneable {
        }
 
        void update_gps() {
-               elevation = 0;
-               range = -1;
+               elevation = AltosLib.MISSING;
+               range = AltosLib.MISSING;
 
                if (gps == null)
                        return;
@@ -903,6 +1149,9 @@ public class AltosState implements Cloneable {
                                gps_ascent_rate.set(gps.climb_rate, time);
                        if (gps.ground_speed != AltosLib.MISSING)
                                gps_ground_speed.set(gps.ground_speed, time);
+                       if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
+                               gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
+                                                       gps.climb_rate * gps.climb_rate), time);
                        if (gps.course != AltosLib.MISSING)
                                gps_course.set(gps.course, time);
                }
@@ -942,6 +1191,10 @@ public class AltosState implements Cloneable {
                return AltosLib.state_name(state);
        }
 
+       public void set_product(String product) {
+               this.product = product;
+       }
+
        public void set_state(int state) {
                if (state != AltosLib.ao_flight_invalid) {
                        this.state = state;
@@ -951,6 +1204,10 @@ public class AltosState implements Cloneable {
                }
        }
 
+       public int state() {
+               return state;
+       }
+
        public void set_device_type(int device_type) {
                this.device_type = device_type;
                switch (device_type) {
@@ -988,15 +1245,27 @@ public class AltosState implements Cloneable {
                firmware_version = version;
        }
 
+       public int compare_version(String other_version) {
+               if (firmware_version == null)
+                       return AltosLib.MISSING;
+               return AltosLib.compare_version(firmware_version, other_version);
+       }
+
+       private void re_init() {
+               int bt = boost_tick;
+               int rs = receiver_serial;
+               init();
+               boost_tick = bt;
+               receiver_serial = rs;
+       }
+
        public void set_flight(int flight) {
 
                /* When the flight changes, reset the state */
-               if (flight != AltosLib.MISSING && flight != 0) {
+               if (flight != AltosLib.MISSING) {
                        if (this.flight != AltosLib.MISSING &&
                            this.flight != flight) {
-                               int bt = boost_tick;
-                               init();
-                               boost_tick = bt;
+                               re_init();
                        }
                        this.flight = flight;
                }
@@ -1007,9 +1276,7 @@ public class AltosState implements Cloneable {
                if (serial != AltosLib.MISSING) {
                        if (this.serial != AltosLib.MISSING &&
                            this.serial != serial) {
-                               int bt = boost_tick;
-                               init();
-                               boost_tick = bt;
+                               re_init();
                        }
                        this.serial = serial;
                }
@@ -1020,6 +1287,15 @@ public class AltosState implements Cloneable {
                        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;
@@ -1046,16 +1322,170 @@ public class AltosState implements Cloneable {
                }
        }
 
+
+       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();
@@ -1080,11 +1510,6 @@ public class AltosState implements Cloneable {
                }
        }
 
-       public void make_companion (int nchannels) {
-               if (companion == null)
-                       companion = new AltosCompanion(nchannels);
-       }
-
        public void set_companion(AltosCompanion companion) {
                this.companion = companion;
        }
@@ -1206,10 +1631,238 @@ public class AltosState implements Cloneable {
        public AltosState clone() {
                AltosState s = new AltosState();
                s.copy(this);
+
+               AltosHashSet    hash = hashSet();
+               String          onetrip = hash.toString();
+               AltosHashSet    back = AltosHashSet.fromString(onetrip);
+               AltosState      tripstate = AltosState.fromHashSet(back);
+               AltosHashSet    triphash = tripstate.hashSet();
+               String          twotrip = triphash.toString();
+
+               if (!onetrip.equals(twotrip)) {
+                       System.out.printf("%s\n%s\n", onetrip, twotrip);
+                       System.exit(1);
+               }
                return s;
        }
 
        public AltosState () {
                init();
        }
+
+       public AltosHashSet hashSet() {
+               AltosHashSet    h = new AltosHashSet();
+
+               h.putBoolean("valid", true);
+               h.putInt("set", set);
+               h.putLong("received_time", received_time);
+               h.putDouble("time", time);
+               h.putDouble("prev_time", prev_time);
+               h.putDouble("time_change", time_change);
+               h.putInt("tick", tick);
+               h.putInt("prev_tick", prev_tick);
+               h.putInt("boost_tick", boost_tick);
+               h.putInt("state", state);
+               h.putInt("flight", flight);
+               h.putInt("serial", serial);
+               h.putInt("altitude_32", altitude_32);
+               h.putInt("receiver_serial", receiver_serial);
+               h.putBoolean("landed", landed);
+               h.putBoolean("ascent", ascent);
+               h.putBoolean("boost", boost);
+               h.putInt("rssi", rssi);
+               h.putInt("status", status);
+               h.putInt("device_type", device_type);
+               h.putInt("config_major", config_major);
+               h.putInt("config_minor", config_minor);
+               h.putInt("apogee_delay", apogee_delay);
+               h.putInt("main_deploy", main_deploy);
+               h.putInt("flight_log_max", flight_log_max);
+               h.putHashable("ground_altitude", ground_altitude);
+               h.putHashable("gps_ground_altitude", gps_ground_altitude);
+               h.putHashable("ground_pressure", ground_pressure);
+               h.putHashable("altitude", altitude);
+               h.putHashable("gps_altitude", gps_altitude);
+               h.putHashable("gps_ground_speed", gps_ground_speed);
+               h.putHashable("gps_ascent_rate", gps_ascent_rate);
+               h.putHashable("gps_course", gps_course);
+               h.putHashable("gps_speed", gps_speed);
+               h.putHashable("pressure", pressure);
+               h.putHashable("speed", speed);
+               h.putHashable("acceleration", acceleration);
+               h.putHashable("orient", orient);
+               h.putHashable("kalman_height", kalman_height);
+               h.putHashable("kalman_speed", kalman_speed);
+               h.putHashable("kalman_acceleration", kalman_acceleration);
+
+               h.putDouble("battery_voltage",battery_voltage);
+               h.putDouble("pyro_voltage",pyro_voltage);
+               h.putDouble("temperature",temperature);
+               h.putDouble("apogee_voltage",apogee_voltage);
+               h.putDouble("main_voltage",main_voltage);
+               h.putDoubleArray("ignitor_voltage",ignitor_voltage);
+               h.putHashable("gps", gps);
+               h.putHashable("temp_gps", temp_gps);
+               h.putInt("temp_gps_sat_tick", temp_gps_sat_tick);
+               h.putBoolean("gps_pending", gps_pending);
+               h.putInt("gps_sequence", gps_sequence);
+               h.putHashable("imu", imu);
+               h.putHashable("mag", mag);
+
+               h.putInt("npad", npad);
+               h.putInt("gps_waiting", gps_waiting);
+               h.putBoolean("gps_ready", gps_ready);
+               h.putInt("ngps", ngps);
+               h.putHashable("from_pad", from_pad);
+               h.putDouble("elevation", elevation);
+               h.putDouble("range", range);
+               h.putDouble("gps_height", gps_height);
+               h.putDouble("pad_lat", pad_lat);
+               h.putDouble("pad_lon", pad_lon);
+               h.putDouble("pad_alt", pad_alt);
+               h.putInt("speak_tick", speak_tick);
+               h.putDouble("speak_altitude", speak_altitude);
+               h.putString("callsign", callsign);
+               h.putString("firmware_version", firmware_version);
+               h.putDouble("accel_plus_g", accel_plus_g);
+               h.putDouble("accel_minus_g", accel_minus_g);
+               h.putDouble("accel", accel);
+               h.putDouble("ground_accel", ground_accel);
+               h.putDouble("ground_accel_avg", ground_accel_avg);
+               h.putInt("log_format", log_format);
+               h.putString("product", product);
+               h.putHashable("baro", baro);
+               h.putHashable("companion", companion);
+               h.putInt("pyro_fired", pyro_fired);
+               h.putDouble("accel_zero_along", accel_zero_along);
+               h.putDouble("accel_zero_across", accel_zero_across);
+               h.putDouble("accel_zero_through", accel_zero_through);
+
+               h.putHashable("rotation", rotation);
+               h.putHashable("ground_rotation", ground_rotation);
+
+               h.putInt("pad_orientation", pad_orientation);
+
+               h.putDouble("accel_ground_along", accel_ground_along);
+               h.putDouble("accel_ground_across", accel_ground_across);
+               h.putDouble("accel_ground_through", accel_ground_through);
+
+               h.putDouble("gyro_zero_roll", gyro_zero_roll);
+               h.putDouble("gyro_zero_pitch", gyro_zero_pitch);
+               h.putDouble("gyro_zero_yaw", gyro_zero_yaw);
+
+               h.putDouble("last_imu_time", last_imu_time);
+               return h;
+       }
+
+       public AltosState(AltosHashSet h) {
+               this();
+
+               set = h.getInt("set", set);
+               received_time = h.getLong("received_time", received_time);
+               time = h.getDouble("time", time);
+               prev_time = h.getDouble("prev_time", prev_time);
+               time_change = h.getDouble("time_change", time_change);
+               tick = h.getInt("tick", tick);
+               prev_tick = h.getInt("prev_tick", prev_tick);
+               boost_tick = h.getInt("boost_tick", boost_tick);
+               state = h.getInt("state", state);
+               flight = h.getInt("flight", flight);
+               serial = h.getInt("serial", serial);
+               altitude_32 = h.getInt("altitude_32", altitude_32);
+               receiver_serial = h.getInt("receiver_serial", receiver_serial);
+               landed = h.getBoolean("landed", landed);
+               ascent = h.getBoolean("ascent", ascent);
+               boost = h.getBoolean("boost", boost);
+               rssi = h.getInt("rssi", rssi);
+               status = h.getInt("status", status);
+               device_type = h.getInt("device_type", device_type);
+               config_major = h.getInt("config_major", config_major);
+               config_minor = h.getInt("config_minor", config_minor);
+               apogee_delay = h.getInt("apogee_delay", apogee_delay);
+               main_deploy = h.getInt("main_deploy", main_deploy);
+               flight_log_max = h.getInt("flight_log_max", flight_log_max);
+               ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude);
+               gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude);
+               ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure);
+               altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude);
+               gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude);
+               gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed);
+               gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate);
+               gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course);
+               gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed);
+               pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure);
+               speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed);
+               acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration);
+               orient = AltosCValue_fromHashSet(h.getHash("orient"), orient);
+               kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height);
+               kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed);
+               kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration);
+
+               battery_voltage = h.getDouble("battery_voltage", battery_voltage);
+               pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage);
+               temperature = h.getDouble("temperature", temperature);
+               apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage);
+               main_voltage=  h.getDouble("main_voltage", main_voltage);
+               ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage);
+               gps = AltosGPS.fromHashSet(h.getHash("gps"), gps);
+               temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps);
+               temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick);
+               gps_pending = h.getBoolean("gps_pending", gps_pending);
+               gps_sequence = h.getInt("gps_sequence", gps_sequence);
+               imu = AltosIMU.fromHashSet(h.getHash("imu"), imu);
+               mag = AltosMag.fromHashSet(h.getHash("mag"), mag);
+
+               npad = h.getInt("npad", npad);
+               gps_waiting = h.getInt("gps_waiting", gps_waiting);
+               gps_ready = h.getBoolean("gps_ready", gps_ready);
+               ngps = h.getInt("ngps", ngps);
+               from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad);
+               elevation = h.getDouble("elevation", elevation);
+               range = h.getDouble("range", range);
+               gps_height = h.getDouble("gps_height", gps_height);
+               pad_lat = h.getDouble("pad_lat", pad_lat);
+               pad_lon = h.getDouble("pad_lon", pad_lon);
+               pad_alt = h.getDouble("pad_alt", pad_alt);
+               speak_tick = h.getInt("speak_tick", speak_tick);
+               speak_altitude = h.getDouble("speak_altitude", speak_altitude);
+               callsign = h.getString("callsign", callsign);
+               firmware_version = h.getString("firmware_version", firmware_version);
+               accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g);
+               accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g);
+               accel = h.getDouble("accel", accel);
+               ground_accel = h.getDouble("ground_accel", ground_accel);
+               ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg);
+               log_format = h.getInt("log_format", log_format);
+               product = h.getString("product", product);
+               baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro);
+               companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion);
+               pyro_fired = h.getInt("pyro_fired", pyro_fired);
+               accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along);
+               accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across);
+               accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through);
+
+               rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation);
+               ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation);
+
+               pad_orientation = h.getInt("pad_orientation", pad_orientation);
+
+               accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along);
+               accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across);
+               accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through);
+
+               gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll);
+               gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch);
+               gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw);
+
+               last_imu_time = h.getDouble("last_imu_time", last_imu_time);
+       }
+
+       public static AltosState fromHashSet(AltosHashSet h) {
+               if (h == null)
+                       return null;
+               if (!h.getBoolean("valid", false))
+                       return null;
+               return new AltosState(h);
+       }
 }