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_12;
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_accel = new double[AltosLib.ao_flight_invalid + 1];
30 public double[] state_time = new double[AltosLib.ao_flight_invalid + 1];
31 public String product;
32 public String firmware_version;
35 public int year, month, day;
36 public int hour, minute, second;
37 public double boost_time;
38 public double landed_time;
39 public double lat, lon;
40 public double pad_lat, pad_lon;
41 public boolean has_flight_data;
42 public boolean has_gps;
43 public boolean has_gps_sats;
44 public boolean has_gps_detail;
45 public boolean has_flight_adc;
46 public boolean has_battery;
47 public boolean has_rssi;
48 public boolean has_imu;
49 public boolean has_mag;
50 public boolean has_orient;
51 public int num_igniter;
53 double landed_time(AltosFlightSeries series) {
54 double landed_state_time = AltosLib.MISSING;
56 if (series.state_series != null) {
57 for (AltosTimeValue state : series.state_series) {
58 if (state.value == AltosLib.ao_flight_landed) {
59 landed_state_time = state.time;
65 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
66 landed_state_time = series.height_series.get(series.height_series.size()-1).time;
68 double landed_height = AltosLib.MISSING;
70 if (series.height_series != null) {
71 for (AltosTimeValue height : series.height_series) {
72 landed_height = height.value;
73 if (height.time >= landed_state_time)
78 if (landed_height == AltosLib.MISSING)
79 return AltosLib.MISSING;
83 double landed_time = AltosLib.MISSING;
85 if (series.height_series != null) {
86 for (AltosTimeValue height : series.height_series) {
87 if (height.value > landed_height + 10) {
90 if (above && Math.abs(height.value - landed_height) < 2) {
92 landed_time = height.time;
98 if (landed_time == AltosLib.MISSING)
99 landed_time = landed_state_time;
103 double boost_time(AltosFlightSeries series) {
104 double boost_time = AltosLib.MISSING;
105 double boost_state_time = AltosLib.MISSING;
107 if (series.state_series != null) {
108 for (AltosTimeValue state : series.state_series) {
109 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
110 boost_state_time = state.time;
115 if (series.accel_series != null) {
116 for (AltosTimeValue accel : series.accel_series) {
118 boost_time = accel.time;
119 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
123 if (boost_time == AltosLib.MISSING)
124 boost_time = boost_state_time;
128 private void add_times(AltosFlightSeries series, int state, double start_time, double end_time) {
129 double delta_time = end_time - start_time;
130 if (0 <= state && state <= AltosLib.ao_flight_invalid && delta_time > 0) {
131 speeds[state].value += series.speed_series.average(start_time, end_time) * delta_time;
132 speeds[state].time += delta_time;
133 accels[state].value += series.accel_series.average(start_time, end_time) * delta_time;
134 accels[state].time += delta_time;
135 state_time[state] += delta_time;
137 if (state == AltosLib.ao_flight_boost) {
138 AltosTimeValue tv_speed = series.speed_series.max(start_time, end_time);
139 if (tv_speed != null && (max_speed == AltosLib.MISSING || tv_speed.value > max_speed))
140 max_speed = tv_speed.value;
141 AltosTimeValue tv_accel = series.accel_series.max(start_time, end_time);
142 if (tv_accel != null && (max_acceleration == AltosLib.MISSING || tv_accel.value > max_acceleration))
143 max_acceleration = tv_accel.value;
148 AltosTimeValue[] speeds = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
149 AltosTimeValue[] accels = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
151 public AltosFlightStats(AltosFlightSeries series) {
152 AltosCalData cal_data = series.cal_data();
156 boost_time = boost_time(series);
157 landed_time = landed_time(series);
159 if (series.state_series != null){
160 boolean fixed_boost = false;
161 boolean fixed_landed = false;
162 for (AltosTimeValue state : series.state_series) {
163 if ((int) state.value == AltosLib.ao_flight_boost)
164 if (boost_time != AltosLib.MISSING && !fixed_boost) {
165 state.time = boost_time;
168 if ((int) state.value == AltosLib.ao_flight_landed)
169 if (landed_time != AltosLib.MISSING && !fixed_landed) {
170 state.time = landed_time;
176 year = month = day = AltosLib.MISSING;
177 hour = minute = second = AltosLib.MISSING;
178 serial = flight = AltosLib.MISSING;
179 lat = lon = AltosLib.MISSING;
180 has_flight_data = false;
182 has_gps_sats = false;
183 has_flight_adc = false;
190 for (int s = 0; s < AltosLib.ao_flight_invalid + 1; s++) {
191 state_speed[s] = AltosLib.MISSING;
192 state_accel[s] = AltosLib.MISSING;
194 speeds[s] = new AltosTimeValue(0, 0);
195 accels[s] = new AltosTimeValue(0, 0);
198 max_speed = AltosLib.MISSING;
199 max_acceleration = AltosLib.MISSING;
201 if (series.state_series != null) {
202 AltosTimeValue prev = null;
203 for (AltosTimeValue state : series.state_series) {
205 add_times(series, (int) prev.value, prev.time, state.time);
209 add_times(series, (int) prev.value, prev.time, series.accel_series.last().time);
212 for (int s = 0; s <= AltosLib.ao_flight_invalid; s++) {
213 if (speeds[s].time > 0)
214 state_speed[s] = speeds[s].value / speeds[s].time;
215 if (accels[s].time > 0)
216 state_accel[s] = accels[s].value / accels[s].time;
219 product = cal_data.product;
220 firmware_version = cal_data.firmware_version;
221 serial = cal_data.serial;
222 flight = cal_data.flight;
224 has_battery = series.battery_voltage_series != null;
225 has_flight_adc = series.main_voltage_series != null;
226 has_rssi = series.rssi_series != null;
227 has_flight_data = series.pressure_series != null;
229 AltosGPS gps = series.cal_data().gps_pad;
239 lat = pad_lat = gps.lat;
240 lon = pad_lon = gps.lon;
241 for (AltosGPSTimeValue gtv : series.gps_series) {
243 if (gps.locked && gps.nsat >= 4) {
251 max_height = AltosLib.MISSING;
252 if (series.height_series != null)
253 max_height = series.height_series.max().value;
254 max_gps_height = AltosLib.MISSING;
255 if (series.gps_height != null) {
256 AltosTimeValue tv = series.gps_height.max();
258 max_gps_height = tv.value;