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; 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.
21 import org.altusmetrum.altoslib_3.*;
23 public class AltosFlightStats {
26 double max_acceleration;
27 double[] state_speed = new double[Altos.ao_flight_invalid + 1];
28 double[] state_accel = new double[Altos.ao_flight_invalid + 1];
29 int[] state_count = new int[Altos.ao_flight_invalid + 1];
30 double[] state_start = new double[Altos.ao_flight_invalid + 1];
31 double[] state_end = new double[Altos.ao_flight_invalid + 1];
35 int hour, minute, second;
37 double pad_lat, pad_lon;
39 boolean has_other_adc;
45 double landed_time(AltosStateIterable states) {
46 AltosState state = null;
48 for (AltosState s : states) {
50 if (state.state == Altos.ao_flight_landed)
57 double landed_height = state.height();
63 double landed_time = -1000;
65 for (AltosState s : states) {
68 if (state.height() > landed_height + 10) {
71 if (above && state.height() < landed_height + 2) {
73 landed_time = state.time;
77 if (landed_time == -1000)
78 landed_time = state.time;
82 double boost_time(AltosStateIterable states) {
83 double boost_time = AltosLib.MISSING;
84 AltosState state = null;
86 for (AltosState s : states) {
88 if (state.acceleration() < 1)
89 boost_time = state.time;
90 if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
96 if (boost_time == AltosLib.MISSING)
97 boost_time = state.time;
102 public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
103 double boost_time = boost_time(states);
105 double landed_time = landed_time(states);
107 year = month = day = AltosLib.MISSING;
108 hour = minute = second = AltosLib.MISSING;
109 serial = flight = AltosLib.MISSING;
110 lat = lon = AltosLib.MISSING;
112 has_other_adc = false;
117 for (AltosState state : states) {
118 if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
119 serial = state.serial;
120 if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
121 flight = state.flight;
122 if (state.battery_voltage != AltosLib.MISSING)
123 has_other_adc = true;
124 if (state.rssi != AltosLib.MISSING)
126 end_time = state.time;
128 int state_id = state.state;
129 if (state.time >= boost_time && state_id < Altos.ao_flight_boost)
130 state_id = Altos.ao_flight_boost;
131 if (state.time >= landed_time && state_id < Altos.ao_flight_landed)
132 state_id = Altos.ao_flight_landed;
133 if (state.gps != null && state.gps.locked) {
134 year = state.gps.year;
135 month = state.gps.month;
137 hour = state.gps.hour;
138 minute = state.gps.minute;
139 second = state.gps.second;
141 if (0 <= state_id && state_id < Altos.ao_flight_invalid) {
142 double acceleration = state.acceleration();
143 double speed = state.speed();
144 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
145 state_accel[state_id] += acceleration;
146 state_speed[state_id] += speed;
147 state_count[state_id]++;
149 if (state_start[state_id] == 0.0)
150 state_start[state_id] = state.time;
151 if (state_end[state_id] < state.time)
152 state_end[state_id] = state.time;
153 max_height = state.max_height();
154 max_speed = state.max_speed();
155 max_acceleration = state.max_acceleration();
157 if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
158 if (state_id <= Altos.ao_flight_pad) {
159 pad_lat = state.gps.lat;
160 pad_lon = state.gps.lon;
166 if (state.imu != null)
168 if (state.mag != null)
170 if (state.orient() != AltosLib.MISSING)
173 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
174 if (state_count[s] > 0) {
175 state_speed[s] /= state_count[s];
176 state_accel[s] /= state_count[s];
178 if (state_start[s] == 0)
179 state_start[s] = end_time;
180 if (state_end[s] == 0)
181 state_end[s] = end_time;