add thrust as a graphable time series type
[fw/altos] / altoslib / AltosState.java
index 5be008b7ed524e694886fb3487fe20f5113786e6..846bda42dd834f454c8dc225de5bc4a42a44643f 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_5;
+package org.altusmetrum.altoslib_11;
 
 import java.io.*;
 
-public class AltosState implements Cloneable, Serializable {
+public class AltosState extends AltosFlightListener implements Cloneable {
 
        public static final int set_position = 1;
        public static final int set_gps = 2;
@@ -31,8 +32,9 @@ public class AltosState implements Cloneable, Serializable {
 
        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 */
 
@@ -41,11 +43,9 @@ public class AltosState implements Cloneable, Serializable {
        public double   time;
        public double   prev_time;
        public double   time_change;
-       public int      tick;
        private int     prev_tick;
-       public int      boost_tick;
 
-       class AltosValue implements Serializable{
+       class AltosValue {
                double  value;
                double  prev_value;
                private double  max_value;
@@ -64,8 +64,10 @@ public class AltosState implements Cloneable, Serializable {
                }
 
                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);
                }
 
@@ -179,14 +181,19 @@ public class AltosState implements Cloneable, Serializable {
                        prev_value = AltosLib.MISSING;
                        max_value = AltosLib.MISSING;
                }
+
        }
 
-       class AltosCValue implements Serializable {
+       class AltosCValue {
 
-               class AltosIValue extends AltosValue implements Serializable {
+               class AltosIValue extends AltosValue {
                        boolean can_max() {
                                return c_can_max();
                        }
+
+                       AltosIValue() {
+                               super();
+                       }
                };
 
                public AltosIValue      measured;
@@ -275,13 +282,13 @@ public class AltosState implements Cloneable, Serializable {
                        computed.finish_update();
                }
 
-               AltosCValue() {
+               public AltosCValue() {
                        measured = new AltosIValue();
                        computed = new AltosIValue();
                }
        }
 
-       public int      state;
+       private int     state;
        public int      flight;
        public int      serial;
        public int      altitude_32;
@@ -314,7 +321,7 @@ public class AltosState implements Cloneable, Serializable {
                ground_altitude.set_measured(a, time);
        }
 
-       class AltosGpsGroundAltitude extends AltosValue implements Serializable {
+       class AltosGpsGroundAltitude extends AltosValue {
                void set(double a, double t) {
                        super.set(a, t);
                        pad_alt = value();
@@ -326,6 +333,10 @@ public class AltosState implements Cloneable, Serializable {
                        pad_alt = value();
                        gps_altitude.set_gps_height();
                }
+
+               AltosGpsGroundAltitude() {
+                       super();
+               }
        }
 
        private AltosGpsGroundAltitude gps_ground_altitude;
@@ -338,7 +349,7 @@ public class AltosState implements Cloneable, Serializable {
                gps_ground_altitude.set(a, time);
        }
 
-       class AltosGroundPressure extends AltosCValue implements Serializable {
+       class AltosGroundPressure extends AltosCValue {
                void set_filtered(double p, double time) {
                        computed.set_filtered(p, time);
                        if (!is_measured())
@@ -349,6 +360,10 @@ public class AltosState implements Cloneable, Serializable {
                        super.set_measured(p, time);
                        ground_altitude.set_computed(pressure_to_altitude(p), time);
                }
+
+               AltosGroundPressure () {
+                       super();
+               }
        }
 
        private AltosGroundPressure ground_pressure;
@@ -361,7 +376,7 @@ public class AltosState implements Cloneable, Serializable {
                ground_pressure.set_measured(pressure, time);
        }
 
-       class AltosAltitude extends AltosCValue implements Serializable {
+       class AltosAltitude extends AltosCValue {
 
                private void set_speed(AltosValue v) {
                        if (!acceleration.is_measured() || !ascent)
@@ -379,11 +394,15 @@ public class AltosState implements Cloneable, Serializable {
                        set_speed(measured);
                        set |= set_position;
                }
+
+               AltosAltitude() {
+                       super();
+               }
        }
 
        private AltosAltitude   altitude;
 
-       class AltosGpsAltitude extends AltosValue implements Serializable {
+       class AltosGpsAltitude extends AltosValue {
 
                private void set_gps_height() {
                        double  a = value();
@@ -399,6 +418,10 @@ public class AltosState implements Cloneable, Serializable {
                        super.set(a, t);
                        set_gps_height();
                }
+
+               AltosGpsAltitude() {
+                       super();
+               }
        }
 
        private AltosGpsAltitude        gps_altitude;
@@ -466,7 +489,7 @@ public class AltosState implements Cloneable, Serializable {
                return gps_speed.max();
        }
 
-       class AltosPressure extends AltosValue implements Serializable {
+       class AltosPressure extends AltosValue {
                void set(double p, double time) {
                        super.set(p, time);
                        if (state == AltosLib.ao_flight_pad)
@@ -474,6 +497,10 @@ public class AltosState implements Cloneable, Serializable {
                        double a = pressure_to_altitude(p);
                        altitude.set_computed(a, time);
                }
+
+               AltosPressure() {
+                       super();
+               }
        }
 
        private AltosPressure   pressure;
@@ -486,6 +513,25 @@ public class AltosState implements Cloneable, Serializable {
                pressure.set(p, time);
        }
 
+       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();
@@ -536,7 +582,7 @@ public class AltosState implements Cloneable, Serializable {
                return AltosLib.MISSING;
        }
 
-       class AltosSpeed extends AltosCValue implements Serializable {
+       class AltosSpeed extends AltosCValue {
 
                boolean can_max() {
                        return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
@@ -560,6 +606,10 @@ public class AltosState implements Cloneable, Serializable {
                        super.set_measured(new_value, time);
                        set_accel();
                }
+
+               AltosSpeed() {
+                       super();
+               }
        }
 
        private AltosSpeed speed;
@@ -590,7 +640,7 @@ public class AltosState implements Cloneable, Serializable {
                return AltosLib.MISSING;
        }
 
-       class AltosAccel extends AltosCValue implements Serializable {
+       class AltosAccel extends AltosCValue {
 
                boolean can_max() {
                        return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
@@ -601,6 +651,10 @@ public class AltosState implements Cloneable, Serializable {
                        if (ascent)
                                speed.set_integral(this.measured);
                }
+
+               AltosAccel() {
+                       super();
+               }
        }
 
        AltosAccel acceleration;
@@ -681,6 +735,7 @@ public class AltosState implements Cloneable, Serializable {
        public double   ground_accel_avg;
 
        public int      log_format;
+       public int      log_space;
        public String   product;
 
        public AltosMs5607      baro;
@@ -724,6 +779,7 @@ public class AltosState implements Cloneable, Serializable {
                ground_pressure = new AltosGroundPressure();
                altitude = new AltosAltitude();
                pressure = new AltosPressure();
+               thrust = new AltosForce();
                speed = new AltosSpeed();
                acceleration = new AltosAccel();
                orient = new AltosCValue();
@@ -798,6 +854,7 @@ public class AltosState implements Cloneable, Serializable {
                ground_accel_avg = AltosLib.MISSING;
 
                log_format = AltosLib.MISSING;
+               log_space = AltosLib.MISSING;
                product = null;
                serial = AltosLib.MISSING;
                receiver_serial = AltosLib.MISSING;
@@ -831,6 +888,8 @@ public class AltosState implements Cloneable, Serializable {
                        return;
                }
 
+               super.copy(old);
+
                received_time = old.received_time;
                time = old.time;
                time_change = old.time_change;
@@ -957,6 +1016,7 @@ public class AltosState implements Cloneable, Serializable {
                ground_accel_avg = old.ground_accel_avg;
 
                log_format = old.log_format;
+               log_space = old.log_space;
                product = old.product;
                serial = old.serial;
                receiver_serial = old.receiver_serial;
@@ -1031,15 +1091,14 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
-       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);
        }
 
+       public void set_product(String product) {
+               this.product = product;
+       }
+
        public void set_state(int state) {
                if (state != AltosLib.ao_flight_invalid) {
                        this.state = state;
@@ -1049,6 +1108,10 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
+       public int state() {
+               return state;
+       }
+
        public void set_device_type(int device_type) {
                this.device_type = device_type;
                switch (device_type) {
@@ -1067,6 +1130,10 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
+       public void set_log_space(int log_space) {
+               this.log_space = log_space;
+       }
+
        public void set_flight_params(int apogee_delay, int main_deploy) {
                this.apogee_delay = apogee_delay;
                this.main_deploy = main_deploy;
@@ -1336,7 +1403,7 @@ public class AltosState implements Cloneable, Serializable {
        public void set_ms5607(AltosMs5607 ms5607) {
                baro = ms5607;
 
-               if (baro != null) {
+               if (baro != null && baro.pa != AltosLib.MISSING && baro.cc != AltosLib.MISSING) {
                        set_pressure(baro.pa);
                        set_temperature(baro.cc / 100.0);
                }
@@ -1351,11 +1418,6 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
-       public void make_companion (int nchannels) {
-               if (companion == null)
-                       companion = new AltosCompanion(nchannels);
-       }
-
        public void set_companion(AltosCompanion companion) {
                this.companion = companion;
        }
@@ -1455,28 +1517,57 @@ public class AltosState implements Cloneable, Serializable {
                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;
+               super.set_temp_gps();
+       }
+
+       public void set_config_data(AltosConfigData config_data) {
+               if (config_data.callsign != null)
+                       set_callsign(config_data.callsign);
+               if (config_data.accel_cal_plus != AltosLib.MISSING &&
+                   config_data.accel_cal_minus != AltosLib.MISSING)
+                       set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
+               if (config_data.product != null)
+                       set_product(config_data.product);
+               if (config_data.log_format != AltosLib.MISSING)
+                       set_log_format(config_data.log_format);
+               if (config_data.serial != AltosLib.MISSING)
+                       set_serial(config_data.serial);
+               AltosMs5607 ms5607 = new AltosMs5607(config_data);
+               if (ms5607.valid_config())
+                       set_ms5607(ms5607);
        }
 
        public AltosState clone() {
                AltosState s = new AltosState();
                s.copy(this);
+
+               /* Code to test state save/restore. Enable only for that purpose
+                */
+               if (false) {
+                       AltosJson       json = new AltosJson(this);
+                       String          onetrip = json.toPrettyString();
+                       AltosJson       back = AltosJson.fromString(onetrip);
+                       AltosState      tripstate = (AltosState) back.make(this.getClass());
+                       AltosJson       tripjson = new AltosJson(tripstate);
+                       String          twotrip = tripjson.toPrettyString();
+
+                       if (!onetrip.equals(twotrip)) {
+                               try {
+                                       FileWriter one_file = new FileWriter("one.json", true);
+                                       one_file.write(onetrip);
+                                       one_file.flush();
+                                       FileWriter two_file = new FileWriter("two.json", true);
+                                       two_file.write(twotrip);
+                                       two_file.flush();
+                               } catch (Exception e) {
+                               }
+                               System.out.printf("json error\n");
+                               System.exit(1);
+                       }
+               }
                return s;
        }