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