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 data stream
24 import altosui.AltosTelemetry;
25 import altosui.AltosGPS;
27 public class AltosState {
38 boolean ascent; /* going up? */
40 double ground_altitude;
51 double max_acceleration;
60 static final int MIN_PAD_SAMPLES = 10;
66 AltosGreatCircle from_pad;
71 double speak_altitude;
74 void init (AltosTelemetry cur, AltosState prev_state) {
77 double accel_counts_per_mss;
81 ground_altitude = AltosConvert.cc_barometer_to_altitude(data.ground_pres);
82 height = AltosConvert.cc_barometer_to_altitude(data.flight_pres) - ground_altitude;
84 report_time = System.currentTimeMillis();
86 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
87 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
88 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
89 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
90 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
91 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
92 battery = AltosConvert.cc_battery_to_voltage(data.batt);
96 if (prev_state != null) {
98 /* Preserve any existing gps data */
99 npad = prev_state.npad;
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;
118 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
120 baro_speed = prev_state.baro_speed;
128 if (state == AltosTelemetry.ao_flight_pad) {
129 if (data.gps != null && data.gps.gps_locked && data.gps.nsat >= 4) {
132 /* filter pad position */
133 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
134 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
135 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
137 pad_lat = data.gps.lat;
138 pad_lon = data.gps.lon;
139 pad_alt = data.gps.alt;
146 gps_waiting = MIN_PAD_SAMPLES - npad;
150 gps_ready = gps_waiting == 0;
152 ascent = (AltosTelemetry.ao_flight_boost <= state &&
153 state <= AltosTelemetry.ao_flight_coast);
155 /* Only look at accelerometer data on the way up */
156 if (ascent && acceleration > max_acceleration)
157 max_acceleration = acceleration;
158 if (ascent && speed > max_speed)
161 if (height > max_height)
163 if (data.gps != null) {
164 if (gps == null || !gps.gps_locked || data.gps.gps_locked)
166 if (npad > 0 && gps.gps_locked)
167 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
170 gps_height = gps.alt - pad_alt;
176 public AltosState(AltosTelemetry cur) {
180 public AltosState (AltosTelemetry cur, AltosState prev) {