altoslib: Do data analysis on raw values rather than AltosState
[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_ignitor;
50
51         double landed_time(AltosFlightSeries series) {
52                 double  landed_state_time = AltosLib.MISSING;
53
54                 for (AltosTimeValue state : series.state_series) {
55                         if (state.value == AltosLib.ao_flight_landed) {
56                                 landed_state_time = state.time;
57                                 break;
58                         }
59                 }
60
61                 if (landed_state_time == AltosLib.MISSING)
62                         return AltosLib.MISSING;
63
64                 double landed_height = AltosLib.MISSING;
65                 for (AltosTimeValue height : series.height_series) {
66                         if (height.time >= landed_state_time) {
67                                 landed_height = height.value;
68                                 break;
69                         }
70                 }
71
72                 if (landed_height == AltosLib.MISSING)
73                         return AltosLib.MISSING;
74
75                 boolean above = true;
76
77                 double  landed_time = AltosLib.MISSING;
78
79                 for (AltosTimeValue height : series.height_series) {
80                         if (height.value > landed_height + 10) {
81                                 above = true;
82                         } else {
83                                 if (above && Math.abs(height.value - landed_height) < 2) {
84                                         above = false;
85                                         landed_time = height.time;
86                                 }
87                         }
88                 }
89                 return landed_time;
90         }
91
92         double boost_time(AltosFlightSeries series) {
93                 double          boost_time = AltosLib.MISSING;
94                 double          boost_state_time = AltosLib.MISSING;
95
96                 for (AltosTimeValue state : series.state_series) {
97                         if (state.value >= AltosLib.ao_flight_boost && state.value <= AltosLib.ao_flight_landed) {
98                                 boost_state_time = state.time;
99                                 break;
100                         }
101                 }
102                 for (AltosTimeValue accel : series.accel_series) {
103                         if (accel.value < 1)
104                                 boost_time = accel.time;
105                         if (boost_state_time != AltosLib.MISSING && accel.time >= boost_state_time)
106                                 break;
107                 }
108                 return boost_time;
109         }
110
111
112         public AltosFlightStats(AltosFlightSeries series) throws InterruptedException, IOException {
113                 AltosCalData    cal_data = series.cal_data;
114                 double          boost_time = boost_time(series);
115                 double          end_time = 0;
116                 double          landed_time = landed_time(series);
117
118                 System.out.printf("flight stats %s\n", series.toString());
119
120                 year = month = day = AltosLib.MISSING;
121                 hour = minute = second = AltosLib.MISSING;
122                 serial = flight = AltosLib.MISSING;
123                 lat = lon = AltosLib.MISSING;
124                 has_flight_data = false;
125                 has_gps = false;
126                 has_gps_sats = false;
127                 has_flight_adc = false;
128                 has_battery = false;
129                 has_rssi = false;
130                 has_imu = false;
131                 has_mag = false;
132                 has_orient = false;
133
134                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
135                         state_count[s] = 0;
136                         state_speed[s] = 0.0;
137                         state_accel[s] = 0.0;
138                 }
139
140                 serial = cal_data.serial;
141                 flight = cal_data.flight;
142
143                 has_battery = series.battery_voltage_series != null;
144                 has_flight_adc = series.main_voltage_series != null;
145                 has_rssi = series.rssi_series != null;
146                 has_flight_data = series.pressure_series != null;
147
148                 if (series.gps_series != null) {
149                         AltosGPS gps = series.gps_series.get(0).gps;
150
151                         year = gps.year;
152                         month = gps.month;
153                         day = gps.day;
154                         hour = gps.hour;
155                         minute = gps.minute;
156                         second = gps.second;
157                 }
158
159                 max_height = AltosLib.MISSING;
160                 if (series.height_series != null)
161                         max_height = series.height_series.max();
162                 max_speed = AltosLib.MISSING;
163                 if (series.speed_series != null)
164                         max_speed = series.speed_series.max();
165                 else
166                         System.out.printf("missing speed series\n");
167                 max_acceleration = AltosLib.MISSING;
168                 if (series.accel_series != null)
169                         max_acceleration = series.accel_series.max();
170                 max_gps_height = AltosLib.MISSING;
171                 if (series.gps_height != null)
172                         max_gps_height = series.gps_height.max();
173
174 /*
175                 for (AltosState state : states) {
176                         end_time = state.time;
177
178                         int state_id = state.state();
179                         if (boost_time != AltosLib.MISSING && state.time >= boost_time && state_id < AltosLib.ao_flight_boost) {
180                                 state_id = AltosLib.ao_flight_boost;
181                         }
182                         if (landed_time != AltosLib.MISSING && state.time >= landed_time && state_id < AltosLib.ao_flight_landed) {
183                                 state_id = AltosLib.ao_flight_landed;
184                         }
185
186                         if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
187                                 double acceleration = state.acceleration();
188                                 double speed = state.speed();
189                                 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
190                                         state_accel[state_id] += acceleration;
191                                         state_speed[state_id] += speed;
192                                         state_count[state_id]++;
193                                 }
194                                 if (state_start[state_id] == 0.0)
195                                         state_start[state_id] = state.time;
196                                 if (state_end[state_id] < state.time)
197                                         state_end[state_id] = state.time;
198                         }
199                         if (state.pad_lat != AltosLib.MISSING) {
200                                 pad_lat = state.pad_lat;
201                                 pad_lon = state.pad_lon;
202                         }
203                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
204                                 lat = state.gps.lat;
205                                 lon = state.gps.lon;
206                                 has_gps = true;
207                                 if (state.gps.cc_gps_sat != null)
208                                         has_gps_sats = true;
209                                 if (state.gps.course != AltosLib.MISSING)
210                                         has_gps_detail = true;
211                         }
212                         if (state.imu != null)
213                                 has_imu = true;
214                         if (state.mag != null)
215                                 has_mag = true;
216                         if (state.orient() != AltosLib.MISSING)
217                                 has_orient = true;
218                         if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
219                                 num_ignitor = state.ignitor_voltage.length;
220                 }
221 */
222                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
223                         if (state_count[s] > 0) {
224                                 state_speed[s] /= state_count[s];
225                                 state_accel[s] /= state_count[s];
226                         } else {
227                                 state_speed[s] = AltosLib.MISSING;
228                                 state_accel[s] = AltosLib.MISSING;
229                         }
230                         if (state_start[s] == 0)
231                                 state_start[s] = end_time;
232                         if (state_end[s] == 0)
233                                 state_end[s] = end_time;
234                 }
235         }
236 }