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;
62 AltosGreatCircle from_pad;
67 double speak_altitude;
70 void init (AltosTelemetry cur, AltosState prev_state) {
73 double accel_counts_per_mss;
77 ground_altitude = AltosConvert.cc_barometer_to_altitude(data.ground_pres);
78 height = AltosConvert.cc_barometer_to_altitude(data.flight_pres) - ground_altitude;
80 report_time = System.currentTimeMillis();
82 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
83 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
84 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
85 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
86 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
87 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
88 battery = AltosConvert.cc_battery_to_voltage(data.batt);
92 if (prev_state != null) {
94 /* Preserve any existing gps data */
95 npad = prev_state.npad;
97 pad_lat = prev_state.pad_lat;
98 pad_lon = prev_state.pad_lon;
99 pad_alt = prev_state.pad_alt;
100 max_height = prev_state.max_height;
101 max_acceleration = prev_state.max_acceleration;
102 max_speed = prev_state.max_speed;
104 /* make sure the clock is monotonic */
105 while (tick < prev_state.tick)
108 time_change = (tick - prev_state.tick) / 100.0;
110 /* compute barometric speed */
112 double height_change = height - prev_state.height;
114 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
116 baro_speed = prev_state.baro_speed;
124 if (state == AltosTelemetry.ao_flight_pad) {
125 if (data.gps != null && data.gps.gps_locked && data.gps.nsat >= 4) {
128 /* filter pad position */
129 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
130 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
131 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
133 pad_lat = data.gps.lat;
134 pad_lon = data.gps.lon;
135 pad_alt = data.gps.alt;
139 ascent = (AltosTelemetry.ao_flight_boost <= state &&
140 state <= AltosTelemetry.ao_flight_coast);
142 /* Only look at accelerometer data on the way up */
143 if (ascent && acceleration > max_acceleration)
144 max_acceleration = acceleration;
145 if (ascent && speed > max_speed)
148 if (height > max_height)
150 if (data.gps != null) {
151 if (gps == null || !gps.gps_locked || data.gps.gps_locked)
153 if (npad > 0 && gps.gps_locked)
154 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
157 gps_height = gps.alt - pad_alt;
163 public AltosState(AltosTelemetry cur) {
167 public AltosState (AltosTelemetry cur, AltosState prev) {