altosui/altosuilib/altoslib: Move more stuff out of autosui. Reduce site map memory
[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_4;
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_gps;
39         public boolean          has_other_adc;
40         public boolean          has_rssi;
41         public boolean          has_imu;
42         public boolean          has_mag;
43         public boolean          has_orient;
44         public int              num_ignitor;
45
46         double landed_time(AltosStateIterable states) {
47                 AltosState state = null;
48
49                 for (AltosState s : states) {
50                         state = s;
51                         if (state.state == AltosLib.ao_flight_landed)
52                                 break;
53                 }
54
55                 if (state == null)
56                         return 0;
57
58                 double  landed_height = state.height();
59
60                 state = null;
61
62                 boolean above = true;
63
64                 double  landed_time = -1000;
65
66                 for (AltosState s : states) {
67                         state = s;
68
69                         if (state.height() > landed_height + 10) {
70                                 above = true;
71                         } else {
72                                 if (above && state.height() < landed_height + 2) {
73                                         above = false;
74                                         landed_time = state.time;
75                                 }
76                         }
77                 }
78                 if (landed_time == -1000)
79                         landed_time = state.time;
80                 return landed_time;
81         }
82
83         double boost_time(AltosStateIterable states) {
84                 double boost_time = AltosLib.MISSING;
85                 AltosState      state = null;
86
87                 for (AltosState s : states) {
88                         state = s;
89                         if (state.acceleration() < 1)
90                                 boost_time = state.time;
91                         if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
92                                 break;
93                 }
94                 if (state == null)
95                         return 0;
96
97                 if (boost_time == AltosLib.MISSING)
98                         boost_time = state.time;
99                 return boost_time;
100         }
101
102
103         public AltosFlightStats(AltosStateIterable states) throws InterruptedException, IOException {
104                 double          boost_time = boost_time(states);
105                 double          end_time = 0;
106                 double          landed_time = landed_time(states);
107
108                 year = month = day = AltosLib.MISSING;
109                 hour = minute = second = AltosLib.MISSING;
110                 serial = flight = AltosLib.MISSING;
111                 lat = lon = AltosLib.MISSING;
112                 has_gps = false;
113                 has_other_adc = false;
114                 has_rssi = false;
115                 has_imu = false;
116                 has_mag = false;
117                 has_orient = false;
118                 for (AltosState state : states) {
119                         if (serial == AltosLib.MISSING && state.serial != AltosLib.MISSING)
120                                 serial = state.serial;
121                         if (flight == AltosLib.MISSING && state.flight != AltosLib.MISSING)
122                                 flight = state.flight;
123                         if (state.battery_voltage != AltosLib.MISSING)
124                                 has_other_adc = true;
125                         if (state.rssi != AltosLib.MISSING)
126                                 has_rssi = true;
127                         end_time = state.time;
128
129                         int state_id = state.state;
130                         if (state.time >= boost_time && state_id < AltosLib.ao_flight_boost)
131                                 state_id = AltosLib.ao_flight_boost;
132                         if (state.time >= landed_time && state_id < AltosLib.ao_flight_landed)
133                                 state_id = AltosLib.ao_flight_landed;
134                         if (state.gps != null && state.gps.locked) {
135                                 year = state.gps.year;
136                                 month = state.gps.month;
137                                 day = state.gps.day;
138                                 hour = state.gps.hour;
139                                 minute = state.gps.minute;
140                                 second = state.gps.second;
141                         }
142                         if (0 <= state_id && state_id < AltosLib.ao_flight_invalid) {
143                                 double acceleration = state.acceleration();
144                                 double speed = state.speed();
145                                 if (acceleration != AltosLib.MISSING && speed != AltosLib.MISSING) {
146                                         state_accel[state_id] += acceleration;
147                                         state_speed[state_id] += speed;
148                                         state_count[state_id]++;
149                                 }
150                                 if (state_start[state_id] == 0.0)
151                                         state_start[state_id] = state.time;
152                                 if (state_end[state_id] < state.time)
153                                         state_end[state_id] = state.time;
154                                 max_height = state.max_height();
155                                 max_speed = state.max_speed();
156                                 max_acceleration = state.max_acceleration();
157                                 max_gps_height = state.max_gps_height();
158                         }
159                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
160                                 if (state_id <= AltosLib.ao_flight_pad) {
161                                         pad_lat = state.gps.lat;
162                                         pad_lon = state.gps.lon;
163                                 }
164                                 lat = state.gps.lat;
165                                 lon = state.gps.lon;
166                                 has_gps = true;
167                         }
168                         if (state.imu != null)
169                                 has_imu = true;
170                         if (state.mag != null)
171                                 has_mag = true;
172                         if (state.orient() != AltosLib.MISSING)
173                                 has_orient = true;
174                         if (state.ignitor_voltage != null && state.ignitor_voltage.length > num_ignitor)
175                                 num_ignitor = state.ignitor_voltage.length;
176                 }
177                 for (int s = AltosLib.ao_flight_startup; s <= AltosLib.ao_flight_landed; s++) {
178                         if (state_count[s] > 0) {
179                                 state_speed[s] /= state_count[s];
180                                 state_accel[s] /= state_count[s];
181                         }
182                         if (state_start[s] == 0)
183                                 state_start[s] = end_time;
184                         if (state_end[s] == 0)
185                                 state_end[s] = end_time;
186                 }
187         }
188 }