altosdroid: Add map types and map preloading UIs
[fw/altos] / altoslib / AltosState.java
index dba9bff8084d5db712ea733e537cac6d9005bbe8..ad032a1685af8548af8a3b763f6c8d447328b131 100644 (file)
  * Track flight state from telemetry or eeprom data stream
  */
 
-package org.altusmetrum.altoslib_2;
+package org.altusmetrum.altoslib_7;
 
-public class AltosState implements Cloneable {
+import java.io.*;
+
+public class AltosState implements Cloneable, Serializable {
 
        public static final int set_position = 1;
        public static final int set_gps = 2;
@@ -29,8 +31,8 @@ public class AltosState implements Cloneable {
 
        public int set;
 
-       static final double ascent_filter_len = 0.1;
-       static final double descent_filter_len = 2.0;
+       static final double ascent_filter_len = 0.5;
+       static final double descent_filter_len = 0.5;
 
        /* derived data */
 
@@ -43,20 +45,20 @@ 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 Serializable{
+               double  value;
+               double  prev_value;
                private double  max_value;
                private double  set_time;
                private double  prev_set_time;
-               private double  max_rate = 1000.0;
+
+               boolean can_max() { return true; }
 
                void set(double new_value, double time) {
                        if (new_value != AltosLib.MISSING) {
                                value = new_value;
-                               if (max_value == AltosLib.MISSING || value > max_value) {
+                               if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
                                        max_value = value;
-                               }
                                set_time = time;
                        }
                }
@@ -109,7 +111,7 @@ public class AltosState implements Cloneable {
 
                void set_derivative(AltosValue in) {
                        double  n = in.rate();
-                       
+
                        if (n == AltosLib.MISSING)
                                return;
 
@@ -124,13 +126,15 @@ public class AltosState implements Cloneable {
                        /* Clip changes to reduce noise */
                        double  ddt = in.time() - pt;
                        double  ddv = (n - p) / ddt;
-                               
+
+                       final double max = 100000;
+
                        /* 100gs */
-                       if (Math.abs(ddv) > 1000) {
+                       if (Math.abs(ddv) > max) {
                                if (n > p)
-                                       n = p + ddt * 1000;
+                                       n = p + ddt * max;
                                else
-                                       n = p - ddt * 1000;
+                                       n = p - ddt * max;
                        }
 
                        double filter_len;
@@ -177,9 +181,20 @@ public class AltosState implements Cloneable {
                }
        }
 
-       class AltosCValue {
-               AltosValue      measured;
-               AltosValue      computed;
+       class AltosCValue implements Serializable {
+
+               class AltosIValue extends AltosValue implements Serializable {
+                       boolean can_max() {
+                               return c_can_max();
+                       }
+               };
+
+               public AltosIValue      measured;
+               public AltosIValue      computed;
+
+               boolean can_max() { return true; }
+
+               boolean c_can_max() { return can_max(); }
 
                double value() {
                        double v = measured.value();
@@ -245,11 +260,11 @@ public class AltosState implements Cloneable {
                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);
@@ -261,14 +276,16 @@ public class AltosState implements Cloneable {
                }
 
                AltosCValue() {
-                       measured = new AltosValue();
-                       computed = new AltosValue();
+                       measured = new AltosIValue();
+                       computed = new AltosIValue();
                }
        }
 
        public 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 */
@@ -297,10 +314,35 @@ public class AltosState implements Cloneable {
                ground_altitude.set_measured(a, time);
        }
 
-       class AltosGroundPressure extends AltosCValue {
+       class AltosGpsGroundAltitude extends AltosValue implements Serializable {
+               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();
+               }
+       }
+
+       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 implements Serializable {
                void set_filtered(double p, double time) {
                        computed.set_filtered(p, time);
-                       ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
+                       if (!is_measured())
+                               ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
                }
 
                void set_measured(double p, double time) {
@@ -310,7 +352,7 @@ public class AltosState implements Cloneable {
        }
 
        private AltosGroundPressure ground_pressure;
-               
+
        public double ground_pressure() {
                return ground_pressure.value();
        }
@@ -319,7 +361,7 @@ public class AltosState implements Cloneable {
                ground_pressure.set_measured(pressure, time);
        }
 
-       class AltosAltitude extends AltosCValue {
+       class AltosAltitude extends AltosCValue implements Serializable {
 
                private void set_speed(AltosValue v) {
                        if (!acceleration.is_measured() || !ascent)
@@ -341,27 +383,90 @@ public class AltosState implements Cloneable {
 
        private AltosAltitude   altitude;
 
+       class AltosGpsAltitude extends AltosValue implements Serializable {
+
+               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();
+               }
+       }
+
+       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;
-               if (gps != null)
-                       return gps.alt;
-               return AltosLib.MISSING;
+               return gps_altitude.value();
        }
 
        public double max_altitude() {
                double a = altitude.max();
                if (a != AltosLib.MISSING)
                        return a;
-               return AltosLib.MISSING;
+               return gps_altitude.max();
        }
 
        public void set_altitude(double new_altitude) {
                altitude.set_measured(new_altitude, time);
        }
 
-       class AltosPressure extends AltosValue {
+       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 implements Serializable {
                void set(double p, double time) {
                        super.set(p, time);
                        if (state == AltosLib.ao_flight_pad)
@@ -381,11 +486,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)
@@ -393,6 +494,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)
@@ -400,13 +513,35 @@ public class AltosState implements Cloneable {
 
                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 {
-               
+       class AltosSpeed extends AltosCValue implements Serializable {
+
+               boolean can_max() {
+                       return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
+               }
+
                void set_accel() {
                        acceleration.set_derivative(this);
                }
@@ -433,17 +568,34 @@ 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 Serializable {
+
+               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)
@@ -461,6 +613,20 @@ public class AltosState implements Cloneable {
                return acceleration.max();
        }
 
+       public AltosCValue      orient;
+
+       public void set_orient(double new_orient) {
+               orient.set_measured(new_orient, time);
+       }
+
+       public double orient() {
+               return orient.value();
+       }
+
+       public double max_orient() {
+               return orient.max();
+       }
+
        public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
 
        public void set_kalman(double height, double speed, double acceleration) {
@@ -515,11 +681,14 @@ public class AltosState implements Cloneable {
        public double   ground_accel_avg;
 
        public int      log_format;
+       public String   product;
 
        public AltosMs5607      baro;
 
        public AltosCompanion   companion;
 
+       public int      pyro_fired;
+
        public void set_npad(int npad) {
                this.npad = npad;
                gps_waiting = MIN_PAD_SAMPLES - npad;
@@ -557,6 +726,7 @@ public class AltosState implements Cloneable {
                pressure = new AltosPressure();
                speed = new AltosSpeed();
                acceleration = new AltosAccel();
+               orient = new AltosCValue();
 
                temperature = AltosLib.MISSING;
                battery_voltage = AltosLib.MISSING;
@@ -576,7 +746,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;
@@ -590,10 +777,18 @@ public class AltosState implements Cloneable {
                pad_lon = AltosLib.MISSING;
                pad_alt = AltosLib.MISSING;
 
+               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();
+
                speak_tick = AltosLib.MISSING;
                speak_altitude = AltosLib.MISSING;
 
                callsign = null;
+               firmware_version = null;
 
                accel_plus_g = AltosLib.MISSING;
                accel_minus_g = AltosLib.MISSING;
@@ -603,10 +798,15 @@ public class AltosState implements Cloneable {
                ground_accel_avg = AltosLib.MISSING;
 
                log_format = AltosLib.MISSING;
+               product = null;
                serial = AltosLib.MISSING;
+               receiver_serial = AltosLib.MISSING;
+               altitude_32 = AltosLib.MISSING;
 
                baro = null;
                companion = null;
+
+               pyro_fired = 0;
        }
 
        void finish_update() {
@@ -617,6 +817,7 @@ public class AltosState implements Cloneable {
                pressure.finish_update();
                speed.finish_update();
                acceleration.finish_update();
+               orient.finish_update();
 
                kalman_height.finish_update();
                kalman_speed.finish_update();
@@ -632,7 +833,9 @@ public class AltosState implements Cloneable {
 
                received_time = old.received_time;
                time = old.time;
-               time_change = 0;
+               time_change = old.time_change;
+               prev_time = old.time;
+
                tick = old.tick;
                prev_tick = old.tick;
                boost_tick = old.boost_tick;
@@ -650,14 +853,16 @@ public class AltosState implements Cloneable {
                apogee_delay = old.apogee_delay;
                main_deploy = old.main_deploy;
                flight_log_max = old.flight_log_max;
-               
+
                set = 0;
 
+               ground_pressure.copy(old.ground_pressure);
                ground_altitude.copy(old.ground_altitude);
                altitude.copy(old.altitude);
                pressure.copy(old.pressure);
                speed.copy(old.speed);
                acceleration.copy(old.acceleration);
+               orient.copy(old.orient);
 
                battery_voltage = old.battery_voltage;
                pyro_voltage = old.pyro_voltage;
@@ -686,6 +891,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();
@@ -706,6 +932,14 @@ public class AltosState implements Cloneable {
                range = old.range;
 
                gps_height = old.gps_height;
+
+               gps_altitude.copy(old.gps_altitude);
+               gps_ground_altitude.copy(old.gps_ground_altitude);
+               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;
                pad_alt = old.pad_alt;
@@ -714,6 +948,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;
@@ -722,38 +957,52 @@ public class AltosState implements Cloneable {
                ground_accel_avg = old.ground_accel_avg;
 
                log_format = old.log_format;
+               product = old.product;
                serial = old.serial;
+               receiver_serial = old.receiver_serial;
+               altitude_32 = old.altitude_32;
 
                baro = old.baro;
                companion = old.companion;
+
+               pyro_fired = old.pyro_fired;
        }
-       
+
        void update_time() {
        }
 
        void update_gps() {
-               elevation = 0;
-               range = -1;
-               gps_height = 0;
+               elevation = AltosLib.MISSING;
+               range = AltosLib.MISSING;
 
                if (gps == null)
                        return;
 
                if (gps.locked && gps.nsat >= 4) {
                        /* Track consecutive 'good' gps reports, waiting for 10 of them */
-                       if (state == AltosLib.ao_flight_pad) {
+                       if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
                                set_npad(npad+1);
-                               if (pad_lat != AltosLib.MISSING) {
+                               if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
                                        pad_lat = (pad_lat * 31 + gps.lat) / 32;
                                        pad_lon = (pad_lon * 31 + gps.lon) / 32;
-                                       pad_alt = (pad_alt * 31 + gps.alt) / 32;
+                                       gps_ground_altitude.set_filtered(gps.alt, time);
                                }
                        }
                        if (pad_lat == AltosLib.MISSING) {
                                pad_lat = gps.lat;
                                pad_lon = gps.lon;
-                               pad_alt = gps.alt;
+                               gps_ground_altitude.set(gps.alt, time);
                        }
+                       gps_altitude.set(gps.alt, time);
+                       if (gps.climb_rate != AltosLib.MISSING)
+                               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);
                }
                if (gps.lat != 0 && gps.lon != 0 &&
                    pad_lat != AltosLib.MISSING &&
@@ -766,7 +1015,6 @@ public class AltosState implements Cloneable {
                        from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
                        elevation = from_pad.elevation;
                        range = from_pad.range;
-                       gps_height = gps.alt - pad_alt;
                }
        }
 
@@ -779,6 +1027,7 @@ public class AltosState implements Cloneable {
                        }
                        tick = new_tick;
                        time = tick / 100.0;
+                       time_change = time - prev_time;
                }
        }
 
@@ -798,18 +1047,34 @@ public class AltosState implements Cloneable {
                                  state <= AltosLib.ao_flight_coast);
                        boost = (AltosLib.ao_flight_boost == state);
                }
-
        }
 
        public void set_device_type(int device_type) {
                this.device_type = device_type;
+               switch (device_type) {
+               case AltosLib.product_telegps:
+                       this.state = AltosLib.ao_flight_stateless;
+                       break;
+               }
        }
 
-       public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
-               config_major = major;
-               config_minor = minor;
+       public void set_log_format(int log_format) {
+               this.log_format = log_format;
+               switch (log_format) {
+               case AltosLib.AO_LOG_FORMAT_TELEGPS:
+                       this.state = AltosLib.ao_flight_stateless;
+                       break;
+               }
+       }
+
+       public void set_flight_params(int apogee_delay, int main_deploy) {
                this.apogee_delay = apogee_delay;
                this.main_deploy = main_deploy;
+       }
+
+       public void set_config(int major, int minor, int flight_log_max) {
+               config_major = major;
+               config_minor = minor;
                this.flight_log_max = flight_log_max;
        }
 
@@ -821,13 +1086,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) {
-                               init();
+                               re_init();
                        }
                        this.flight = flight;
                }
@@ -838,12 +1117,26 @@ public class AltosState implements Cloneable {
                if (serial != AltosLib.MISSING) {
                        if (this.serial != AltosLib.MISSING &&
                            this.serial != serial) {
-                               init();
+                               re_init();
                        }
                        this.serial = serial;
                }
        }
 
+       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;
@@ -870,16 +1163,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();
@@ -914,14 +1361,8 @@ public class AltosState implements Cloneable {
        }
 
        void update_accel() {
-               double  ground = ground_accel;
-
-               if (ground == AltosLib.MISSING)
-                       ground = ground_accel_avg;
                if (accel == AltosLib.MISSING)
                        return;
-               if (ground == AltosLib.MISSING)
-                       return;
                if (accel_plus_g == AltosLib.MISSING)
                        return;
                if (accel_minus_g == AltosLib.MISSING)
@@ -929,7 +1370,7 @@ public class AltosState implements Cloneable {
 
                double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
                double counts_per_mss = counts_per_g / 9.80665;
-               acceleration.set_measured((ground - accel) / counts_per_mss, time);
+               acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
        }
 
        public void set_accel_g(double accel_plus_g, double accel_minus_g) {
@@ -941,10 +1382,8 @@ public class AltosState implements Cloneable {
        }
 
        public void set_ground_accel(double ground_accel) {
-               if (ground_accel != AltosLib.MISSING) {
+               if (ground_accel != AltosLib.MISSING)
                        this.ground_accel = ground_accel;
-                       update_accel();
-               }
        }
 
        public void set_accel(double accel) {
@@ -999,14 +1438,17 @@ public class AltosState implements Cloneable {
                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 - boost_tick) / 100.0;
-               }
-               return tick / 100.0;
+               if (boost_tick == AltosLib.MISSING)
+                       return tick / 100.0;
+               return (tick - boost_tick) / 100.0;
        }
 
        public boolean valid() {