f48e6be6077d8cb5a85b935624c92aa955362195
[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           lat, lon;
38         public double           pad_lat, pad_lon;
39         public boolean          has_flight_data;
40         public boolean          has_gps;
41         public boolean          has_gps_sats;
42         public boolean          has_gps_detail;
43         public boolean          has_flight_adc;
44         public boolean          has_battery;
45         public boolean          has_rssi;
46         public boolean          has_imu;
47         public boolean          has_mag;
48         public boolean          has_orient;
49         public int              num_igniter;
50
51         double landed_time(AltosFlightSeries series) {
52                 double  landed_state_time = AltosLib.MISSING;
53
54                 if (series.state_series != null) {
55                         for (AltosTimeValue state : series.state_series) {
56                                 if (state.value == AltosLib.ao_flight_landed) {
57                                         landed_state_time = state.time;
58                                         break;
59                                 }
60                         }
61                 }
62
63                 if (landed_state_time == AltosLib.MISSING && series.height_series != null)
64                         landed_state_time = series.height_series.get(series.height_series.size()-1).time;
65
66                 double landed_height = AltosLib.MISSING;
67
68                 if (series.height_series != null) {
69                         for (AltosTimeValue height : series.height_series) {
70                                 if (height.time >= landed_state_time) {
71                                         landed_height = height.value;
72                                         break;
73                                 }
74                         }
75                 }
76
77                 if (landed_height == AltosLib.MISSING)
78                         return AltosLib.MISSING;
79
80                 boolean above = true;
81
82                 double  landed_time = AltosLib.MISSING;
83
84                 if (series.height_series != null) {
85                         for (AltosTimeValue height : series.height_series) {
86                                 if (height.value > landed_height + 10) {
87                                         above = true;
88                                 } else {
89                                         if (above && Math.abs(height.value - landed_height) < 2) {
90                                                 above = false;
91                                                 landed_time = height.time;
92                                         }
93                                 }
94                         }
95                 }
96                 return landed_time;
97         }
98
99         double boost_time(AltosFlightSeries series) {
100                 double          boost_time = AltosLib.MISSING;
101                 double          boost_state_time = AltosLib.MISSING;
102
103                 if (series.state_series != null) {
104                         for (AltosTimeValue state : series.state_series) {
105                                 if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
106                                         boost_state_time = state.time;
107                                         break;
108                                 }
109                         }
110                 }
111                 if (series.accel_series != null) {
112                         for (AltosTimeValue accel : series.accel_series) {
113                                 if (accel.value < 1)
114                                         boost_time = accel.time;
115                                 if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
116                                         break;
117                         }
118                 }
119                 return boost_time;
120         }
121
122
123         public AltosFlightStats(AltosFlightSeries series) {
124                 AltosCalData    cal_data = series.cal_data;
125                 double          boost_time = boost_time(series);
126                 double          end_time = 0;
127                 double          landed_time = landed_time(series);
128
129                 series.finish();
130
131                 year = month = day = AltosLib.MISSING;
132                 hour = minute = second = AltosLib.MISSING;
133                 serial = flight = AltosLib.MISSING;
134                 lat = lon = AltosLib.MISSING;
135                 has_flight_data = false;
136                 has_gps = false;
137                 has_gps_sats = false;
138                 has_flight_adc = false;
139                 has_battery = false;
140                 has_rssi = false;
141                 has_imu = false;
142                 has_mag = false;
143                 has_orient = false;
144
145                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
146                         state_count[s] = 0;
147
148                         if (s == AltosLib.ao_flight_boost)
149                                 state_start[s] = boost_time;
150                         else if (series.state_series != null)
151                                 state_start[s] = series.state_series.time_of(s);
152                         else
153                                 state_start[s] = AltosLib.MISSING;
154                         if (s == AltosLib.ao_flight_main)
155                                 state_end[s] = landed_time;
156                         else if (series.state_series != null)
157                                 state_end[s] = series.state_series.time_of(s+1);
158                         else
159                                 state_end[s] = AltosLib.MISSING;
160
161                         if (series.speed_series != null)
162                                 state_speed[s] = series.speed_series.average(state_start[s], state_end[s]);
163
164                         if (series.accel_series != null)
165                                 state_accel[s] = series.accel_series.average(state_start[s], state_end[s]);
166                 }
167
168                 serial = cal_data.serial;
169                 flight = cal_data.flight;
170
171                 has_battery = series.battery_voltage_series != null;
172                 has_flight_adc = series.main_voltage_series != null;
173                 has_rssi = series.rssi_series != null;
174                 has_flight_data = series.pressure_series != null;
175
176                 AltosGPS gps = series.cal_data.gps_pad;
177
178                 if (gps != null) {
179                         year = gps.year;
180                         month = gps.month;
181                         day = gps.day;
182                         hour = gps.hour;
183                         minute = gps.minute;
184                         second = gps.second;
185                         has_gps = true;
186                         lat = pad_lat = gps.lat;
187                         lon = pad_lon = gps.lon;
188                         for (AltosGPSTimeValue gtv : series.gps_series) {
189                                 gps = gtv.gps;
190                                 if (gps.locked && gps.nsat >= 4) {
191                                         lat = gps.lat;
192                                         lon = gps.lon;
193                                 }
194                         }
195
196                 }
197
198                 max_height = AltosLib.MISSING;
199                 if (series.height_series != null)
200                         max_height = series.height_series.max();
201                 max_speed = AltosLib.MISSING;
202                 if (series.speed_series != null) {
203                         max_speed = series.speed_series.max(state_start[AltosLib.ao_flight_boost], state_start[AltosLib.ao_flight_drogue]);
204                         if (max_speed == AltosLib.MISSING)
205                                 max_speed = series.speed_series.max();
206                 }
207                 max_acceleration = AltosLib.MISSING;
208                 if (series.accel_series != null) {
209                         max_acceleration = series.accel_series.max(state_start[AltosLib.ao_flight_boost], state_start[AltosLib.ao_flight_drogue]);
210                         if (max_acceleration == AltosLib.MISSING)
211                                 max_acceleration = series.accel_series.max();
212                 }
213                 max_gps_height = AltosLib.MISSING;
214                 if (series.gps_height != null)
215                         max_gps_height = series.gps_height.max();
216
217         }
218 }