altoslib: Fix labels in AltosIMU to match source data
[fw/altos] / altoslib / AltosCalData.java
index 3da0e4009f5da00480cf786c42877a84e7d58552..49b90ce7d5ff9a58f13f7949e2a2f8dd94d0cccb 100644 (file)
@@ -65,8 +65,11 @@ public class AltosCalData {
        public int              device_type = AltosLib.MISSING;
 
        public void set_device_type(int device_type) {
-               if (device_type != AltosLib.MISSING)
+               if (device_type != AltosLib.MISSING) {
                        this.device_type = device_type;
+                       if (product == null)
+                               set_product(AltosLib.product_name(device_type));
+               }
        }
 
        public int              config_major = AltosLib.MISSING;
@@ -98,6 +101,8 @@ public class AltosCalData {
 
        public void set_accel_plus_minus(double plus, double minus) {
                if (plus != AltosLib.MISSING && minus != AltosLib.MISSING) {
+                       if (plus == minus)
+                               return;
                        accel_plus_g = plus;
                        accel_minus_g = minus;
                }
@@ -163,6 +168,7 @@ public class AltosCalData {
        }
 
        public int              tick = AltosLib.MISSING;
+       private int             first_tick = AltosLib.MISSING;
        private int             prev_tick = AltosLib.MISSING;
 
        public void set_tick(int tick) {
@@ -172,28 +178,51 @@ public class AltosCalData {
                                        tick += 65536;
                                }
                        }
+                       if (first_tick == AltosLib.MISSING)
+                               first_tick = tick;
+                       prev_tick = tick;
                        this.tick = tick;
                }
        }
 
+       /* Reset all values which change during flight
+        */
+       public void reset() {
+               state = AltosLib.MISSING;
+               tick = AltosLib.MISSING;
+               prev_tick = AltosLib.MISSING;
+               temp_gps = null;
+               prev_gps = null;
+               temp_gps_sat_tick = AltosLib.MISSING;
+               accel = AltosLib.MISSING;
+       }
+
        public int              boost_tick = AltosLib.MISSING;
 
        public void set_boost_tick() {
                boost_tick = tick;
        }
 
+       public double           ticks_per_sec = 100.0;
+
+       public void set_ticks_per_sec(double ticks_per_sec) {
+               this.ticks_per_sec = ticks_per_sec;
+       }
+
        public double time() {
                if (tick == AltosLib.MISSING)
                        return AltosLib.MISSING;
-               if (boost_tick == AltosLib.MISSING)
-                       return AltosLib.MISSING;
-               return (tick - boost_tick) / 100.0;
+               if (boost_tick != AltosLib.MISSING)
+                       return (tick - boost_tick) / ticks_per_sec;
+               if (first_tick != AltosLib.MISSING)
+                       return (tick - first_tick) / ticks_per_sec;
+               return tick / ticks_per_sec;
        }
 
        public double boost_time() {
                if (boost_tick == AltosLib.MISSING)
                        return AltosLib.MISSING;
-               return boost_tick / 100.0;
+               return boost_tick / ticks_per_sec;
        }
 
        public int              state = AltosLib.MISSING;
@@ -206,9 +235,13 @@ public class AltosCalData {
 
        public AltosGPS         gps_pad = null;
 
+       public double           gps_pad_altitude = AltosLib.MISSING;
+
        public void set_gps(AltosGPS gps) {
                if ((state != AltosLib.MISSING && state < AltosLib.ao_flight_boost) || gps_pad == null)
                        gps_pad = gps;
+               if (gps_pad_altitude == AltosLib.MISSING && gps.alt != AltosLib.MISSING)
+                       gps_pad_altitude = gps.alt;
        }
 
        /*
@@ -216,6 +249,7 @@ public class AltosCalData {
         * object and then deliver the result atomically to the listener
         */
        AltosGPS                temp_gps = null;
+       AltosGPS                prev_gps = null;
        int                     temp_gps_sat_tick = AltosLib.MISSING;
 
        public AltosGPS temp_gps() {
@@ -226,8 +260,9 @@ public class AltosCalData {
                if (temp_gps != null) {
                        if (temp_gps.locked && temp_gps.nsat >= 4)
                                set_gps(temp_gps);
+                       prev_gps = temp_gps;
+                       temp_gps = null;
                }
-               temp_gps = null;
        }
 
        public boolean gps_pending() {
@@ -236,7 +271,10 @@ public class AltosCalData {
 
        public AltosGPS make_temp_gps(int tick, boolean sats) {
                if (temp_gps == null) {
-                       temp_gps = new AltosGPS();
+                       if (prev_gps != null)
+                               temp_gps = prev_gps.clone();
+                       else
+                               temp_gps = new AltosGPS();
                }
                if (sats) {
                        if (tick != temp_gps_sat_tick)
@@ -275,25 +313,27 @@ public class AltosCalData {
                        gyro_zero_roll = roll;
                        gyro_zero_pitch = pitch;
                        gyro_zero_yaw = yaw;
+                       imu_wrap_checked = false;
                }
        }
 
        public double gyro_roll(double counts) {
                if (gyro_zero_roll == AltosLib.MISSING || counts == AltosLib.MISSING)
                        return AltosLib.MISSING;
-               return AltosIMU.convert_gyro(counts - gyro_zero_roll);
+
+               return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_roll);
        }
 
        public double gyro_pitch(double counts) {
                if (gyro_zero_pitch == AltosLib.MISSING || counts == AltosLib.MISSING)
                        return AltosLib.MISSING;
-               return AltosIMU.convert_gyro(counts - gyro_zero_pitch);
+               return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_pitch);
        }
 
        public double gyro_yaw(double counts) {
                if (gyro_zero_yaw == AltosLib.MISSING || counts == AltosLib.MISSING)
                        return AltosLib.MISSING;
-               return AltosIMU.convert_gyro(counts - gyro_zero_yaw);
+               return AltosIMU.gyro_degrees_per_second(counts, gyro_zero_yaw);
        }
 
        private double gyro_zero_overflow(double first) {
@@ -302,13 +342,25 @@ public class AltosCalData {
                        v = Math.ceil(v);
                else
                        v = Math.floor(v);
+               if (v != 0)
+                       System.out.printf("Adjusting gyro axis by %g steps\n", v);
                return v * 128.0;
        }
 
+       /* Initial TeleMega log format had only 16 bits for gyro cal, so the top 9 bits got lost as the
+        * cal data are scaled by 512. Use the first sample to adjust the cal value, assuming that it is
+        * from a time of fairly low rotation speed. Fixed in later TeleMega firmware by storing 32 bits
+        * of cal values.
+        */
+       private boolean imu_wrap_checked = false;
+
        public void check_imu_wrap(double roll, double pitch, double yaw) {
-               gyro_zero_roll += gyro_zero_overflow(roll);
-               gyro_zero_pitch += gyro_zero_overflow(pitch);
-               gyro_zero_yaw += gyro_zero_overflow(yaw);
+               if (!imu_wrap_checked) {
+                       gyro_zero_roll += gyro_zero_overflow(roll);
+                       gyro_zero_pitch += gyro_zero_overflow(pitch);
+                       gyro_zero_yaw += gyro_zero_overflow(yaw);
+                       imu_wrap_checked = true;
+               }
        }
 
        public double mag_along(double along) {
@@ -334,9 +386,16 @@ public class AltosCalData {
 
        public AltosCalData(AltosConfigData config_data) {
                set_serial(config_data.serial);
+               set_ticks_per_sec(100.0);
                set_flight(config_data.flight);
                set_callsign(config_data.callsign);
+               set_config(config_data.config_major, config_data.config_minor, config_data.flight_log_max);
+               set_firmware_version(config_data.version);
+               set_flight_params(config_data.apogee_delay / ticks_per_sec, config_data.apogee_lockout / ticks_per_sec);
+               set_pad_orientation(config_data.pad_orientation);
+               set_product(config_data.product);
                set_accel_plus_minus(config_data.accel_cal_plus, config_data.accel_cal_minus);
+               set_accel_zero(config_data.accel_zero_along, config_data.accel_zero_across, config_data.accel_zero_through);
                set_ms5607(config_data.ms5607);
                try {
                        set_mma655x_inverted(config_data.mma655x_inverted());