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_2.*;
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;
42 double landed_time(AltosStateIterable states) {
43 AltosState state = null;
45 for (AltosState s : states) {
47 if (state.state == Altos.ao_flight_landed)
54 double landed_height = state.height();
60 double landed_time = -1000;
62 for (AltosState s : states) {
65 if (state.height() > landed_height + 10) {
68 if (above && state.height() < landed_height + 2) {
70 landed_time = state.time;
74 if (landed_time == -1000)
75 landed_time = state.time;
79 double boost_time(AltosStateIterable states) {
80 double boost_time = AltosLib.MISSING;
81 AltosState state = null;
83 for (AltosState s : states) {
85 if (state.acceleration() < 1)
86 boost_time = state.time;
87 if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
93 if (boost_time == AltosLib.MISSING)
94 boost_time = state.time;
99 public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
100 double boost_time = boost_time(states);
102 double landed_time = landed_time(states);
104 year = month = day = AltosLib.MISSING;
105 hour = minute = second = AltosLib.MISSING;
106 serial = flight = AltosLib.MISSING;
107 lat = lon = AltosLib.MISSING;
109 has_other_adc = false;
111 for (AltosState state : states) {
112 if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
113 serial = state.serial;
114 if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
115 flight = state.flight;
116 if (state.battery_voltage != AltosLib.MISSING)
117 has_other_adc = true;
118 if (state.rssi != AltosLib.MISSING)
120 end_time = state.time;
122 int state_id = state.state;
123 if (state.time >= boost_time && state_id < Altos.ao_flight_boost)
124 state_id = Altos.ao_flight_boost;
125 if (state.time >= landed_time && state_id < Altos.ao_flight_landed)
126 state_id = Altos.ao_flight_landed;
127 if (state.gps != null && state.gps.locked) {
128 year = state.gps.year;
129 month = state.gps.month;
131 hour = state.gps.hour;
132 minute = state.gps.minute;
133 second = state.gps.second;
135 if (0 <= state_id && state_id < Altos.ao_flight_invalid) {
136 double acceleration = state.acceleration();
137 double speed = state.speed();
138 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
139 state_accel[state_id] += acceleration;
140 state_speed[state_id] += speed;
141 state_count[state_id]++;
143 if (state_start[state_id] == 0.0)
144 state_start[state_id] = state.time;
145 if (state_end[state_id] < state.time)
146 state_end[state_id] = state.time;
147 max_height = state.max_height();
148 max_speed = state.max_speed();
149 max_acceleration = state.max_acceleration();
151 if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
152 if (state_id <= Altos.ao_flight_pad) {
153 pad_lat = state.gps.lat;
154 pad_lon = state.gps.lon;
161 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
162 if (state_count[s] > 0) {
163 state_speed[s] /= state_count[s];
164 state_accel[s] /= state_count[s];
166 if (state_start[s] == 0)
167 state_start[s] = end_time;
168 if (state_end[s] == 0)
169 state_end[s] = end_time;