a6c149080171c3087e9e08a5627a6a1fbba578ab
[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 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                                 if (height.time >= landed_state_time) {
73                                         landed_height = height.value;
74                                         break;
75                                 }
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                 return landed_time;
99         }
100
101         double boost_time(AltosFlightSeries series) {
102                 double          boost_time = AltosLib.MISSING;
103                 double          boost_state_time = AltosLib.MISSING;
104
105                 if (series.state_series != null) {
106                         for (AltosTimeValue state : series.state_series) {
107                                 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
108                                         boost_state_time = state.time;
109                                         break;
110                                 }
111                         }
112                 }
113                 if (series.accel_series != null) {
114                         for (AltosTimeValue accel : series.accel_series) {
115                                 if (accel.value < 1)
116                                         boost_time = accel.time;
117                                 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
118                                         break;
119                         }
120                 }
121                 if (boost_time == AltosLib.MISSING)
122                         boost_time = boost_state_time;
123                 return boost_time;
124         }
125
126
127         public AltosFlightStats(AltosFlightSeries series) {
128                 AltosCalData    cal_data = series.cal_data;
129
130                 series.finish();
131
132                 boost_time = boost_time(series);
133                 landed_time = landed_time(series);
134
135                 year = month = day = AltosLib.MISSING;
136                 hour = minute = second = AltosLib.MISSING;
137                 serial = flight = AltosLib.MISSING;
138                 lat = lon = AltosLib.MISSING;
139                 has_flight_data = false;
140                 has_gps = false;
141                 has_gps_sats = false;
142                 has_flight_adc = false;
143                 has_battery = false;
144                 has_rssi = false;
145                 has_imu = false;
146                 has_mag = false;
147                 has_orient = false;
148
149                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
150                         state_count[s] = 0;
151
152                         if (s == AltosLib.ao_flight_boost)
153                                 state_start[s] = boost_time;
154                         else if (series.state_series != null)
155                                 state_start[s] = series.state_series.time_of(s);
156                         else
157                                 state_start[s] = AltosLib.MISSING;
158                         if (s == AltosLib.ao_flight_main)
159                                 state_end[s] = landed_time;
160                         else if (series.state_series != null)
161                                 state_end[s] = series.state_series.time_of(s+1);
162                         else
163                                 state_end[s] = AltosLib.MISSING;
164
165                         if (state_end[s] > landed_time)
166                                 state_end[s] = landed_time;
167
168                         if (series.speed_series != null)
169                                 state_speed[s] = series.speed_series.average(state_start[s], state_end[s]);
170
171                         if (series.accel_series != null)
172                                 state_accel[s] = series.accel_series.average(state_start[s], state_end[s]);
173                 }
174
175                 serial = cal_data.serial;
176                 flight = cal_data.flight;
177
178                 has_battery = series.battery_voltage_series != null;
179                 has_flight_adc = series.main_voltage_series != null;
180                 has_rssi = series.rssi_series != null;
181                 has_flight_data = series.pressure_series != null;
182
183                 AltosGPS gps = series.cal_data.gps_pad;
184
185                 if (gps != null) {
186                         year = gps.year;
187                         month = gps.month;
188                         day = gps.day;
189                         hour = gps.hour;
190                         minute = gps.minute;
191                         second = gps.second;
192                         has_gps = true;
193                         lat = pad_lat = gps.lat;
194                         lon = pad_lon = gps.lon;
195                         for (AltosGPSTimeValue gtv : series.gps_series) {
196                                 gps = gtv.gps;
197                                 if (gps.locked && gps.nsat >= 4) {
198                                         lat = gps.lat;
199                                         lon = gps.lon;
200                                 }
201                         }
202
203                 }
204
205                 max_height = AltosLib.MISSING;
206                 if (series.height_series != null)
207                         max_height = series.height_series.max().value;
208                 max_speed = AltosLib.MISSING;
209                 if (series.speed_series != null) {
210                         AltosTimeValue tv = series.speed_series.max(state_start[AltosLib.ao_flight_boost], state_start[AltosLib.ao_flight_drogue]);
211                         if (tv == null)
212                                 tv = series.speed_series.max();
213                         if (tv != null)
214                                 max_speed = tv.value;
215                 }
216                 max_acceleration = AltosLib.MISSING;
217                 if (series.accel_series != null) {
218                         AltosTimeValue tv = series.accel_series.max(state_start[AltosLib.ao_flight_boost], state_start[AltosLib.ao_flight_drogue]);
219                         if (tv == null)
220                                 tv = series.accel_series.max();
221                         if (tv != null)
222                                 max_acceleration = tv.value;
223                 }
224                 max_gps_height = AltosLib.MISSING;
225                 if (series.gps_height != null)
226                         max_gps_height = series.gps_height.max().value;
227
228         }
229 }