2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 * Track flight state from telemetry or eeprom data stream
24 import org.altusmetrum.AltosLib.*;
26 public class AltosState {
39 boolean ascent; /* going up? */
40 boolean boost; /* under power */
42 double ground_altitude;
53 double max_acceleration;
55 double max_baro_speed;
66 static final int MIN_PAD_SAMPLES = 10;
73 AltosGreatCircle from_pad;
74 double elevation; /* from pad */
75 double range; /* total distance */
80 double speak_altitude;
82 void init (AltosRecord cur, AltosState prev_state) {
88 ground_altitude = data.ground_altitude();
89 height = data.filtered_height();
91 report_time = System.currentTimeMillis();
93 acceleration = data.acceleration();
94 speed = data.accel_speed();
95 temperature = data.temperature();
96 drogue_sense = data.drogue_voltage();
97 main_sense = data.main_voltage();
98 battery = data.battery_voltage();
102 if (prev_state != null) {
104 /* Preserve any existing gps data */
105 npad = prev_state.npad;
106 ngps = prev_state.ngps;
107 gps = prev_state.gps;
108 pad_lat = prev_state.pad_lat;
109 pad_lon = prev_state.pad_lon;
110 pad_alt = prev_state.pad_alt;
111 max_height = prev_state.max_height;
112 max_acceleration = prev_state.max_acceleration;
113 max_speed = prev_state.max_speed;
114 max_baro_speed = prev_state.max_baro_speed;
115 imu = prev_state.imu;
116 mag = prev_state.mag;
118 /* make sure the clock is monotonic */
119 while (tick < prev_state.tick)
122 time_change = (tick - prev_state.tick) / 100.0;
124 /* compute barometric speed */
126 double height_change = height - prev_state.height;
127 if (data.speed != AltosRecord.MISSING)
128 baro_speed = data.speed;
131 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
133 baro_speed = prev_state.baro_speed;
145 if (cur.new_gps && (state == Altos.ao_flight_pad || state == Altos.ao_flight_idle)) {
147 /* Track consecutive 'good' gps reports, waiting for 10 of them */
148 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
153 /* Average GPS data while on the pad */
154 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
156 /* filter pad position */
157 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
158 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
159 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
161 pad_lat = data.gps.lat;
162 pad_lon = data.gps.lon;
163 pad_alt = data.gps.alt;
169 gps_waiting = MIN_PAD_SAMPLES - npad;
173 gps_ready = gps_waiting == 0;
175 ascent = (Altos.ao_flight_boost <= state &&
176 state <= Altos.ao_flight_coast);
177 boost = (Altos.ao_flight_boost == state);
179 /* Only look at accelerometer data under boost */
180 if (boost && acceleration > max_acceleration)
181 max_acceleration = acceleration;
182 if (boost && speed > max_speed)
184 if (boost && baro_speed > max_baro_speed)
185 max_baro_speed = baro_speed;
187 if (height > max_height)
189 if (data.gps != null) {
190 if (gps == null || !gps.locked || data.gps.locked)
192 if (ngps > 0 && gps.locked) {
193 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
199 gps_height = gps.alt - pad_alt;
200 if (from_pad != null) {
201 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
202 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
209 public AltosState(AltosRecord cur) {
213 public AltosState (AltosRecord cur, AltosState prev) {