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 {
36 boolean ascent; /* going up? */
38 double ground_altitude;
49 double max_acceleration;
58 static final int MIN_PAD_SAMPLES = 10;
65 AltosGreatCircle from_pad;
66 double elevation; /* from pad */
67 double range; /* total distance */
72 double speak_altitude;
75 void init (AltosRecord cur, AltosState prev_state) {
81 ground_altitude = data.ground_altitude();
82 height = data.filtered_height();
84 report_time = System.currentTimeMillis();
86 acceleration = data.acceleration();
87 speed = data.accel_speed();
88 temperature = data.temperature();
89 drogue_sense = data.drogue_voltage();
90 main_sense = data.main_voltage();
91 battery = data.battery_voltage();
95 if (prev_state != null) {
97 /* Preserve any existing gps data */
98 npad = prev_state.npad;
99 ngps = prev_state.ngps;
100 gps = prev_state.gps;
101 pad_lat = prev_state.pad_lat;
102 pad_lon = prev_state.pad_lon;
103 pad_alt = prev_state.pad_alt;
104 max_height = prev_state.max_height;
105 max_acceleration = prev_state.max_acceleration;
106 max_speed = prev_state.max_speed;
108 /* make sure the clock is monotonic */
109 while (tick < prev_state.tick)
112 time_change = (tick - prev_state.tick) / 100.0;
114 /* compute barometric speed */
116 double height_change = height - prev_state.height;
117 if (data.speed != AltosRecord.MISSING)
118 baro_speed = data.speed;
121 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
123 baro_speed = prev_state.baro_speed;
133 if (state == Altos.ao_flight_pad) {
135 /* Track consecutive 'good' gps reports, waiting for 10 of them */
136 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
141 /* Average GPS data while on the pad */
142 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
144 /* filter pad position */
145 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
146 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
147 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
149 pad_lat = data.gps.lat;
150 pad_lon = data.gps.lon;
151 pad_alt = data.gps.alt;
157 gps_waiting = MIN_PAD_SAMPLES - npad;
161 gps_ready = gps_waiting == 0;
163 ascent = (Altos.ao_flight_boost <= state &&
164 state <= Altos.ao_flight_coast);
166 /* Only look at accelerometer data on the way up */
167 if (ascent && acceleration > max_acceleration)
168 max_acceleration = acceleration;
169 if (ascent && speed > max_speed)
172 if (height > max_height)
174 if (data.gps != null) {
175 if (gps == null || !gps.locked || data.gps.locked)
177 if (ngps > 0 && gps.locked) {
178 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
184 gps_height = gps.alt - pad_alt;
185 if (from_pad != null) {
186 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
187 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
194 public AltosState(AltosRecord cur) {
198 public AltosState (AltosRecord cur, AltosState prev) {