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