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