altoslib: Trim stale bits of AltosState
[fw/altos] / altoslib / AltosState.java
index d65e3bd88fb2bc466ce97457d3d8c478ebefdfb7..d3929b8f7e047480ddcd02cef4eed0953863f26c 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * Track flight state from telemetry or eeprom data stream
  */
 
-package org.altusmetrum.altoslib_3;
+package org.altusmetrum.altoslib_11;
 
-public class AltosState implements Cloneable {
+import java.io.*;
+
+public class AltosState extends AltosDataListener {
 
        public static final int set_position = 1;
        public static final int set_gps = 2;
@@ -29,23 +32,22 @@ 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 */
 
        public long     received_time;
 
+       public int      rssi;
+       public int      status;
+
        public double   time;
-       public double   prev_time;
-       public double   time_change;
-       public int      tick;
-       private int     prev_tick;
-       public int      boost_tick;
 
        class AltosValue {
-               private double  value;
-               private double  prev_value;
+               double  value;
+               double  prev_value;
                private double  max_value;
                private double  set_time;
                private double  prev_set_time;
@@ -62,8 +64,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);
                }
 
@@ -177,11 +181,27 @@ public class AltosState implements Cloneable {
                        prev_value = AltosLib.MISSING;
                        max_value = AltosLib.MISSING;
                }
+
        }
 
        class AltosCValue {
-               AltosValue      measured;
-               AltosValue      computed;
+
+               class AltosIValue extends AltosValue {
+                       boolean can_max() {
+                               return c_can_max();
+                       }
+
+                       AltosIValue() {
+                               super();
+                       }
+               };
+
+               public AltosIValue      measured;
+               public AltosIValue      computed;
+
+               boolean can_max() { return true; }
+
+               boolean c_can_max() { return can_max(); }
 
                double value() {
                        double v = measured.value();
@@ -262,27 +282,16 @@ public class AltosState implements Cloneable {
                        computed.finish_update();
                }
 
-               AltosCValue() {
-                       measured = new AltosValue();
-                       computed = new AltosValue();
+               public AltosCValue() {
+                       measured = new AltosIValue();
+                       computed = new AltosIValue();
                }
        }
 
-       public int      state;
-       public int      flight;
-       public int      serial;
-       public int      receiver_serial;
+       private int     state;
        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)
@@ -312,6 +321,10 @@ public class AltosState implements Cloneable {
                        pad_alt = value();
                        gps_altitude.set_gps_height();
                }
+
+               AltosGpsGroundAltitude() {
+                       super();
+               }
        }
 
        private AltosGpsGroundAltitude gps_ground_altitude;
@@ -335,6 +348,10 @@ public class AltosState implements Cloneable {
                        super.set_measured(p, time);
                        ground_altitude.set_computed(pressure_to_altitude(p), time);
                }
+
+               AltosGroundPressure () {
+                       super();
+               }
        }
 
        private AltosGroundPressure ground_pressure;
@@ -365,6 +382,10 @@ public class AltosState implements Cloneable {
                        set_speed(measured);
                        set |= set_position;
                }
+
+               AltosAltitude() {
+                       super();
+               }
        }
 
        private AltosAltitude   altitude;
@@ -385,10 +406,19 @@ public class AltosState implements Cloneable {
                        super.set(a, t);
                        set_gps_height();
                }
+
+               AltosGpsAltitude() {
+                       super();
+               }
        }
 
        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)
@@ -404,6 +434,11 @@ public class AltosState implements Cloneable {
        }
 
        public void set_altitude(double new_altitude) {
+               double old_altitude = altitude.value();
+               if (old_altitude != AltosLib.MISSING) {
+                       while (old_altitude - new_altitude > 32000)
+                               new_altitude += 65536.0;
+               }
                altitude.set_measured(new_altitude, time);
        }
 
@@ -419,6 +454,34 @@ public class AltosState implements Cloneable {
                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);
@@ -427,6 +490,10 @@ public class AltosState implements Cloneable {
                        double a = pressure_to_altitude(p);
                        altitude.set_computed(a, time);
                }
+
+               AltosPressure() {
+                       super();
+               }
        }
 
        private AltosPressure   pressure;
@@ -439,11 +506,26 @@ public class AltosState implements Cloneable {
                pressure.set(p, time);
        }
 
