altosuilib: Don't show 'Sats in view' for TeleGPS eeprom graphing
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.io.*;
21
22 public class AltosFlightStats {
23         public double           max_height;
24         public double           max_gps_height;
25         public double           max_speed;
26         public double           max_acceleration;
27         public double[] state_speed = new double[AltosLib.ao_flight_invalid + 1];
28         public double[] state_accel = new double[AltosLib.ao_flight_invalid + 1];
29         public int[]            state_count = new int[AltosLib.ao_flight_invalid + 1];
30         public double[] state_start = new double[AltosLib.ao_flight_invalid + 1];
31         public double[] state_end = new double[AltosLib.ao_flight_invalid + 1];
32         public int              serial;
33         public int              flight;
34         public int              year, month, day;
35         public int              hour, minute, second;
36         public double           lat, lon;
37         public double           pad_lat, pad_lon;
38         public boolean          has_flight_data;
39         public boolean          has_gps;
40         public boolean          has_gps_sats;
41         public boolean          has_flight_adc;
42         public boolean          has_battery;
43         public boolean          has_rssi;
44         public boolean          has_imu;
45         public boolean          has_mag;
46         public boolean          has_orient;
47         public int              num_ignitor;
48
49         double landed_time(AltosStateIterable states) {
50                 AltosState state = null;
51
52                 for (AltosState s : states) {
53                         state = s;
54                         if (state.state == AltosLib.ao_flight_landed)
55                                 break;
56                 }
57
58                 if (state == null)
59                         return 0;
60
61                 double  landed_height = state.height();
62
63                 state = null;
64
65                 boolean above = true;
66
67                 double  landed_time = -1000;
68
69                 for (AltosState s : states) {
70                         state = s;
71
72                         if (state.height() > landed_height + 10) {
73                                 above = true;
74                         } else {
75                                 if (above && state.height() < landed_height + 2) {
76                                         above = false;
77                                         landed_time = state.time;
78                                 }
79                         }
80                 }
81                 if (landed_time == -1000)
82                         landed_time = state.time;
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 0;
99
100                 if (boost_time == AltosLib.MISSING)
101                         boost_time = state.time;
102                 return boost_time;
103         }
104
105
106         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
107                 double          boost_time = boost_time(states);
108                 double          end_time = 0;
109                 double          landed_time = landed_time(states);
110
111                 year = month = day = AltosLib.MISSING;
112                 hour = minute = second = AltosLib.MISSING;
113                 serial = flight = AltosLib.MISSING;
114                 lat = lon = AltosLib.MISSING;
115                 has_flight_data = false;
116                 has_gps = false;
117                 has_gps_sats = false;
118                 has_flight_adc = false;
119                 has_battery = false;
120                 has_rssi = false;
121                 has_imu = false;
122                 has_mag = false;
123                 has_orient = false;
124                 for (AltosState state : states) {
125                         if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
126                                 serial = state.serial;
127                         if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
128                                 flight = state.flight;
129                         if (state.battery_voltage != AltosLib.MISSING)
130                                 has_battery = true;
131                         if (state.main_voltage != AltosLib.MISSING)
132                                 has_flight_adc = true;
133                         if (state.rssi != AltosLib.MISSING)
134                                 has_rssi = true;
135                         end_time = state.time;
136
137                         if (state.pressure() != AltosLib.MISSING)
138                                 has_flight_data = true;
139
140                         int state_id = state.state;
141                         if (state.time >= boost_time && state_id < AltosLib.ao_flight_boost)
142                                 state_id = AltosLib.ao_flight_boost;
143                         if (state.time >= landed_time && state_id < AltosLib.ao_flight_landed)
144                                 state_id = AltosLib.ao_flight_landed;
145                         if (state.gps != null && state.gps.locked) {
146                                 year = state.gps.year;
147                                 month = state.gps.month;
148                                 day = state.gps.day;
149                                 hour = state.gps.hour;
150                                 minute = state.gps.minute;
151                                 second = state.gps.second;
152                         }
153                         max_height = state.max_height();
154                         max_speed = state.max_speed();
155                         max_acceleration = state.max_acceleration();
156                         max_gps_height = state.max_gps_height();
157
158                         if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
159                                 double acceleration = state.acceleration();
160                                 double speed = state.speed();
161                                 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
162                                         state_accel[state_id] += acceleration;
163                                         state_speed[state_id] += speed;
164                                         state_count[state_id]++;
165                                 }
166                                 if (state_start[state_id] == 0.0)
167                                         state_start[state_id] = state.time;
168                                 if (state_end[state_id] < state.time)
169                                         state_end[state_id] = state.time;
170                         }
171                         if (state.pad_lat != AltosLib.MISSING) {
172                                 pad_lat = state.pad_lat;
173                                 pad_lon = state.pad_lon;
174                         }
175                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
176                                 lat = state.gps.lat;
177                                 lon = state.gps.lon;
178                                 has_gps = true;
179                                 if (state.gps.cc_gps_sat != null)
180                                         has_gps_sats = true;
181                         }
182                         if (state.imu != null)
183                                 has_imu = true;
184                         if (state.mag != null)
185                                 has_mag = true;
186                         if (state.orient() != AltosLib.MISSING)
187                                 has_orient = true;
188                         if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
189                                 num_ignitor = state.ignitor_voltage.length;
190                 }
191                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
192                         if (state_count[s] > 0) {
193                                 state_speed[s] /= state_count[s];
194                                 state_accel[s] /= state_count[s];
195                         } else {
196                                 state_speed[s] = AltosLib.MISSING;
197                                 state_accel[s] = AltosLib.MISSING;
198                         }
199                         if (state_start[s] == 0)
200                                 state_start[s] = end_time;
201                         if (state_end[s] == 0)
202                                 state_end[s] = end_time;
203                 }
204         }
205 }