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
22 package org.altusmetrum.AltosLib;
24 public class AltosState {
25 public AltosRecord data;
29 public long report_time;
32 public double time_change;
36 public boolean landed;
37 public boolean ascent; /* going up? */
38 public boolean boost; /* under power */
40 public double ground_altitude;
43 public double acceleration;
44 public double battery;
45 public double temperature;
46 public double main_sense;
47 public double drogue_sense;
48 public double baro_speed;
50 public double max_height;
51 public double max_acceleration;
52 public double max_speed;
53 public double max_baro_speed;
60 public static final int MIN_PAD_SAMPLES = 10;
64 public int gps_waiting;
65 public boolean gps_ready;
67 public AltosGreatCircle from_pad;
68 public double elevation; /* from pad */
69 public double range; /* total distance */
71 public double gps_height;
73 public double pad_lat, pad_lon, pad_alt;
75 public int speak_tick;
76 public double speak_altitude;
78 public void init (AltosRecord cur, AltosState prev_state) {
84 ground_altitude = data.ground_altitude();
85 height = data.filtered_height();
87 report_time = System.currentTimeMillis();
89 acceleration = data.acceleration();
90 speed = data.accel_speed();
91 temperature = data.temperature();
92 drogue_sense = data.drogue_voltage();
93 main_sense = data.main_voltage();
94 battery = data.battery_voltage();
98 if (prev_state != null) {
100 /* Preserve any existing gps data */
101 npad = prev_state.npad;
102 ngps = prev_state.ngps;
103 gps = prev_state.gps;
104 pad_lat = prev_state.pad_lat;
105 pad_lon = prev_state.pad_lon;
106 pad_alt = prev_state.pad_alt;
107 max_height = prev_state.max_height;
108 max_acceleration = prev_state.max_acceleration;
109 max_speed = prev_state.max_speed;
110 max_baro_speed = prev_state.max_baro_speed;
111 imu = prev_state.imu;
112 mag = prev_state.mag;
114 /* make sure the clock is monotonic */
115 while (tick < prev_state.tick)
118 time_change = (tick - prev_state.tick) / 100.0;
120 /* compute barometric speed */
122 double height_change = height - prev_state.height;
123 if (data.speed != AltosRecord.MISSING)
124 baro_speed = data.speed;
127 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
129 baro_speed = prev_state.baro_speed;
141 if (cur.new_gps && (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_idle)) {
143 /* Track consecutive 'good' gps reports, waiting for 10 of them */
144 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
149 /* Average GPS data while on the pad */
150 if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
152 /* filter pad position */
153 pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
154 pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
155 pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
157 pad_lat = data.gps.lat;
158 pad_lon = data.gps.lon;
159 pad_alt = data.gps.alt;
164 pad_alt = ground_altitude;
166 gps_waiting = MIN_PAD_SAMPLES - npad;
170 gps_ready = gps_waiting == 0;
172 ascent = (AltosLib.ao_flight_boost <= state &&
173 state <= AltosLib.ao_flight_coast);
174 boost = (AltosLib.ao_flight_boost == state);
176 /* Only look at accelerometer data under boost */
177 if (boost && acceleration > max_acceleration && acceleration != AltosRecord.MISSING)
178 max_acceleration = acceleration;
179 if (boost && speed > max_speed && speed != AltosRecord.MISSING)
181 if (boost && baro_speed > max_baro_speed && baro_speed != AltosRecord.MISSING)
182 max_baro_speed = baro_speed;
184 if (height > max_height && height != AltosRecord.MISSING)
186 if (data.gps != null) {
187 if (gps == null || !gps.locked || data.gps.locked)
189 if (ngps > 0 && gps.locked) {
190 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
196 gps_height = gps.alt - pad_alt;
197 if (from_pad != null) {
198 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
199 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
206 public AltosState(AltosRecord cur) {
210 public AltosState (AltosRecord cur, AltosState prev) {