altoslib: Add ADXL375 support and EasyMega v2.0 support
[fw/altos] / altoslib / AltosCalData.java
index 2eff6ac1188610753c5b38c212183fbf2f0ac405..c3d79250a6e54cb23e25a2fb870e988f16dccce5 100644 (file)
@@ -12,7 +12,7 @@
  * General Public License for more details.
  */
 
-package org.altusmetrum.altoslib_11;
+package org.altusmetrum.altoslib_13;
 
 /*
  * Calibration and other data needed to construct 'real' values from various data
@@ -72,6 +72,13 @@ public class AltosCalData {
                }
        }
 
+       public int              log_format = AltosLib.MISSING;
+
+       public void set_log_format(int log_format) {
+               if (log_format != AltosLib.MISSING)
+                       this.log_format = log_format;
+       }
+
        public int              config_major = AltosLib.MISSING;
        public int              config_minor = AltosLib.MISSING;
        public int              flight_log_max = AltosLib.MISSING;
@@ -126,6 +133,18 @@ public class AltosCalData {
                mma655x_inverted = inverted;
        }
 
+       public boolean adxl375_inverted = false;
+
+       public void set_adxl375_inverted(boolean inverted) {
+               adxl375_inverted = inverted;
+       }
+
+       public int adxl375_axis = AltosLib.MISSING;
+
+       public void set_adxl375_axis(int axis) {
+               adxl375_axis = axis;
+       }
+
        public int pad_orientation = AltosLib.MISSING;
 
        public void set_pad_orientation(int orientation) {
@@ -135,7 +154,11 @@ public class AltosCalData {
 
        /* Compute acceleration */
        public double acceleration(double sensor) {
-               return AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
+               double accel;
+               accel = AltosConvert.acceleration_from_sensor(sensor, accel_plus_g, accel_minus_g, ground_accel);
+               System.out.printf("acceleration %g (+ %g - %g g %g) -> %g\n",
+                                 sensor, accel_plus_g, accel_minus_g, ground_accel, accel);
+               return accel;
        }
 
        public AltosMs5607      ms5607 = null;
@@ -192,7 +215,6 @@ public class AltosCalData {
                tick = AltosLib.MISSING;
                prev_tick = AltosLib.MISSING;
                temp_gps = null;
-               prev_gps = null;
                temp_gps_sat_tick = AltosLib.MISSING;
                accel = AltosLib.MISSING;
        }
@@ -235,13 +257,19 @@ public class AltosCalData {
 
        public AltosGPS         gps_pad = null;
 
+       public AltosGPS         prev_gps = 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;
+       public void set_cal_gps(AltosGPS gps) {
+               if (gps.locked && gps.nsat >= 4) {
+                       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;
+               }
+               temp_gps = null;
+               prev_gps = gps;
        }
 
        /*
@@ -249,33 +277,24 @@ 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() {
+       public AltosGPS temp_cal_gps() {
                return temp_gps;
        }
 
-       public void reset_temp_gps() {
-               if (temp_gps != null) {
-                       if (temp_gps.locked && temp_gps.nsat >= 4)
-                               set_gps(temp_gps);
-                       prev_gps = temp_gps;
-                       temp_gps = null;
-               }
+       public void reset_temp_cal_gps() {
+               if (temp_gps != null)
+                       set_cal_gps(temp_gps);
        }
 
-       public boolean gps_pending() {
+       public boolean cal_gps_pending() {
                return temp_gps != null;
        }
 
-       public AltosGPS make_temp_gps(int tick, boolean sats) {
-               if (temp_gps == null) {
-                       if (prev_gps != null)
-                               temp_gps = prev_gps.clone();
-                       else
-                               temp_gps = new AltosGPS();
-               }
+       public AltosGPS make_temp_cal_gps(int tick, boolean sats) {
+               if (temp_gps == null)
+                       temp_gps = new AltosGPS(prev_gps);
                if (sats) {
                        if (tick != temp_gps_sat_tick)
                                temp_gps.cc_gps_sat = null;
@@ -313,25 +332,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) {
@@ -340,13 +361,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) {
@@ -380,13 +413,21 @@ public class AltosCalData {
                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_plus_minus(config_data.accel_cal_plus(config_data.pad_orientation), config_data.accel_cal_minus(config_data.pad_orientation));
                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());
                } catch (AltosUnknownProduct up) {
                }
+               try {
+                       set_adxl375_inverted(config_data.adxl375_inverted());
+               } catch (AltosUnknownProduct up) {
+               }
+               try {
+                       set_adxl375_axis(config_data.adxl375_axis());
+               } catch (AltosUnknownProduct up) {
+               }
                set_pad_orientation(config_data.pad_orientation);
        }
 }