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;
63 static final int MIN_PAD_SAMPLES = 10;
70 AltosGreatCircle from_pad;
71 double elevation; /* from pad */
72 double range; /* total distance */
77 double speak_altitude;
79 void init (AltosRecord cur, AltosState prev_state) {
85 ground_altitude = data.ground_altitude();
86 height = data.filtered_height();
88 report_time = System.currentTimeMillis();
90 acceleration = data.acceleration();
91 speed = data.accel_speed();
92 temperature = data.temperature();
93 drogue_sense = data.drogue_voltage();
94 main_sense = data.main_voltage();
95 battery = data.battery_voltage();
99 if (prev_state != null) {
101 /* Preserve any existing gps data */
102 npad = prev_state.npad;
103 ngps = prev_state.ngps;
104 gps = prev_state.gps;
105 pad_lat = prev_state.pad_lat;
106 pad_lon = prev_state.pad_lon;
107 pad_alt = prev_state.pad_alt;
108 max_height = prev_state.max_height;
109 max_acceleration = prev_state.max_acceleration;
110 max_speed = prev_state.max_speed;
111 max_baro_speed = prev_state.max_baro_speed;
113 /* make sure the clock is monotonic */
114 while (tick < prev_state.tick)
117 time_change = (tick - prev_state.tick) / 100.0;
119 /* compute barometric speed */
121 double height_change = height - prev_state.height;
122 if (data.speed != AltosRecord.MISSING)
123 baro_speed = data.speed;
126 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
128 baro_speed = prev_state.baro_speed;
140 if (cur.new_gps && (state == Altos.ao_flight_pad || state == Altos.ao_flight_idle)) {
142 /* Track consecutive 'good' gps reports, waiting for 10 of them */
143 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
148 /* Average GPS data while on the pad */
149 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
151 /* filter pad position */
152 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
153 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
154 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
156 pad_lat = data.gps.lat;
157 pad_lon = data.gps.lon;
158 pad_alt = data.gps.alt;
164 gps_waiting = MIN_PAD_SAMPLES - npad;
168 gps_ready = gps_waiting == 0;
170 ascent = (Altos.ao_flight_boost <= state &&
171 state <= Altos.ao_flight_coast);
172 boost = (Altos.ao_flight_boost == state);
174 /* Only look at accelerometer data under boost */
175 if (boost && acceleration > max_acceleration)
176 max_acceleration = acceleration;
177 if (boost && speed > max_speed)
179 if (boost && baro_speed > max_baro_speed)
180 max_baro_speed = baro_speed;
182 if (height > max_height)
184 if (data.gps != null) {
185 if (gps == null || !gps.locked || data.gps.locked)
187 if (ngps > 0 && gps.locked) {
188 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
194 gps_height = gps.alt - pad_alt;
195 if (from_pad != null) {
196 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
197 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
204 public AltosState(AltosRecord cur) {
208 public AltosState (AltosRecord cur, AltosState prev) {