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.
18 package org.altusmetrum.altoslib_11;
22 public class AltosFlightStats {
23 public double max_height;
24 public double max_gps_height;
25 public double max_speed;
26 public double max_acceleration;
27 public double[] state_speed = new double[AltosLib.ao_flight_invalid + 1];
28 public double[] state_accel = new double[AltosLib.ao_flight_invalid + 1];
29 public int[] state_count = new int[AltosLib.ao_flight_invalid + 1];
30 public double[] state_start = new double[AltosLib.ao_flight_invalid + 1];
31 public double[] state_end = new double[AltosLib.ao_flight_invalid + 1];
34 public int year, month, day;
35 public int hour, minute, second;
36 public double lat, lon;
37 public double pad_lat, pad_lon;
38 public boolean has_flight_data;
39 public boolean has_gps;
40 public boolean has_gps_sats;
41 public boolean has_gps_detail;
42 public boolean has_flight_adc;
43 public boolean has_battery;
44 public boolean has_rssi;
45 public boolean has_imu;
46 public boolean has_mag;
47 public boolean has_orient;
48 public int num_ignitor;
50 double landed_time(AltosStateIterable states) {
51 AltosState state = null;
53 for (AltosState s : states) {
55 if (state.state() == AltosLib.ao_flight_landed)
60 return AltosLib.MISSING;
62 double landed_height = state.height();
68 double landed_time = AltosLib.MISSING;
70 for (AltosState s : states) {
73 if (state.height() > landed_height + 10) {
76 if (above && Math.abs(state.height() - landed_height) < 2) {
78 landed_time = state.time;
85 double boost_time(AltosStateIterable states) {
86 double boost_time = AltosLib.MISSING;
87 AltosState state = null;
89 for (AltosState s : states) {
91 if (state.acceleration() < 1)
92 boost_time = state.time;
93 if (state.state() >= AltosLib.ao_flight_boost && state.state() <= AltosLib.ao_flight_landed)
97 return AltosLib.MISSING;
103 public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
104 double boost_time = boost_time(states);
106 double landed_time = landed_time(states);
108 year = month = day = AltosLib.MISSING;
109 hour = minute = second = AltosLib.MISSING;
110 serial = flight = AltosLib.MISSING;
111 lat = lon = AltosLib.MISSING;
112 has_flight_data = false;
114 has_gps_sats = false;
115 has_flight_adc = false;
122 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
124 state_speed[s] = 0.0;
125 state_accel[s] = 0.0;
128 for (AltosState state : states) {
129 if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
130 serial = state.serial;
131 if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
132 flight = state.flight;
133 if (state.battery_voltage != AltosLib.MISSING)
135 if (state.main_voltage != AltosLib.MISSING)
136 has_flight_adc = true;
137 if (state.rssi != AltosLib.MISSING)
139 end_time = state.time;
141 if (state.pressure() != AltosLib.MISSING)
142 has_flight_data = true;
144 int state_id = state.state();
145 if (boost_time != AltosLib.MISSING && state.time >= boost_time && state_id < AltosLib.ao_flight_boost) {
146 state_id = AltosLib.ao_flight_boost;
148 if (landed_time != AltosLib.MISSING && state.time >= landed_time && state_id < AltosLib.ao_flight_landed) {
149 state_id = AltosLib.ao_flight_landed;
152 if (state.gps != null && state.gps.locked) {
153 year = state.gps.year;
154 month = state.gps.month;
156 hour = state.gps.hour;
157 minute = state.gps.minute;
158 second = state.gps.second;
160 max_height = state.max_height();
161 max_speed = state.max_speed();
162 max_acceleration = state.max_acceleration();
163 max_gps_height = state.max_gps_height();
165 if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
166 double acceleration = state.acceleration();
167 double speed = state.speed();
168 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
169 state_accel[state_id] += acceleration;
170 state_speed[state_id] += speed;
171 state_count[state_id]++;
173 if (state_start[state_id] == 0.0)
174 state_start[state_id] = state.time;
175 if (state_end[state_id] < state.time)
176 state_end[state_id] = state.time;
178 if (state.pad_lat != AltosLib.MISSING) {
179 pad_lat = state.pad_lat;
180 pad_lon = state.pad_lon;
182 if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
186 if (state.gps.cc_gps_sat != null)
188 if (state.gps.course != AltosLib.MISSING)
189 has_gps_detail = true;
191 if (state.imu != null)
193 if (state.mag != null)
195 if (state.orient() != AltosLib.MISSING)
197 if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
198 num_ignitor = state.ignitor_voltage.length;
200 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
201 if (state_count[s] > 0) {
202 state_speed[s] /= state_count[s];
203 state_accel[s] /= state_count[s];
205 state_speed[s] = AltosLib.MISSING;
206 state_accel[s] = AltosLib.MISSING;
208 if (state_start[s] == 0)
209 state_start[s] = end_time;
210 if (state_end[s] == 0)
211 state_end[s] = end_time;