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_11;
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 int[] state_count = new int[AltosLib.ao_flight_invalid + 1];
31 public double[] state_start = new double[AltosLib.ao_flight_invalid + 1];
32 public double[] state_end = new double[AltosLib.ao_flight_invalid + 1];
35 public int year, month, day;
36 public int hour, minute, second;
37 public double lat, lon;
38 public double pad_lat, pad_lon;
39 public boolean has_flight_data;
40 public boolean has_gps;
41 public boolean has_gps_sats;
42 public boolean has_gps_detail;
43 public boolean has_flight_adc;
44 public boolean has_battery;
45 public boolean has_rssi;
46 public boolean has_imu;
47 public boolean has_mag;
48 public boolean has_orient;
49 public int num_ignitor;
51 double landed_time(AltosStateIterable states) {
52 AltosState state = null;
54 for (AltosState s : states) {
56 if (state.state() == AltosLib.ao_flight_landed)
61 return AltosLib.MISSING;
63 double landed_height = state.height();
69 double landed_time = AltosLib.MISSING;
71 for (AltosState s : states) {
74 if (state.height() > landed_height + 10) {
77 if (above && Math.abs(state.height() - landed_height) < 2) {
79 landed_time = state.time;
86 double boost_time(AltosStateIterable states) {
87 double boost_time = AltosLib.MISSING;
88 AltosState state = null;
90 for (AltosState s : states) {
92 if (state.acceleration() < 1)
93 boost_time = state.time;
94 if (state.state() >= AltosLib.ao_flight_boost && state.state() <= AltosLib.ao_flight_landed)
98 return AltosLib.MISSING;
104 public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
105 double boost_time = boost_time(states);
107 double landed_time = landed_time(states);
109 year = month = day = AltosLib.MISSING;
110 hour = minute = second = AltosLib.MISSING;
111 serial = flight = AltosLib.MISSING;
112 lat = lon = AltosLib.MISSING;
113 has_flight_data = false;
115 has_gps_sats = false;
116 has_flight_adc = false;
123 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
125 state_speed[s] = 0.0;
126 state_accel[s] = 0.0;
129 for (AltosState state : states) {
130 if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
131 serial = state.serial;
132 if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
133 flight = state.flight;
134 if (state.battery_voltage != AltosLib.MISSING)
136 if (state.main_voltage != AltosLib.MISSING)
137 has_flight_adc = true;
138 if (state.rssi != AltosLib.MISSING)
140 end_time = state.time;
142 if (state.pressure() != AltosLib.MISSING)
143 has_flight_data = true;
145 int state_id = state.state();
146 if (boost_time != AltosLib.MISSING && state.time >= boost_time && state_id < AltosLib.ao_flight_boost) {
147 state_id = AltosLib.ao_flight_boost;
149 if (landed_time != AltosLib.MISSING && state.time >= landed_time && state_id < AltosLib.ao_flight_landed) {
150 state_id = AltosLib.ao_flight_landed;
153 if (state.gps != null && state.gps.locked) {
154 year = state.gps.year;
155 month = state.gps.month;
157 hour = state.gps.hour;
158 minute = state.gps.minute;
159 second = state.gps.second;
161 max_height = state.max_height();
162 max_speed = state.max_speed();
163 max_acceleration = state.max_acceleration();
164 max_gps_height = state.max_gps_height();
166 if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
167 double acceleration = state.acceleration();
168 double speed = state.speed();
169 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
170 state_accel[state_id] += acceleration;
171 state_speed[state_id] += speed;
172 state_count[state_id]++;
174 if (state_start[state_id] == 0.0)
175 state_start[state_id] = state.time;
176 if (state_end[state_id] < state.time)
177 state_end[state_id] = state.time;
179 if (state.pad_lat != AltosLib.MISSING) {
180 pad_lat = state.pad_lat;
181 pad_lon = state.pad_lon;
183 if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
187 if (state.gps.cc_gps_sat != null)
189 if (state.gps.course != AltosLib.MISSING)
190 has_gps_detail = true;
192 if (state.imu != null)
194 if (state.mag != null)
196 if (state.orient() != AltosLib.MISSING)
198 if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
199 num_ignitor = state.ignitor_voltage.length;
201 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
202 if (state_count[s] > 0) {
203 state_speed[s] /= state_count[s];
204 state_accel[s] /= state_count[s];
206 state_speed[s] = AltosLib.MISSING;
207 state_accel[s] = AltosLib.MISSING;
209 if (state_start[s] == 0)
210 state_start[s] = end_time;
211 if (state_end[s] == 0)
212 state_end[s] = end_time;