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