altosui: Add ignitor tab for TeleMega extra ignitors
[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_3.*;
22
23 public class AltosFlightStats {
24         double          max_height;
25         double          max_gps_height;
26         double          max_speed;
27         double          max_acceleration;
28         double[]        state_speed = new double[Altos.ao_flight_invalid + 1];
29         double[]        state_accel = new double[Altos.ao_flight_invalid + 1];
30         int[]           state_count = new int[Altos.ao_flight_invalid + 1];
31         double[]        state_start = new double[Altos.ao_flight_invalid + 1];
32         double[]        state_end = new double[Altos.ao_flight_invalid + 1];
33         int             serial;
34         int             flight;
35         int             year, month, day;
36         int             hour, minute, second;
37         double          lat, lon;
38         double          pad_lat, pad_lon;
39         boolean         has_gps;
40         boolean         has_other_adc;
41         boolean         has_rssi;
42         boolean         has_imu;
43         boolean         has_mag;
44         boolean         has_orient;
45
46         double landed_time(AltosStateIterable states) {
47                 AltosState state = null;
48
49                 for (AltosState s : states) {
50                         state = s;
51                         if (state.state == Altos.ao_flight_landed)
52                                 break;
53                 }
54
55                 if (state == null)
56                         return 0;
57
58                 double  landed_height = state.height();
59
60                 state = null;
61
62                 boolean above = true;
63
64                 double  landed_time = -1000;
65
66                 for (AltosState s : states) {
67                         state = s;
68
69                         if (state.height() > landed_height + 10) {
70                                 above = true;
71                         } else {
72                                 if (above && state.height() < landed_height + 2) {
73                                         above = false;
74                                         landed_time = state.time;
75                                 }
76                         }
77                 }
78                 if (landed_time == -1000)
79                         landed_time = state.time;
80                 return landed_time;
81         }
82
83         double boost_time(AltosStateIterable states) {
84                 double boost_time = AltosLib.MISSING;
85                 AltosState      state = null;
86
87                 for (AltosState s : states) {
88                         state = s;
89                         if (state.acceleration() < 1)
90                                 boost_time = state.time;
91                         if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
92                                 break;
93                 }
94                 if (state == null)
95                         return 0;
96
97                 if (boost_time == AltosLib.MISSING)
98                         boost_time = state.time;
99                 return boost_time;
100         }
101
102
103         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
104                 double          boost_time = boost_time(states);
105                 double          end_time = 0;
106                 double          landed_time = landed_time(states);
107
108                 year = month = day = AltosLib.MISSING;
109                 hour = minute = second = AltosLib.MISSING;
110                 serial = flight = AltosLib.MISSING;
111                 lat = lon = AltosLib.MISSING;
112                 has_gps = false;
113                 has_other_adc = false;
114                 has_rssi = false;
115                 has_imu = false;
116                 has_mag = false;
117                 has_orient = 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)
126                                 has_rssi = true;
127                         end_time = state.time;
128
129                         int state_id = state.state;
130                         if (state.time >= boost_time && state_id < Altos.ao_flight_boost)
131                                 state_id = Altos.ao_flight_boost;
132                         if (state.time >= landed_time && state_id < Altos.ao_flight_landed)
133                                 state_id = Altos.ao_flight_landed;
134                         if (state.gps != null && state.gps.locked) {
135                                 year = state.gps.year;
136                                 month = state.gps.month;
137                                 day = state.gps.day;
138                                 hour = state.gps.hour;
139                                 minute = state.gps.minute;
140                                 second = state.gps.second;
141                         }
142                         if (0 <= state_id && state_id < Altos.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]++;
149                                 }
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();
158                         }
159                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
160                                 if (state_id <= Altos.ao_flight_pad) {
161                                         pad_lat = state.gps.lat;
162                                         pad_lon = state.gps.lon;
163                                 }
164                                 lat = state.gps.lat;
165                                 lon = state.gps.lon;
166                                 has_gps = true;
167                         }
168                         if (state.imu != null)
169                                 has_imu = true;
170                         if (state.mag != null)
171                                 has_mag = true;
172                         if (state.orient() != AltosLib.MISSING)
173                                 has_orient = true;
174                 }
175                 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
176                         if (state_count[s] > 0) {
177                                 state_speed[s] /= state_count[s];
178                                 state_accel[s] /= state_count[s];
179                         }
180                         if (state_start[s] == 0)
181                                 state_start[s] = end_time;
182                         if (state_end[s] == 0)
183                                 state_end[s] = end_time;
184                 }
185         }
186 }