-       public double height() {
-               double k = kalman_height.value();
-               if (k != AltosLib.MISSING)
-                       return k;
+       class AltosForce extends AltosValue {
+               void set(double p, double time) {
+                       super.set(p, time);
+               }
+
+               AltosForce() {
+                       super();
+               }
+       }
+       private AltosForce      thrust;
+
+       public double thrust() {
+               return thrust.value();
+       }
+
+       public void set_thrust(double N) {
+               thrust.set(N, time);
+       }
 
+       public double baro_height() {
                double a = altitude();
                double g = ground_altitude();
                if (a != AltosLib.MISSING && g != AltosLib.MISSING)
@@ -451,6 +533,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)
@@ -460,7 +554,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() {
@@ -484,7 +578,7 @@ public class AltosState implements Cloneable {
        class AltosSpeed extends AltosCValue {
 
                boolean can_max() {
-                       return state < AltosLib.ao_flight_fast;
+                       return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
                }
 
                void set_accel() {
@@ -505,6 +599,10 @@ public class AltosState implements Cloneable {
                        super.set_measured(new_value, time);
                        set_accel();
                }
+
+               AltosSpeed() {
+                       super();
+               }
        }
 
        private AltosSpeed speed;
@@ -513,20 +611,32 @@ 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 {
 
                boolean can_max() {
-                       return state < AltosLib.ao_flight_fast;
+                       return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
                }
 
                void set_measured(double a, double time) {
@@ -534,6 +644,10 @@ public class AltosState implements Cloneable {
                        if (ascent)
                                speed.set_integral(this.measured);
                }
+
+               AltosAccel() {
+                       super();
+               }
        }
 
        AltosAccel acceleration;
@@ -546,10 +660,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() {
@@ -563,6 +677,11 @@ public class AltosState implements Cloneable {
        public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
 
        public void set_kalman(double height, double speed, double acceleration) {
+               double old_height = kalman_height.value();
+               if (old_height != AltosLib.MISSING) {
+                       while (old_height - height > 32000)
+                               height += 65536;
+               }
                kalman_height.set(height, time);
                kalman_speed.set(speed, time);
                kalman_acceleration.set(acceleration, time);
@@ -574,13 +693,10 @@ public class AltosState implements Cloneable {
        public double   apogee_voltage;
        public double   main_voltage;
 
-       public double   ignitor_voltage[];
+       public double   igniter_voltage[];
 
        public AltosGPS gps;
-       public AltosGPS temp_gps;
-       public int temp_gps_sat_tick;
        public boolean  gps_pending;
-       public int gps_sequence;
 
        public AltosIMU imu;
        public AltosMag mag;
@@ -607,18 +723,16 @@ public class AltosState implements Cloneable {
        public String   callsign;
        public String   firmware_version;
 
-       public double   accel_plus_g;
-       public double   accel_minus_g;
-       public double   accel;
        public double   ground_accel;
-       public double   ground_accel_avg;
 
        public int      log_format;
-
-       public AltosMs5607      baro;
+       public int      log_space;
+       public String   product;
 
        public AltosCompanion   companion;
 
+       public int      pyro_fired;
+
        public void set_npad(int npad) {
                this.npad = npad;
                gps_waiting = MIN_PAD_SAMPLES - npad;
@@ -632,52 +746,48 @@ public class AltosState implements Cloneable {
 
                received_time = System.currentTimeMillis();
                time = AltosLib.MISSING;
-               time_change = AltosLib.MISSING;
-               prev_time = AltosLib.MISSING;
-               tick = AltosLib.MISSING;
-               prev_tick = AltosLib.MISSING;
-               boost_tick = AltosLib.MISSING;
                state = AltosLib.ao_flight_invalid;
-               flight = AltosLib.MISSING;
                landed = false;
                boost = false;
                rssi = AltosLib.MISSING;
                status = 0;
-               device_type = AltosLib.MISSING;
-               config_major = AltosLib.MISSING;
-               config_minor = AltosLib.MISSING;
-               apogee_delay = AltosLib.MISSING;
-               main_deploy = AltosLib.MISSING;
-               flight_log_max = AltosLib.MISSING;
 
                ground_altitude = new AltosCValue();
                ground_pressure = new AltosGroundPressure();
                altitude = new AltosAltitude();
                pressure = new AltosPressure();
+               thrust = new AltosForce();
                speed = new AltosSpeed();
                acceleration = new AltosAccel();
-               orient = new AltosValue();
+               orient = new AltosCValue();
 
                temperature = AltosLib.MISSING;
                battery_voltage = AltosLib.MISSING;
                pyro_voltage = AltosLib.MISSING;
                apogee_voltage = AltosLib.MISSING;
                main_voltage = AltosLib.MISSING;
-               ignitor_voltage = null;
+               igniter_voltage = null;
 
                kalman_height = new AltosValue();
                kalman_speed = new AltosValue();
                kalman_acceleration = new AltosValue();
 
                gps = null;
-               temp_gps = null;
-               temp_gps_sat_tick = 0;
-               gps_sequence = 0;
                gps_pending = false;
 
                imu = null;
+               last_imu_time = AltosLib.MISSING;
+               rotation = null;
+               ground_rotation = null;
+
                mag = null;
 
+               accel_ground_along = AltosLib.MISSING;
+               accel_ground_across = AltosLib.MISSING;
+               accel_ground_through = AltosLib.MISSING;
+
+               pad_orientation = AltosLib.MISSING;
+
                set_npad(0);
                ngps = 0;
 
@@ -692,30 +802,29 @@ 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();
 
                speak_tick = AltosLib.MISSING;
                speak_altitude = AltosLib.MISSING;
 
                callsign = null;
-
-               accel_plus_g = AltosLib.MISSING;
-               accel_minus_g = AltosLib.MISSING;
-               accel = AltosLib.MISSING;
+               firmware_version = null;
 
                ground_accel = AltosLib.MISSING;
-               ground_accel_avg = AltosLib.MISSING;
 
                log_format = AltosLib.MISSING;
-               serial = AltosLib.MISSING;
-               receiver_serial = AltosLib.MISSING;
+               log_space = AltosLib.MISSING;
+               product = null;
 
-               baro = null;
                companion = null;
+
+               pyro_fired = 0;
        }
 
        void finish_update() {
-               prev_tick = tick;
-
                ground_altitude.finish_update();
                altitude.finish_update();
                pressure.finish_update();
@@ -728,135 +837,21 @@ public class AltosState implements Cloneable {
                kalman_acceleration.finish_update();
        }
 
-       void copy(AltosState old) {
-
-               if (old == null) {
-                       init();
-                       return;
-               }
-
-               received_time = old.received_time;
-               time = old.time;
-               time_change = old.time_change;
-               prev_time = old.time;
-
-               tick = old.tick;
-               prev_tick = old.tick;
-               boost_tick = old.boost_tick;
-
-               state = old.state;
-               flight = old.flight;
-               landed = old.landed;
-               ascent = old.ascent;
-               boost = old.boost;
-               rssi = old.rssi;
-               status = old.status;
-               device_type = old.device_type;
-               config_major = old.config_major;
-               config_minor = old.config_minor;
-               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;
-               temperature = old.temperature;
-               apogee_voltage = old.apogee_voltage;
-               main_voltage = old.main_voltage;
-               ignitor_voltage = old.ignitor_voltage;
-
-               kalman_height.copy(old.kalman_height);
-               kalman_speed.copy(old.kalman_speed);
-               kalman_acceleration.copy(old.kalman_acceleration);
-
-               if (old.gps != null)
-                       gps = old.gps.clone();
-               else
-                       gps = null;
-               if (old.temp_gps != null)
-                       temp_gps = old.temp_gps.clone();
-               else
-                       temp_gps = null;
-               temp_gps_sat_tick = old.temp_gps_sat_tick;
-               gps_sequence = old.gps_sequence;
-               gps_pending = old.gps_pending;
-
-               if (old.imu != null)
-                       imu = old.imu.clone();
-               else
-                       imu = null;
-
-               if (old.mag != null)
-                       mag = old.mag.clone();
-               else
-                       mag = null;
-
-               npad = old.npad;
-               gps_waiting = old.gps_waiting;
-               gps_ready = old.gps_ready;
-               ngps = old.ngps;
-
-               if (old.from_pad != null)
-                       from_pad = old.from_pad.clone();
-               else
-                       from_pad = null;
-
-               elevation = old.elevation;
-               range = old.range;
-
-               gps_height = old.gps_height;
-
-               gps_altitude.copy(old.gps_altitude);
-               gps_ground_altitude.copy(old.gps_ground_altitude);
-
-               pad_lat = old.pad_lat;
-               pad_lon = old.pad_lon;
-               pad_alt = old.pad_alt;
-
-               speak_tick = old.speak_tick;
-               speak_altitude = old.speak_altitude;
-
-               callsign = old.callsign;
-
-               accel_plus_g = old.accel_plus_g;
-               accel_minus_g = old.accel_minus_g;
-               accel = old.accel;
-               ground_accel = old.ground_accel;
-               ground_accel_avg = old.ground_accel_avg;
-
-               log_format = old.log_format;
-               serial = old.serial;
-               receiver_serial = old.receiver_serial;
-
-               baro = old.baro;
-               companion = old.companion;
-       }
-
        void update_time() {
        }
 
        void update_gps() {
-               elevation = 0;
-               range = -1;
+               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;
                                        gps_ground_altitude.set_filtered(gps.alt, time);
@@ -868,6 +863,15 @@ public class AltosState implements Cloneable {
                                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 &&
@@ -883,24 +887,6 @@ public class AltosState implements Cloneable {
                }
        }
 
-       public void set_tick(int new_tick) {
-               if (new_tick != AltosLib.MISSING) {
-                       if (prev_tick != AltosLib.MISSING) {
-                               while (new_tick < prev_tick - 1000) {
-                                       new_tick += 65536;
-                               }
-                       }
-                       tick = new_tick;
-                       time = tick / 100.0;
-                       time_change = time - prev_time;
-               }
-       }
-
-       public void set_boost_tick(int boost_tick) {
-               if (boost_tick != AltosLib.MISSING)
-                       this.boost_tick = boost_tick;
-       }
-
        public String state_name() {
                return AltosLib.state_name(state);
        }
@@ -912,59 +898,14 @@ 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;
-       }
-
-       public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
-               config_major = major;
-               config_minor = minor;
-               this.apogee_delay = apogee_delay;
-               this.main_deploy = main_deploy;
-               this.flight_log_max = flight_log_max;
-       }
-
-       public void set_callsign(String callsign) {
-               this.callsign = callsign;
-       }
-
-       public void set_firmware_version(String version) {
-               firmware_version = version;
-       }
-
-       public void set_flight(int flight) {
-
-               /* When the flight changes, reset the state */
-               if (flight != AltosLib.MISSING && flight != 0) {
-                       if (this.flight != AltosLib.MISSING &&
-                           this.flight != flight) {
-                               int bt = boost_tick;
-                               init();
-                               boost_tick = bt;
-                       }
-                       this.flight = flight;
-               }
        }
 
-       public void set_serial(int serial) {
-               /* When the serial changes, reset the state */
-               if (serial != AltosLib.MISSING) {
-                       if (this.serial != AltosLib.MISSING &&
-                           this.serial != serial) {
-                               int bt = boost_tick;
-                               init();
-                               boost_tick = bt;
-                       }
-                       this.serial = serial;
-               }
+       public int state() {
+               return state;
        }
 
-       public void set_receiver_serial(int serial) {
-               if (serial != AltosLib.MISSING)
-                       receiver_serial = serial;
+       private void re_init() {
+               init();
        }
 
        public int rssi() {
@@ -984,95 +925,144 @@ public class AltosState implements Cloneable {
                received_time = ms;
        }
 
-       public void set_gps(AltosGPS gps, int sequence) {
+       public void set_gps(AltosGPS gps) {
                if (gps != null) {
-                       this.gps = gps.clone();
-                       gps_sequence = sequence;
+                       this.gps = gps;
                        update_gps();
                        set |= set_gps;
                }
        }
 
-       public void set_imu(AltosIMU imu) {
-               if (imu != null)
-                       imu = imu.clone();
-               this.imu = imu;
+
+       public AltosRotation    rotation;
+       public AltosRotation    ground_rotation;
+
+       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 - cal_data.accel_zero_across),
+                                                    AltosIMU.convert_accel(accel_ground_through - cal_data.accel_zero_through),
+                                                    AltosIMU.convert_accel(accel_ground_along - cal_data.accel_zero_along),
+                                                    pad_orientation);
+                       ground_rotation = rotation;
+                       orient.set_computed(rotation.tilt(), time);
+               }
        }
 
-       public void set_mag(AltosMag mag) {
-               this.mag = mag.clone();
+       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 AltosMs5607 make_baro() {
-               if (baro == null)
-                       baro = new AltosMs5607();
-               return baro;
+       public void set_pad_orientation(int pad_orientation) {
+               this.pad_orientation = pad_orientation;
+               update_pad_rotation();
        }
 
-       public void set_ms5607(AltosMs5607 ms5607) {
-               baro = ms5607;
+       public double   last_imu_time;
 
-               if (baro != null) {
-                       set_pressure(baro.pa);
-                       set_temperature(baro.cc / 100.0);
-               }
+       private double radians(double degrees) {
+               if (degrees == AltosLib.MISSING)
+                       return AltosLib.MISSING;
+               return degrees * Math.PI / 180.0;
        }
 
-       public void set_ms5607(int pres, int temp) {
-               if (baro != null) {
-                       baro.set(pres, temp);
+       private void update_orient() {
+               if (last_imu_time != AltosLib.MISSING) {
+                       double  t = time - last_imu_time;
 
-                       set_pressure(baro.pa);
-                       set_temperature(baro.cc / 100.0);
+                       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 make_companion (int nchannels) {
-               if (companion == null)
-                       companion = new AltosCompanion(nchannels);
+       private double  gyro_roll, gyro_pitch, gyro_yaw;
+
+       public void set_gyro(double roll, double pitch, double yaw) {
+               gyro_roll = roll;
+               gyro_pitch = roll;
+               gyro_roll = roll;
+               update_orient();
        }
 
-       public void set_companion(AltosCompanion companion) {
-               this.companion = companion;
+       private double accel_along, accel_across, accel_through;
+
+       public void set_accel(double along, double across, double through) {
+               accel_along = along;
+               accel_across = across;
+               accel_through = through;
+               update_orient();
        }
 
-       void update_accel() {
-               if (accel == AltosLib.MISSING)
-                       return;
-               if (accel_plus_g == AltosLib.MISSING)
-                       return;
-               if (accel_minus_g == AltosLib.MISSING)
-                       return;
+       public double accel_along() {
+               return accel_along;
+       }
 
-               double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
-               double counts_per_mss = counts_per_g / 9.80665;
-               acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
+       public double accel_across() {
+               return accel_across;
        }
 
-       public void set_accel_g(double accel_plus_g, double accel_minus_g) {
-               if (accel_plus_g != AltosLib.MISSING) {
-                       this.accel_plus_g = accel_plus_g;
-                       this.accel_minus_g = accel_minus_g;
-                       update_accel();
-               }
+       public double accel_through() {
+               return accel_through;
        }
 
-       public void set_ground_accel(double ground_accel) {
-               if (ground_accel != AltosLib.MISSING)
-                       this.ground_accel = ground_accel;
+       public double gyro_roll() {
+               return gyro_roll;
        }
 
-       public void set_accel(double accel) {
-               if (accel != AltosLib.MISSING) {
-                       this.accel = accel;
-                       if (state == AltosLib.ao_flight_pad) {
-                               if (ground_accel_avg == AltosLib.MISSING)
-                                       ground_accel_avg = accel;
-                               else
-                                       ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
-                       }
+       public double gyro_pitch() {
+               return gyro_pitch;
+       }
+
+       public double gyro_yaw() {
+               return gyro_yaw;
+       }
+
+       private double mag_along, mag_across, mag_through;
+
+       public void set_mag(double along, double across, double through) {
+               mag_along = along;
+               mag_across = across;
+               mag_through = through;
+       }
+
+       public double mag_along() {
+               return mag_along;
+       }
+
+       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 void set_companion(AltosCompanion companion) {
+               this.companion = companion;
+       }
+
+       public void set_acceleration(double acceleration) {
+               if (acceleration != AltosLib.MISSING) {
+                       this.acceleration.set_measured(acceleration, time);
+                       set |= set_data;
                }
-               update_accel();
        }
 
        public void set_temperature(double temperature) {
@@ -1110,49 +1100,16 @@ public class AltosState implements Cloneable {
                }
        }
 
-       public void set_ignitor_voltage(double[] voltage) {
-               this.ignitor_voltage = voltage;
-       }
-
-       public double time_since_boost() {
-               if (tick == AltosLib.MISSING)
-                       return 0.0;
-
-               if (boost_tick == AltosLib.MISSING)
-                       return tick / 100.0;
-               return (tick - boost_tick) / 100.0;
-       }
-
-       public boolean valid() {
-               return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
-       }
-
-       public AltosGPS make_temp_gps(boolean sats) {
-               if (temp_gps == null) {
-                       temp_gps = new AltosGPS(gps);
-               }
-               gps_pending = true;
-               if (sats) {
-                       if (tick != temp_gps_sat_tick)
-                               temp_gps.cc_gps_sat = null;
-                       temp_gps_sat_tick = tick;
-               }
-               return temp_gps;
-       }
-
-       public void set_temp_gps() {
-               set_gps(temp_gps, gps_sequence + 1);
-               gps_pending = false;
-               temp_gps = null;
+       public void set_igniter_voltage(double[] voltage) {
+               this.igniter_voltage = voltage;
        }
 
-       public AltosState clone() {
-               AltosState s = new AltosState();
-               s.copy(this);
-               return s;
+       public void set_pyro_fired(int fired) {
+               this.pyro_fired = fired;
        }
 
-       public AltosState () {
+       public AltosState (AltosCalData cal_data) {
+               super(cal_data);
                init();
        }
 }