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_enter_speed = new double[AltosLib.ao_flight_invalid + 1];
30 public double[] state_accel = new double[AltosLib.ao_flight_invalid + 1];
31 public double[] state_time = new double[AltosLib.ao_flight_invalid + 1];
32 public String product;
33 public String firmware_version;
36 public int year, month, day;
37 public int hour, minute, second;
38 public double boost_time;
39 public double landed_time;
40 public double lat, lon;
41 public double pad_lat, pad_lon;
42 public boolean has_flight_data;
43 public boolean has_gps;
44 public boolean has_gps_sats;
45 public boolean has_gps_detail;
46 public boolean has_flight_adc;
47 public boolean has_battery;
48 public boolean has_rssi;
49 public boolean has_imu;
50 public boolean has_mag;
51 public boolean has_orient;
52 public int num_igniter;
54 double landed_time(AltosFlightSeries series) {
55 double landed_state_time = AltosLib.MISSING;
57 double prev_state_time = AltosLib.MISSING;
58 if (series.state_series != null) {
59 for (AltosTimeValue state : series.state_series) {
60 if (state.value == AltosLib.ao_flight_landed) {
61 landed_state_time = state.time;
64 prev_state_time = state.time;
69 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
70 landed_state_time = series.height_series.get(series.height_series.size()-1).time;
72 double landed_height = AltosLib.MISSING;
74 if (series.height_series != null) {
75 for (AltosTimeValue height : series.height_series) {
76 landed_height = height.value;
77 if (height.time >= landed_state_time)
82 if (landed_height == AltosLib.MISSING)
83 return AltosLib.MISSING;
87 double landed_time = AltosLib.MISSING;
89 if (series.height_series != null) {
90 for (AltosTimeValue height : series.height_series) {
91 if (height.value > landed_height + 10) {
94 if (above && Math.abs(height.value - landed_height) < 2) {
96 landed_time = height.time;
102 if (landed_time == AltosLib.MISSING || (prev_state_time != AltosLib.MISSING && landed_time < prev_state_time))
103 landed_time = landed_state_time;
107 double boost_time(AltosFlightSeries series) {
108 double boost_time = AltosLib.MISSING;
109 double boost_state_time = AltosLib.MISSING;
111 if (series.state_series != null) {
112 for (AltosTimeValue state : series.state_series) {
113 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
114 boost_state_time = state.time;
119 if (series.accel_series != null) {
120 for (AltosTimeValue accel : series.accel_series) {
122 boost_time = accel.time;
123 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
127 if (boost_time == AltosLib.MISSING)
128 boost_time = boost_state_time;
132 private void add_times(AltosFlightSeries series, int state, double start_time, double end_time) {
133 double delta_time = end_time - start_time;
134 if (0 <= state && state <= AltosLib.ao_flight_invalid && delta_time > 0) {
135 if (state_enter_speed[state] == AltosLib.MISSING)
136 state_enter_speed[state] = series.speed_series.value(start_time);
137 speeds[state].value += series.speed_series.average(start_time, end_time) * delta_time;
138 speeds[state].time += delta_time;
139 accels[state].value += series.accel_series.average(start_time, end_time) * delta_time;
140 accels[state].time += delta_time;
141 state_time[state] += delta_time;
143 if (state == AltosLib.ao_flight_boost) {
144 AltosTimeValue tv_speed = series.speed_series.max(start_time, end_time);
145 if (tv_speed != null && (max_speed == AltosLib.MISSING || tv_speed.value > max_speed))
146 max_speed = tv_speed.value;
147 AltosTimeValue tv_accel = series.accel_series.max(start_time, end_time);
148 if (tv_accel != null && (max_acceleration == AltosLib.MISSING || tv_accel.value > max_acceleration))
149 max_acceleration = tv_accel.value;
154 AltosTimeValue[] speeds = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
155 AltosTimeValue[] accels = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
157 public AltosFlightStats(AltosFlightSeries series) {
158 AltosCalData cal_data = series.cal_data();
162 boost_time = boost_time(series);
163 landed_time = landed_time(series);
165 if (series.state_series != null){
166 boolean fixed_boost = false;
167 boolean fixed_landed = false;
168 for (AltosTimeValue state : series.state_series) {
169 if ((int) state.value == AltosLib.ao_flight_boost)
170 if (boost_time != AltosLib.MISSING && !fixed_boost) {
171 state.time = boost_time;
174 if ((int) state.value == AltosLib.ao_flight_landed)
175 if (landed_time != AltosLib.MISSING && !fixed_landed) {
176 state.time = landed_time;
182 year = month = day = AltosLib.MISSING;
183 hour = minute = second = AltosLib.MISSING;
184 serial = flight = AltosLib.MISSING;
185 lat = lon = AltosLib.MISSING;
186 has_flight_data = false;
188 has_gps_sats = false;
189 has_flight_adc = false;
196 for (int s = 0; s < AltosLib.ao_flight_invalid + 1; s++) {
197 state_speed[s] = AltosLib.MISSING;
198 state_enter_speed[s] = AltosLib.MISSING;
199 state_accel[s] = AltosLib.MISSING;
201 speeds[s] = new AltosTimeValue(0, 0);
202 accels[s] = new AltosTimeValue(0, 0);
205 max_speed = AltosLib.MISSING;
206 max_acceleration = AltosLib.MISSING;
208 if (series.state_series != null) {
209 AltosTimeValue prev = null;
210 for (AltosTimeValue state : series.state_series) {
212 add_times(series, (int) prev.value, prev.time, state.time);
216 add_times(series, (int) prev.value, prev.time, series.accel_series.last().time);
219 for (int s = 0; s <= AltosLib.ao_flight_invalid; s++) {
220 if (speeds[s].time > 0)
221 state_speed[s] = speeds[s].value / speeds[s].time;
222 if (accels[s].time > 0)
223 state_accel[s] = accels[s].value / accels[s].time;
226 product = cal_data.product;
227 firmware_version = cal_data.firmware_version;
228 serial = cal_data.serial;
229 flight = cal_data.flight;
231 has_battery = series.battery_voltage_series != null;
232 has_flight_adc = series.main_voltage_series != null;
233 has_rssi = series.rssi_series != null;
234 has_flight_data = series.pressure_series != null;
236 AltosGPS gps = series.cal_data().gps_pad;
246 lat = pad_lat = gps.lat;
247 lon = pad_lon = gps.lon;
248 for (AltosGPSTimeValue gtv : series.gps_series) {
250 if (gps.locked && gps.nsat >= 4) {
258 max_height = AltosLib.MISSING;
259 if (series.height_series != null)
260 max_height = series.height_series.max().value;
261 max_gps_height = AltosLib.MISSING;
262 if (series.gps_height != null) {
263 AltosTimeValue tv = series.gps_height.max();
265 max_gps_height = tv.value;