2 * Copyright © 2011 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_14;
23 public class AltosFlightStats {
24 public double max_height;
25 public double max_gps_height;
26 public double max_speed;
27 public double max_acceleration;
28 public double[] state_speed = new double[AltosLib.ao_flight_invalid + 1];
29 public double[] state_enter_speed = new double[AltosLib.ao_flight_invalid + 1];
30 public double[] state_enter_height = new double[AltosLib.ao_flight_invalid + 1];
31 public double[] state_enter_gps_height = new double[AltosLib.ao_flight_invalid + 1];
32 public double[] state_accel = new double[AltosLib.ao_flight_invalid + 1];
33 public double[] state_time = new double[AltosLib.ao_flight_invalid + 1];
34 public String product;
35 public String firmware_version;
38 public int year, month, day;
39 public int hour, minute, second;
40 public double boost_time;
41 public double landed_time;
42 public double lat, lon;
43 public double pad_lat, pad_lon;
44 public boolean has_flight_data;
45 public boolean has_gps;
46 public boolean has_gps_sats;
47 public boolean has_gps_detail;
48 public boolean has_flight_adc;
49 public boolean has_battery;
50 public boolean has_rssi;
51 public boolean has_imu;
52 public boolean has_mag;
53 public boolean has_orient;
54 public int num_igniter;
56 double landed_time(AltosFlightSeries series) {
57 double landed_state_time = AltosLib.MISSING;
59 double prev_state_time = AltosLib.MISSING;
60 if (series.state_series != null) {
61 for (AltosTimeValue state : series.state_series) {
62 if (state.value == AltosLib.ao_flight_landed) {
63 landed_state_time = state.time;
66 prev_state_time = state.time;
71 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
72 landed_state_time = series.height_series.get(series.height_series.size()-1).time;
74 double landed_height = AltosLib.MISSING;
76 if (series.height_series != null) {
77 for (AltosTimeValue height : series.height_series) {
78 landed_height = height.value;
79 if (height.time >= landed_state_time)
84 if (landed_height == AltosLib.MISSING)
85 return AltosLib.MISSING;
89 double landed_time = AltosLib.MISSING;
91 if (series.height_series != null) {
92 for (AltosTimeValue height : series.height_series) {
93 if (height.value > landed_height + 10) {
96 if (above && Math.abs(height.value - landed_height) < 2) {
98 landed_time = height.time;
104 if (landed_time == AltosLib.MISSING || (prev_state_time != AltosLib.MISSING && landed_time < prev_state_time))
105 landed_time = landed_state_time;
109 double boost_time(AltosFlightSeries series) {
110 double boost_time = AltosLib.MISSING;
111 double boost_state_time = AltosLib.MISSING;
113 if (series.state_series != null) {
114 for (AltosTimeValue state : series.state_series) {
115 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
116 boost_state_time = state.time;
121 if (series.accel_series != null) {
122 for (AltosTimeValue accel : series.accel_series) {
124 boost_time = accel.time;
125 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
129 if (boost_time == AltosLib.MISSING)
130 boost_time = boost_state_time;
134 private void add_times(AltosFlightSeries series, int state, double start_time, double end_time) {
135 double delta_time = end_time - start_time;
136 if (0 <= state && state <= AltosLib.ao_flight_invalid && delta_time > 0) {
137 if (state_enter_speed[state] == AltosLib.MISSING)
138 state_enter_speed[state] = series.speed_series.value(start_time);
139 if (state_enter_height[state] == AltosLib.MISSING)
140 state_enter_height[state] = series.height_series.value(start_time);
141 if (state_enter_gps_height[state] == AltosLib.MISSING)
142 if (series.gps_height != null)
143 state_enter_gps_height[state] = series.gps_height.value(start_time);
144 speeds[state].value += series.speed_series.average(start_time, end_time) * delta_time;
145 speeds[state].time += delta_time;
146 accels[state].value += series.accel_series.average(start_time, end_time) * delta_time;
147 accels[state].time += delta_time;
148 state_time[state] += delta_time;
150 if (state == AltosLib.ao_flight_boost) {
151 AltosTimeValue tv_speed = series.speed_series.max(start_time, end_time);
152 if (tv_speed != null && (max_speed == AltosLib.MISSING || tv_speed.value > max_speed))
153 max_speed = tv_speed.value;
154 AltosTimeValue tv_accel = series.accel_series.max(start_time, end_time);
155 if (tv_accel != null && (max_acceleration == AltosLib.MISSING || tv_accel.value > max_acceleration))
156 max_acceleration = tv_accel.value;
161 AltosTimeValue[] speeds = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
162 AltosTimeValue[] accels = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
164 public AltosFlightStats(AltosFlightSeries series) {
165 AltosCalData cal_data = series.cal_data();
169 boost_time = boost_time(series);
170 landed_time = landed_time(series);
172 if (series.state_series != null){
173 boolean fixed_boost = false;
174 boolean fixed_landed = false;
175 for (AltosTimeValue state : series.state_series) {
176 if ((int) state.value == AltosLib.ao_flight_boost)
177 if (boost_time != AltosLib.MISSING && !fixed_boost) {
178 state.time = boost_time;
181 if ((int) state.value == AltosLib.ao_flight_landed)
182 if (landed_time != AltosLib.MISSING && !fixed_landed) {
183 state.time = landed_time;
189 year = month = day = AltosLib.MISSING;
190 hour = minute = second = AltosLib.MISSING;
191 serial = flight = AltosLib.MISSING;
192 lat = lon = AltosLib.MISSING;
193 has_flight_data = false;
195 has_gps_sats = false;
196 has_flight_adc = false;
203 for (int s = 0; s < AltosLib.ao_flight_invalid + 1; s++) {
204 state_speed[s] = AltosLib.MISSING;
205 state_enter_speed[s] = AltosLib.MISSING;
206 state_accel[s] = AltosLib.MISSING;
208 speeds[s] = new AltosTimeValue(0, 0);
209 accels[s] = new AltosTimeValue(0, 0);
212 max_speed = AltosLib.MISSING;
213 max_acceleration = AltosLib.MISSING;
215 if (series.state_series != null) {
216 AltosTimeValue prev = null;
217 for (AltosTimeValue state : series.state_series) {
219 add_times(series, (int) prev.value, prev.time, state.time);
223 AltosTimeValue last_accel = series.accel_series.last();
224 if (last_accel != null)
225 add_times(series, (int) prev.value, prev.time, last_accel.time);
229 for (int s = 0; s <= AltosLib.ao_flight_invalid; s++) {
230 if (speeds[s].time > 0)
231 state_speed[s] = speeds[s].value / speeds[s].time;
232 if (accels[s].time > 0)
233 state_accel[s] = accels[s].value / accels[s].time;
236 product = cal_data.product;
237 firmware_version = cal_data.firmware_version;
238 serial = cal_data.serial;
239 flight = cal_data.flight;
241 has_battery = series.battery_voltage_series != null;
242 has_flight_adc = series.main_voltage_series != null;
243 has_rssi = series.rssi_series != null;
244 has_flight_data = series.pressure_series != null;
246 AltosGPS gps = series.cal_data().gps_pad;
256 lat = pad_lat = gps.lat;
257 lon = pad_lon = gps.lon;
258 if (series.gps_series != null) {
259 for (AltosGPSTimeValue gtv : series.gps_series) {
261 if (gps.locked && gps.nsat >= 4) {
269 max_height = series.max_height;
270 if (max_height == AltosLib.MISSING && series.height_series != null)
271 max_height = series.height_series.max().value;
272 max_gps_height = AltosLib.MISSING;
273 if (series.gps_height != null) {
274 AltosTimeValue tv = series.gps_height.max();
276 max_gps_height = tv.value;