Switch from GPLv2 to GPLv2+
[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(AltosStateIterable states) {
52                 AltosState state = null;
53
54                 for (AltosState s : states) {
55                         state = s;
56                         if (state.state() == AltosLib.ao_flight_landed)
57                                 break;
58                 }
59
60                 if (state == null)
61                         return AltosLib.MISSING;
62
63                 double  landed_height = state.height();
64
65                 state = null;
66
67                 boolean above = true;
68
69                 double  landed_time = AltosLib.MISSING;
70
71                 for (AltosState s : states) {
72                         state = s;
73
74                         if (state.height() > landed_height + 10) {
75                                 above = true;
76                         } else {
77                                 if (above && Math.abs(state.height() - landed_height) < 2) {
78                                         above = false;
79                                         landed_time = state.time;
80                                 }
81                         }
82                 }
83                 return landed_time;
84         }
85
86         double boost_time(AltosStateIterable states) {
87                 double boost_time = AltosLib.MISSING;
88                 AltosState      state = null;
89
90                 for (AltosState s : states) {
91                         state = s;
92                         if (state.acceleration() < 1)
93                                 boost_time = state.time;
94                         if (state.state() >= AltosLib.ao_flight_boost && state.state() <= AltosLib.ao_flight_landed)
95                                 break;
96                 }
97                 if (state == null)
98                         return AltosLib.MISSING;
99
100                 return boost_time;
101         }
102
103
104         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
105                 double          boost_time = boost_time(states);
106                 double          end_time = 0;
107                 double          landed_time = landed_time(states);
108
109                 year = month = day = AltosLib.MISSING;
110                 hour = minute = second = AltosLib.MISSING;
111                 serial = flight = AltosLib.MISSING;
112                 lat = lon = AltosLib.MISSING;
113                 has_flight_data = false;
114                 has_gps = false;
115                 has_gps_sats = false;
116                 has_flight_adc = false;
117                 has_battery = false;
118                 has_rssi = false;
119                 has_imu = false;
120                 has_mag = false;
121                 has_orient = false;
122
123                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
124                         state_count[s] = 0;
125                         state_speed[s] = 0.0;
126                         state_accel[s] = 0.0;
127                 }
128
129                 for (AltosState state : states) {
130                         if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
131                                 serial = state.serial;
132                         if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
133                                 flight = state.flight;
134                         if (state.battery_voltage != AltosLib.MISSING)
135                                 has_battery = true;
136                         if (state.main_voltage != AltosLib.MISSING)
137                                 has_flight_adc = true;
138                         if (state.rssi != AltosLib.MISSING)
139                                 has_rssi = true;
140                         end_time = state.time;
141
142                         if (state.pressure() != AltosLib.MISSING)
143                                 has_flight_data = true;
144
145                         int state_id = state.state();
146                         if (boost_time != AltosLib.MISSING && state.time >= boost_time && state_id < AltosLib.ao_flight_boost) {
147                                 state_id = AltosLib.ao_flight_boost;
148                         }
149                         if (landed_time != AltosLib.MISSING && state.time >= landed_time && state_id < AltosLib.ao_flight_landed) {
150                                 state_id = AltosLib.ao_flight_landed;
151                         }
152
153                         if (state.gps != null && state.gps.locked) {
154                                 year = state.gps.year;
155                                 month = state.gps.month;
156                                 day = state.gps.day;
157                                 hour = state.gps.hour;
158                                 minute = state.gps.minute;
159                                 second = state.gps.second;
160                         }
161                         max_height = state.max_height();
162                         max_speed = state.max_speed();
163                         max_acceleration = state.max_acceleration();
164                         max_gps_height = state.max_gps_height();
165
166                         if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
167                                 double acceleration = state.acceleration();
168                                 double speed = state.speed();
169                                 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
170                                         state_accel[state_id] += acceleration;
171                                         state_speed[state_id] += speed;
172                                         state_count[state_id]++;
173                                 }
174                                 if (state_start[state_id] == 0.0)
175                                         state_start[state_id] = state.time;
176                                 if (state_end[state_id] < state.time)
177                                         state_end[state_id] = state.time;
178                         }
179                         if (state.pad_lat != AltosLib.MISSING) {
180                                 pad_lat = state.pad_lat;
181                                 pad_lon = state.pad_lon;
182                         }
183                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
184                                 lat = state.gps.lat;
185                                 lon = state.gps.lon;
186                                 has_gps = true;
187                                 if (state.gps.cc_gps_sat != null)
188                                         has_gps_sats = true;
189                                 if (state.gps.course != AltosLib.MISSING)
190                                         has_gps_detail = true;
191                         }
192                         if (state.imu != null)
193                                 has_imu = true;
194                         if (state.mag != null)
195                                 has_mag = true;
196                         if (state.orient() != AltosLib.MISSING)
197                                 has_orient = true;
198                         if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
199                                 num_ignitor = state.ignitor_voltage.length;
200                 }
201                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
202                         if (state_count[s] > 0) {
203                                 state_speed[s] /= state_count[s];
204                                 state_accel[s] /= state_count[s];
205                         } else {
206                                 state_speed[s] = AltosLib.MISSING;
207                                 state_accel[s] = AltosLib.MISSING;
208                         }
209                         if (state_start[s] == 0)
210                                 state_start[s] = end_time;
211                         if (state_end[s] == 0)
212                                 state_end[s] = end_time;
213                 }
214         }
215 }