Merge branch 'master' into droid-gps
[fw/altos] / altoslib / AltosState.java
index f18bf368c22e3314962cfa3c329cef2c190c12d1..a3b9a8c07efafdb54f889094c09e1b7ba6a505f9 100644 (file)
@@ -98,14 +98,16 @@ public class AltosState {
                ground_altitude = data.ground_altitude();
 
                altitude = data.altitude();
+               if (altitude == AltosRecord.MISSING && data.gps != null)
+                       altitude = data.gps.alt;
 
                height = AltosRecord.MISSING;
                if (data.kalman_height != AltosRecord.MISSING)
                        height = data.kalman_height;
                else {
-                       if (prev_state != null && altitude != AltosRecord.MISSING && ground_altitude != AltosRecord.MISSING) {
+                       if (altitude != AltosRecord.MISSING && ground_altitude != AltosRecord.MISSING) {
                                double  cur_height = altitude - ground_altitude;
-                               if (prev_state.height == AltosRecord.MISSING)
+                               if (prev_state == null || prev_state.height == AltosRecord.MISSING)
                                        height = cur_height;
                                else
                                        height = (prev_state.height * 15 + cur_height) / 16.0;
@@ -116,10 +118,8 @@ public class AltosState {
 
                if (data.kalman_acceleration != AltosRecord.MISSING)
                        acceleration = data.kalman_acceleration;
-               else {
+               else
                        acceleration = data.acceleration();
-                       System.out.printf ("data acceleration %g\n", acceleration);
-               }
                temperature = data.temperature();
                drogue_sense = data.drogue_voltage();
                main_sense = data.main_voltage();
@@ -155,34 +155,45 @@ public class AltosState {
                                /* compute barometric speed */
 
                                double height_change = height - prev_state.height;
+
+                               double prev_baro_speed = prev_state.baro_speed;
+                               if (prev_baro_speed == AltosRecord.MISSING)
+                                       prev_baro_speed = 0;
+
                                if (time_change > 0)
-                                       baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
+                                       baro_speed = (prev_baro_speed * 3 + (height_change / time_change)) / 4.0;
                                else
                                        baro_speed = prev_state.baro_speed;
 
+                               double prev_accel_speed = prev_state.accel_speed;
+
+                               if (prev_accel_speed == AltosRecord.MISSING)
+                                       prev_accel_speed = 0;
+
                                if (acceleration == AltosRecord.MISSING) {
                                        /* Fill in mising acceleration value */
                                        accel_speed = baro_speed;
-                                       if (time_change > 0)
-                                               acceleration = (accel_speed - prev_state.accel_speed) / time_change;
+
+                                       if (time_change > 0 && accel_speed != AltosRecord.MISSING)
+                                               acceleration = (accel_speed - prev_accel_speed) / time_change;
                                        else
                                                acceleration = prev_state.acceleration;
                                } else {
                                        /* compute accelerometer speed */
-                                       accel_speed = prev_state.accel_speed + acceleration * time_change;
+                                       accel_speed = prev_accel_speed + acceleration * time_change;
                                }
                        }
-
                } else {
                        npad = 0;
                        ngps = 0;
                        gps = null;
                        baro_speed = AltosRecord.MISSING;
                        accel_speed = AltosRecord.MISSING;
-                       max_baro_speed = AltosRecord.MISSING;
-                       max_accel_speed = AltosRecord.MISSING;
-                       max_height = AltosRecord.MISSING;
-                       max_acceleration = AltosRecord.MISSING;
+                       pad_alt = AltosRecord.MISSING;
+                       max_baro_speed = 0;
+                       max_accel_speed = 0;
+                       max_height = 0;
+                       max_acceleration = 0;
                        time_change = 0;
                }
 
@@ -230,30 +241,28 @@ public class AltosState {
                /* Only look at accelerometer data under boost */
                if (boost && acceleration != AltosRecord.MISSING && (max_acceleration == AltosRecord.MISSING || acceleration > max_acceleration))
                        max_acceleration = acceleration;
-               if (boost && accel_speed != AltosRecord.MISSING && (max_accel_speed == AltosRecord.MISSING || accel_speed > max_accel_speed))
+               if (boost && accel_speed != AltosRecord.MISSING && accel_speed > max_accel_speed)
                        max_accel_speed = accel_speed;
-               if (boost && baro_speed != AltosRecord.MISSING && (max_baro_speed == AltosRecord.MISSING || baro_speed > max_baro_speed))
+               if (boost && baro_speed != AltosRecord.MISSING && baro_speed > max_baro_speed)
                        max_baro_speed = baro_speed;
 
-               if (height != AltosRecord.MISSING || (max_height == AltosRecord.MISSING || height > max_height))
+               if (height != AltosRecord.MISSING && height > max_height)
                        max_height = height;
+               elevation = 0;
+               range = -1;
+               gps_height = 0;
                if (data.gps != null) {
                        if (gps == null || !gps.locked || data.gps.locked)
                                gps = data.gps;
                        if (ngps > 0 && gps.locked) {
-                               from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
-                       }
-               }
-               elevation = 0;
-               range = -1;
-               if (ngps > 0) {
-                       gps_height = gps.alt - pad_alt;
-                       if (from_pad != null) {
-                               elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
-                               range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
+                               double h = height;
+
+                               if (h == AltosRecord.MISSING) h = 0;
+                               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;
                        }
-               } else {
-                       gps_height = 0;
                }
        }