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 public class AltosState {
37 boolean ascent; /* going up? */
38 boolean boost; /* under power */
40 double ground_altitude;
51 double max_acceleration;
53 double max_baro_speed;
61 static final int MIN_PAD_SAMPLES = 10;
68 AltosGreatCircle from_pad;
69 double elevation; /* from pad */
70 double range; /* total distance */
75 double speak_altitude;
77 void init (AltosRecord cur, AltosState prev_state) {
83 ground_altitude = data.ground_altitude();
84 height = data.filtered_height();
86 report_time = System.currentTimeMillis();
88 acceleration = data.acceleration();
89 speed = data.accel_speed();
90 temperature = data.temperature();
91 drogue_sense = data.drogue_voltage();
92 main_sense = data.main_voltage();
93 battery = data.battery_voltage();
97 if (prev_state != null) {
99 /* Preserve any existing gps data */
100 npad = prev_state.npad;
101 ngps = prev_state.ngps;
102 gps = prev_state.gps;
103 pad_lat = prev_state.pad_lat;
104 pad_lon = prev_state.pad_lon;
105 pad_alt = prev_state.pad_alt;
106 max_height = prev_state.max_height;
107 max_acceleration = prev_state.max_acceleration;
108 max_speed = prev_state.max_speed;
109 max_baro_speed = prev_state.max_baro_speed;
111 /* make sure the clock is monotonic */
112 while (tick < prev_state.tick)
115 time_change = (tick - prev_state.tick) / 100.0;
117 /* compute barometric speed */
119 double height_change = height - prev_state.height;
120 if (data.speed != AltosRecord.MISSING)
121 baro_speed = data.speed;
124 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
126 baro_speed = prev_state.baro_speed;
138 if (cur.new_gps && (state == Altos.ao_flight_pad || state == Altos.ao_flight_idle)) {
140 /* Track consecutive 'good' gps reports, waiting for 10 of them */
141 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
146 /* Average GPS data while on the pad */
147 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
149 /* filter pad position */
150 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
151 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
152 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
154 pad_lat = data.gps.lat;
155 pad_lon = data.gps.lon;
156 pad_alt = data.gps.alt;
162 gps_waiting = MIN_PAD_SAMPLES - npad;
166 gps_ready = gps_waiting == 0;
168 ascent = (Altos.ao_flight_boost <= state &&
169 state <= Altos.ao_flight_coast);
170 boost = (Altos.ao_flight_boost == state);
172 /* Only look at accelerometer data under boost */
173 if (boost && acceleration > max_acceleration)
174 max_acceleration = acceleration;
175 if (boost && speed > max_speed)
177 if (boost && baro_speed > max_baro_speed)
178 max_baro_speed = baro_speed;
180 if (height > max_height)
182 if (data.gps != null) {
183 if (gps == null || !gps.locked || data.gps.locked)
185 if (ngps > 0 && gps.locked) {
186 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
192 gps_height = gps.alt - pad_alt;
193 if (from_pad != null) {
194 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
195 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
202 public AltosState(AltosRecord cur) {
206 public AltosState (AltosRecord cur, AltosState prev) {