b3ee14acc516cdc8e8e75bf0fb503e6ff8577648
[fw/altos] / altosui / AltosFlightStats.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.io.*;
21 import org.altusmetrum.altoslib_2.*;
22
23 public class AltosFlightStats {
24         double          max_height;
25         double          max_speed;
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];
32         int             serial;
33         int             flight;
34         int             year, month, day;
35         int             hour, minute, second;
36         double          lat, lon;
37         double          pad_lat, pad_lon;
38         boolean         has_gps;
39         boolean         has_other_adc;
40         boolean         has_rssi;
41         boolean         has_imu;
42         boolean         has_mag;
43         boolean         has_orient;
44
45         double landed_time(AltosStateIterable states) {
46                 AltosState state = null;
47
48                 for (AltosState s : states) {
49                         state = s;
50                         if (state.state == Altos.ao_flight_landed)
51                                 break;
52                 }
53
54                 if (state == null)
55                         return 0;
56
57                 double  landed_height = state.height();
58
59                 state = null;
60
61                 boolean above = true;
62
63                 double  landed_time = -1000;
64
65                 for (AltosState s : states) {
66                         state = s;
67
68                         if (state.height() > landed_height + 10) {
69                                 above = true;
70                         } else {
71                                 if (above && state.height() < landed_height + 2) {
72                                         above = false;
73                                         landed_time = state.time;
74                                 }
75                         }
76                 }
77                 if (landed_time == -1000)
78                         landed_time = state.time;
79                 return landed_time;
80         }
81
82         double boost_time(AltosStateIterable states) {
83                 double boost_time = AltosLib.MISSING;
84                 AltosState      state = null;
85
86                 for (AltosState s : states) {
87                         state = s;
88                         if (state.acceleration() < 1)
89                                 boost_time = state.time;
90                         if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
91                                 break;
92                 }
93                 if (state == null)
94                         return 0;
95
96                 if (boost_time == AltosLib.MISSING)
97                         boost_time = state.time;
98                 return boost_time;
99         }
100
101
102         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
103                 double          boost_time = boost_time(states);
104                 double          end_time = 0;
105                 double          landed_time = landed_time(states);
106
107                 year = month = day = AltosLib.MISSING;
108                 hour = minute = second = AltosLib.MISSING;
109                 serial = flight = AltosLib.MISSING;
110                 lat = lon = AltosLib.MISSING;
111                 has_gps = false;
112                 has_other_adc = false;
113                 has_rssi = false;
114                 has_imu = false;
115                 has_mag = false;
116                 has_orient = 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)
125                                 has_rssi = true;
126                         end_time = state.time;
127
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;
136                                 day = state.gps.day;
137                                 hour = state.gps.hour;
138                                 minute = state.gps.minute;
139                                 second = state.gps.second;
140                         }
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]++;
148                                 }
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();
156                         }
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;
161                                 }
162                                 lat = state.gps.lat;
163                                 lon = state.gps.lon;
164                                 has_gps = true;
165                         }
166                         if (state.imu != null)
167                                 has_imu = true;
168                         if (state.mag != null)
169                                 has_mag = true;
170                         if (state.orient() != AltosLib.MISSING)
171                                 has_orient = true;
172                 }
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];
177                         }
178                         if (state_start[s] == 0)
179                                 state_start[s] = end_time;
180                         if (state_end[s] == 0)
181                                 state_end[s] = end_time;
182                 }
183         }
184 }