micropeak: Compile for java 6
[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
42         double landed_time(AltosStateIterable states) {
43                 AltosState state = null;
44
45                 for (AltosState s : states) {
46                         state = s;
47                         if (state.state == Altos.ao_flight_landed)
48                                 break;
49                 }
50
51                 if (state == null)
52                         return 0;
53
54                 double  landed_height = state.height();
55
56                 state = null;
57
58                 boolean above = true;
59
60                 double  landed_time = -1000;
61
62                 for (AltosState s : states) {
63                         state = s;
64
65                         if (state.height() > landed_height + 10) {
66                                 above = true;
67                         } else {
68                                 if (above && state.height() < landed_height + 2) {
69                                         above = false;
70                                         landed_time = state.time;
71                                 }
72                         }
73                 }
74                 if (landed_time == -1000)
75                         landed_time = state.time;
76                 return landed_time;
77         }
78
79         double boost_time(AltosStateIterable states) {
80                 double boost_time = AltosLib.MISSING;
81                 AltosState      state = null;
82
83                 for (AltosState s : states) {
84                         state = s;
85                         if (state.acceleration() < 1)
86                                 boost_time = state.time;
87                         if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
88                                 break;
89                 }
90                 if (state == null)
91                         return 0;
92
93                 if (boost_time == AltosLib.MISSING)
94                         boost_time = state.time;
95                 return boost_time;
96         }
97
98
99         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
100                 double          boost_time = boost_time(states);
101                 double          end_time = 0;
102                 double          landed_time = landed_time(states);
103
104                 year = month = day = AltosLib.MISSING;
105                 hour = minute = second = AltosLib.MISSING;
106                 serial = flight = AltosLib.MISSING;
107                 lat = lon = AltosLib.MISSING;
108                 has_gps = false;
109                 has_other_adc = false;
110                 has_rssi = 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)
119                                 has_rssi = true;
120                         end_time = state.time;
121
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;
130                                 day = state.gps.day;
131                                 hour = state.gps.hour;
132                                 minute = state.gps.minute;
133                                 second = state.gps.second;
134                         }
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]++;
142                                 }
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();
150                         }
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;
155                                 }
156                                 lat = state.gps.lat;
157                                 lon = state.gps.lon;
158                                 has_gps = true;
159                         }
160                 }
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];
165                         }
166                         if (state_start[s] == 0)
167                                 state_start[s] = end_time;
168                         if (state_end[s] == 0)
169                                 state_end[s] = end_time;
170                 }
171         }
172 }