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