7c8ed85e589d7f89623c116b7f32d2830d0d070e
[fw/altos] / altoslib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_13;
20
21 import java.io.*;
22
23 public class AltosFlightStats {
24         public double           max_height;
25         public double           max_gps_height;
26         public double           max_speed;
27         public double           max_acceleration;
28         public double[]         state_speed = new double[AltosLib.ao_flight_invalid + 1];
29         public double[]         state_enter_speed = new double[AltosLib.ao_flight_invalid + 1];
30         public double[]         state_enter_height = new double[AltosLib.ao_flight_invalid + 1];
31         public double[]         state_enter_gps_height = new double[AltosLib.ao_flight_invalid + 1];
32         public double[]         state_accel = new double[AltosLib.ao_flight_invalid + 1];
33         public double[]         state_time = new double[AltosLib.ao_flight_invalid + 1];
34         public String           product;
35         public String           firmware_version;
36         public int              serial;
37         public int              flight;
38         public int              year, month, day;
39         public int              hour, minute, second;
40         public double           boost_time;
41         public double           landed_time;
42         public double           lat, lon;
43         public double           pad_lat, pad_lon;
44         public boolean          has_flight_data;
45         public boolean          has_gps;
46         public boolean          has_gps_sats;
47         public boolean          has_gps_detail;
48         public boolean          has_flight_adc;
49         public boolean          has_battery;
50         public boolean          has_rssi;
51         public boolean          has_imu;
52         public boolean          has_mag;
53         public boolean          has_orient;
54         public int              num_igniter;
55
56         double landed_time(AltosFlightSeries series) {
57                 double  landed_state_time = AltosLib.MISSING;
58
59                 double  prev_state_time = AltosLib.MISSING;
60                 if (series.state_series != null) {
61                         for (AltosTimeValue state : series.state_series) {
62                                 if (state.value == AltosLib.ao_flight_landed) {
63                                         landed_state_time = state.time;
64                                         break;
65                                 } else {
66                                         prev_state_time = state.time;
67                                 }
68                         }
69                 }
70
71                 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
72                         landed_state_time = series.height_series.get(series.height_series.size()-1).time;
73
74                 double landed_height = AltosLib.MISSING;
75
76                 if (series.height_series != null) {
77                         for (AltosTimeValue height : series.height_series) {
78                                 landed_height = height.value;
79                                 if (height.time >= landed_state_time)
80                                         break;
81                         }
82                 }
83
84                 if (landed_height == AltosLib.MISSING)
85                         return AltosLib.MISSING;
86
87                 boolean above = true;
88
89                 double  landed_time = AltosLib.MISSING;
90
91                 if (series.height_series != null) {
92                         for (AltosTimeValue height : series.height_series) {
93                                 if (height.value > landed_height + 10) {
94                                         above = true;
95                                 } else {
96                                         if (above && Math.abs(height.value - landed_height) < 2) {
97                                                 above = false;
98                                                 landed_time = height.time;
99                                         }
100                                 }
101                         }
102                 }
103
104                 if (landed_time == AltosLib.MISSING || (prev_state_time != AltosLib.MISSING && landed_time < prev_state_time))
105                         landed_time = landed_state_time;
106                 return landed_time;
107         }
108
109         double boost_time(AltosFlightSeries series) {
110                 double          boost_time = AltosLib.MISSING;
111                 double          boost_state_time = AltosLib.MISSING;
112
113                 if (series.state_series != null) {
114                         for (AltosTimeValue state : series.state_series) {
115                                 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
116                                         boost_state_time = state.time;
117                                         break;
118                                 }
119                         }
120                 }
121                 if (series.accel_series != null) {
122                         for (AltosTimeValue accel : series.accel_series) {
123                                 if (accel.value < 1)
124                                         boost_time = accel.time;
125                                 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
126                                         break;
127                         }
128                 }
129                 if (boost_time == AltosLib.MISSING)
130                         boost_time = boost_state_time;
131                 return boost_time;
132         }
133
134         private void add_times(AltosFlightSeries series, int state, double start_time, double end_time) {
135                 double delta_time = end_time - start_time;
136                 if (0 <= state && state <= AltosLib.ao_flight_invalid && delta_time > 0) {
137                         if (state_enter_speed[state] == AltosLib.MISSING)
138                                 state_enter_speed[state] = series.speed_series.value(start_time);
139                         if (state_enter_height[state] == AltosLib.MISSING)
140                                 state_enter_height[state] = series.height_series.value(start_time);
141                         if (state_enter_gps_height[state] == AltosLib.MISSING)
142                                 if (series.gps_height != null)
143                                         state_enter_gps_height[state] = series.gps_height.value(start_time);
144                         speeds[state].value += series.speed_series.average(start_time, end_time) * delta_time;
145                         speeds[state].time += delta_time;
146                         accels[state].value += series.accel_series.average(start_time, end_time) * delta_time;
147                         accels[state].time += delta_time;
148                         state_time[state] += delta_time;
149
150                         if (state == AltosLib.ao_flight_boost) {
151                                 AltosTimeValue tv_speed = series.speed_series.max(start_time, end_time);
152                                 if (tv_speed != null && (max_speed == AltosLib.MISSING || tv_speed.value > max_speed))
153                                         max_speed = tv_speed.value;
154                                 AltosTimeValue tv_accel = series.accel_series.max(start_time, end_time);
155                                 if (tv_accel != null && (max_acceleration == AltosLib.MISSING || tv_accel.value > max_acceleration))
156                                         max_acceleration = tv_accel.value;
157                         }
158                 }
159         }
160
161         AltosTimeValue[]        speeds = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
162         AltosTimeValue[]        accels = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
163
164         public AltosFlightStats(AltosFlightSeries series) {
165                 AltosCalData    cal_data = series.cal_data();
166
167                 series.finish();
168
169                 boost_time = boost_time(series);
170                 landed_time = landed_time(series);
171
172                 if (series.state_series != null){
173                         boolean fixed_boost = false;
174                         boolean fixed_landed = false;
175                         for (AltosTimeValue state : series.state_series) {
176                                 if ((int) state.value == AltosLib.ao_flight_boost)
177                                         if (boost_time != AltosLib.MISSING && !fixed_boost) {
178                                                 state.time = boost_time;
179                                                 fixed_boost = true;
180                                         }
181                                 if ((int) state.value == AltosLib.ao_flight_landed)
182                                         if (landed_time != AltosLib.MISSING && !fixed_landed) {
183                                                 state.time = landed_time;
184                                                 fixed_landed = true;
185                                         }
186                         }
187                 }
188
189                 year = month = day = AltosLib.MISSING;
190                 hour = minute = second = AltosLib.MISSING;
191                 serial = flight = AltosLib.MISSING;
192                 lat = lon = AltosLib.MISSING;
193                 has_flight_data = false;
194                 has_gps = false;
195                 has_gps_sats = false;
196                 has_flight_adc = false;
197                 has_battery = false;
198                 has_rssi = false;
199                 has_imu = false;
200                 has_mag = false;
201                 has_orient = false;
202
203                 for (int s = 0; s < AltosLib.ao_flight_invalid + 1; s++) {
204                         state_speed[s] = AltosLib.MISSING;
205                         state_enter_speed[s] = AltosLib.MISSING;
206                         state_accel[s] = AltosLib.MISSING;
207                         state_time[s] = 0;
208                         speeds[s] = new AltosTimeValue(0, 0);
209                         accels[s] = new AltosTimeValue(0, 0);
210                 }
211
212                 max_speed = AltosLib.MISSING;
213                 max_acceleration = AltosLib.MISSING;
214
215                 if (series.state_series != null) {
216                         AltosTimeValue prev = null;
217                         for (AltosTimeValue state : series.state_series) {
218                                 if (prev != null)
219                                         add_times(series, (int) prev.value, prev.time, state.time);
220                                 prev = state;
221                         }
222                         if (prev != null) {
223                                 AltosTimeValue last_accel = series.accel_series.last();
224                                 if (last_accel != null)
225                                         add_times(series, (int) prev.value, prev.time, last_accel.time);
226                         }
227                 }
228
229                 for (int s = 0; s <= AltosLib.ao_flight_invalid; s++) {
230                         if (speeds[s].time > 0)
231                                 state_speed[s] = speeds[s].value / speeds[s].time;
232                         if (accels[s].time > 0)
233                                 state_accel[s] = accels[s].value / accels[s].time;
234                 }
235
236                 product = cal_data.product;
237                 firmware_version = cal_data.firmware_version;
238                 serial = cal_data.serial;
239                 flight = cal_data.flight;
240
241                 has_battery = series.battery_voltage_series != null;
242                 has_flight_adc = series.main_voltage_series != null;
243                 has_rssi = series.rssi_series != null;
244                 has_flight_data = series.pressure_series != null;
245
246                 AltosGPS gps = series.cal_data().gps_pad;
247
248                 if (gps != null) {
249                         year = gps.year;
250                         month = gps.month;
251                         day = gps.day;
252                         hour = gps.hour;
253                         minute = gps.minute;
254                         second = gps.second;
255                         has_gps = true;
256                         lat = pad_lat = gps.lat;
257                         lon = pad_lon = gps.lon;
258                         if (series.gps_series != null) {
259                                 for (AltosGPSTimeValue gtv : series.gps_series) {
260                                         gps = gtv.gps;
261                                         if (gps.locked && gps.nsat >= 4) {
262                                                 lat = gps.lat;
263                                                 lon = gps.lon;
264                                         }
265                                 }
266                         }
267                 }
268
269                 max_height = series.max_height;
270                 if (max_height == AltosLib.MISSING && series.height_series != null)
271                         max_height = series.height_series.max().value;
272                 max_gps_height = AltosLib.MISSING;
273                 if (series.gps_height != null) {
274                         AltosTimeValue tv = series.gps_height.max();
275                         if (tv != null)
276                                 max_gps_height = tv.value;
277                 }
278         }
279 }