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_4;
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_gps;
39 public boolean has_other_adc;
40 public boolean has_rssi;
41 public boolean has_imu;
42 public boolean has_mag;
43 public boolean has_orient;
44 public int num_ignitor;
46 double landed_time(AltosStateIterable states) {
47 AltosState state = null;
49 for (AltosState s : states) {
51 if (state.state == AltosLib.ao_flight_landed)
58 double landed_height = state.height();
64 double landed_time = -1000;
66 for (AltosState s : states) {
69 if (state.height() > landed_height + 10) {
72 if (above && state.height() < landed_height + 2) {
74 landed_time = state.time;
78 if (landed_time == -1000)
79 landed_time = state.time;
83 double boost_time(AltosStateIterable states) {
84 double boost_time = AltosLib.MISSING;
85 AltosState state = null;
87 for (AltosState s : states) {
89 if (state.acceleration() < 1)
90 boost_time = state.time;
91 if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
97 if (boost_time == AltosLib.MISSING)
98 boost_time = state.time;
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;
113 has_other_adc = false;
118 for (AltosState state : states) {
119 if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
120 serial = state.serial;
121 if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
122 flight = state.flight;
123 if (state.battery_voltage != AltosLib.MISSING)
124 has_other_adc = true;
125 if (state.rssi != AltosLib.MISSING)
127 end_time = state.time;
129 int state_id = state.state;
130 if (state.time >= boost_time && state_id < AltosLib.ao_flight_boost)
131 state_id = AltosLib.ao_flight_boost;
132 if (state.time >= landed_time && state_id < AltosLib.ao_flight_landed)
133 state_id = AltosLib.ao_flight_landed;
134 if (state.gps != null && state.gps.locked) {
135 year = state.gps.year;
136 month = state.gps.month;
138 hour = state.gps.hour;
139 minute = state.gps.minute;
140 second = state.gps.second;
142 if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
143 double acceleration = state.acceleration();
144 double speed = state.speed();
145 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
146 state_accel[state_id] += acceleration;
147 state_speed[state_id] += speed;
148 state_count[state_id]++;
150 if (state_start[state_id] == 0.0)
151 state_start[state_id] = state.time;
152 if (state_end[state_id] < state.time)
153 state_end[state_id] = state.time;
154 max_height = state.max_height();
155 max_speed = state.max_speed();
156 max_acceleration = state.max_acceleration();
157 max_gps_height = state.max_gps_height();
159 if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
160 if (state_id <= AltosLib.ao_flight_pad) {
161 pad_lat = state.gps.lat;
162 pad_lon = state.gps.lon;
168 if (state.imu != null)
170 if (state.mag != null)
172 if (state.orient() != AltosLib.MISSING)
174 if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
175 num_ignitor = state.ignitor_voltage.length;
177 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
178 if (state_count[s] > 0) {
179 state_speed[s] /= state_count[s];
180 state_accel[s] /= state_count[s];
182 if (state_start[s] == 0)
183 state_start[s] = end_time;
184 if (state_end[s] == 0)
185 state_end[s] = end_time;