Set version to 1.7.1 for TeleBT firmware build
[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_12;
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_accel = new double[AltosLib.ao_flight_invalid + 1];
30         public double[]         state_time = new double[AltosLib.ao_flight_invalid + 1];
31         public String           product;
32         public String           firmware_version;
33         public int              serial;
34         public int              flight;
35         public int              year, month, day;
36         public int              hour, minute, second;
37         public double           boost_time;
38         public double           landed_time;
39         public double           lat, lon;
40         public double           pad_lat, pad_lon;
41         public boolean          has_flight_data;
42         public boolean          has_gps;
43         public boolean          has_gps_sats;
44         public boolean          has_gps_detail;
45         public boolean          has_flight_adc;
46         public boolean          has_battery;
47         public boolean          has_rssi;
48         public boolean          has_imu;
49         public boolean          has_mag;
50         public boolean          has_orient;
51         public int              num_igniter;
52
53         double landed_time(AltosFlightSeries series) {
54                 double  landed_state_time = AltosLib.MISSING;
55
56                 if (series.state_series != null) {
57                         for (AltosTimeValue state : series.state_series) {
58                                 if (state.value == AltosLib.ao_flight_landed) {
59                                         landed_state_time = state.time;
60                                         break;
61                                 }
62                         }
63                 }
64
65                 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
66                         landed_state_time = series.height_series.get(series.height_series.size()-1).time;
67
68                 double landed_height = AltosLib.MISSING;
69
70                 if (series.height_series != null) {
71                         for (AltosTimeValue height : series.height_series) {
72                                 landed_height = height.value;
73                                 if (height.time >= landed_state_time)
74                                         break;
75                         }
76                 }
77
78                 if (landed_height == AltosLib.MISSING)
79                         return AltosLib.MISSING;
80
81                 boolean above = true;
82
83                 double  landed_time = AltosLib.MISSING;
84
85                 if (series.height_series != null) {
86                         for (AltosTimeValue height : series.height_series) {
87                                 if (height.value > landed_height + 10) {
88                                         above = true;
89                                 } else {
90                                         if (above && Math.abs(height.value - landed_height) < 2) {
91                                                 above = false;
92                                                 landed_time = height.time;
93                                         }
94                                 }
95                         }
96                 }
97
98                 if (landed_time == AltosLib.MISSING)
99                         landed_time = landed_state_time;
100                 return landed_time;
101         }
102
103         double boost_time(AltosFlightSeries series) {
104                 double          boost_time = AltosLib.MISSING;
105                 double          boost_state_time = AltosLib.MISSING;
106
107                 if (series.state_series != null) {
108                         for (AltosTimeValue state : series.state_series) {
109                                 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
110                                         boost_state_time = state.time;
111                                         break;
112                                 }
113                         }
114                 }
115                 if (series.accel_series != null) {
116                         for (AltosTimeValue accel : series.accel_series) {
117                                 if (accel.value < 1)
118                                         boost_time = accel.time;
119                                 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
120                                         break;
121                         }
122                 }
123                 if (boost_time == AltosLib.MISSING)
124                         boost_time = boost_state_time;
125                 return boost_time;
126         }
127
128         private void add_times(AltosFlightSeries series, int state, double start_time, double end_time) {
129                 double delta_time = end_time - start_time;
130                 if (0 <= state && state <= AltosLib.ao_flight_invalid && delta_time > 0) {
131                         speeds[state].value += series.speed_series.average(start_time, end_time) * delta_time;
132                         speeds[state].time += delta_time;
133                         accels[state].value += series.accel_series.average(start_time, end_time) * delta_time;
134                         accels[state].time += delta_time;
135                         state_time[state] += delta_time;
136
137                         if (state == AltosLib.ao_flight_boost) {
138                                 AltosTimeValue tv_speed = series.speed_series.max(start_time, end_time);
139                                 if (tv_speed != null && (max_speed == AltosLib.MISSING || tv_speed.value > max_speed))
140                                         max_speed = tv_speed.value;
141                                 AltosTimeValue tv_accel = series.accel_series.max(start_time, end_time);
142                                 if (tv_accel != null && (max_acceleration == AltosLib.MISSING || tv_accel.value > max_acceleration))
143                                         max_acceleration = tv_accel.value;
144                         }
145                 }
146         }
147
148         AltosTimeValue[]        speeds = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
149         AltosTimeValue[]        accels = new AltosTimeValue[AltosLib.ao_flight_invalid + 1];
150
151         public AltosFlightStats(AltosFlightSeries series) {
152                 AltosCalData    cal_data = series.cal_data();
153
154                 series.finish();
155
156                 boost_time = boost_time(series);
157                 landed_time = landed_time(series);
158
159                 if (series.state_series != null){
160                         boolean fixed_boost = false;
161                         boolean fixed_landed = false;
162                         for (AltosTimeValue state : series.state_series) {
163                                 if ((int) state.value == AltosLib.ao_flight_boost)
164                                         if (boost_time != AltosLib.MISSING && !fixed_boost) {
165                                                 state.time = boost_time;
166                                                 fixed_boost = true;
167                                         }
168                                 if ((int) state.value == AltosLib.ao_flight_landed)
169                                         if (landed_time != AltosLib.MISSING && !fixed_landed) {
170                                                 state.time = landed_time;
171                                                 fixed_landed = true;
172                                         }
173                         }
174                 }
175
176                 year = month = day = AltosLib.MISSING;
177                 hour = minute = second = AltosLib.MISSING;
178                 serial = flight = AltosLib.MISSING;
179                 lat = lon = AltosLib.MISSING;
180                 has_flight_data = false;
181                 has_gps = false;
182                 has_gps_sats = false;
183                 has_flight_adc = false;
184                 has_battery = false;
185                 has_rssi = false;
186                 has_imu = false;
187                 has_mag = false;
188                 has_orient = false;
189
190                 for (int s = 0; s < AltosLib.ao_flight_invalid + 1; s++) {
191                         state_speed[s] = AltosLib.MISSING;
192                         state_accel[s] = AltosLib.MISSING;
193                         state_time[s] = 0;
194                         speeds[s] = new AltosTimeValue(0, 0);
195                         accels[s] = new AltosTimeValue(0, 0);
196                 }
197
198                 max_speed = AltosLib.MISSING;
199                 max_acceleration = AltosLib.MISSING;
200
201                 if (series.state_series != null) {
202                         AltosTimeValue prev = null;
203                         for (AltosTimeValue state : series.state_series) {
204                                 if (prev != null)
205                                         add_times(series, (int) prev.value, prev.time, state.time);
206                                 prev = state;
207                         }
208                         if (prev != null)
209                                 add_times(series, (int) prev.value, prev.time, series.accel_series.last().time);
210                 }
211
212                 for (int s = 0; s <= AltosLib.ao_flight_invalid; s++) {
213                         if (speeds[s].time > 0)
214                                 state_speed[s] = speeds[s].value / speeds[s].time;
215                         if (accels[s].time > 0)
216                                 state_accel[s] = accels[s].value / accels[s].time;
217                 }
218
219                 product = cal_data.product;
220                 firmware_version = cal_data.firmware_version;
221                 serial = cal_data.serial;
222                 flight = cal_data.flight;
223
224                 has_battery = series.battery_voltage_series != null;
225                 has_flight_adc = series.main_voltage_series != null;
226                 has_rssi = series.rssi_series != null;
227                 has_flight_data = series.pressure_series != null;
228
229                 AltosGPS gps = series.cal_data().gps_pad;
230
231                 if (gps != null) {
232                         year = gps.year;
233                         month = gps.month;
234                         day = gps.day;
235                         hour = gps.hour;
236                         minute = gps.minute;
237                         second = gps.second;
238                         has_gps = true;
239                         lat = pad_lat = gps.lat;
240                         lon = pad_lon = gps.lon;
241                         for (AltosGPSTimeValue gtv : series.gps_series) {
242                                 gps = gtv.gps;
243                                 if (gps.locked && gps.nsat >= 4) {
244                                         lat = gps.lat;
245                                         lon = gps.lon;
246                                 }
247                         }
248
249                 }
250
251                 max_height = AltosLib.MISSING;
252                 if (series.height_series != null)
253                         max_height = series.height_series.max().value;
254                 max_gps_height = AltosLib.MISSING;
255                 if (series.gps_height != null) {
256                         AltosTimeValue tv = series.gps_height.max();
257                         if (tv != null)
258                                 max_gps_height = tv.value;
259                 }
260         }
261 }