libaltos: Delay after opening bluetooth device on linux
[fw/altos] / altosui / 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 altosui;
19
20 import java.io.*;
21 import org.altusmetrum.altoslib_1.*;
22
23 public class AltosFlightStats {
24         double          max_height;
25         double          max_speed;
26         double          max_acceleration;
27         double[]        state_accel_speed = new double[Altos.ao_flight_invalid + 1];
28         double[]        state_baro_speed = new double[Altos.ao_flight_invalid + 1];
29         double[]        state_accel = new double[Altos.ao_flight_invalid + 1];
30         int[]           state_count = new int[Altos.ao_flight_invalid + 1];
31         double[]        state_start = new double[Altos.ao_flight_invalid + 1];
32         double[]        state_end = new double[Altos.ao_flight_invalid + 1];
33         int             serial;
34         int             flight;
35         int             year, month, day;
36         int             hour, minute, second;
37         double          lat, lon;
38         double          pad_lat, pad_lon;
39         boolean         has_gps;
40         boolean         has_other_adc;
41         boolean         has_rssi;
42
43         double landed_time(AltosRecordIterable iterable) {
44                 AltosState      state = null;
45                 for (AltosRecord record : iterable) {
46                         state = new AltosState(record, state);
47
48                         if (state.state == Altos.ao_flight_landed)
49                                 break;
50                 }
51
52                 double  landed_height = state.height;
53
54                 state = null;
55
56                 boolean above = true;
57
58                 double  landed_time = -1000;
59
60                 for (AltosRecord record : iterable) {
61                         state = new AltosState(record, state);
62
63                         if (state.height > landed_height + 10) {
64                                 above = true;
65                         } else {
66                                 if (above && state.height < landed_height + 2) {
67                                         above = false;
68                                         landed_time = state.time;
69                                 }
70                         }
71                 }
72                 if (landed_time == -1000)
73                         landed_time = state.time;
74                 return landed_time;
75         }
76
77         double boost_time(AltosRecordIterable iterable) {
78                 double boost_time = -1000;
79
80                 AltosState state = null;
81
82                 for (AltosRecord record : iterable) {
83                         state = new AltosState(record, state);
84                         
85                         if (state.acceleration < 1)
86                                 boost_time = state.time;
87                         if (state.state >= Altos.ao_flight_boost)
88                                 break;
89                 }
90                 if (boost_time == -1000)
91                         boost_time = state.time;
92                 return boost_time;
93         }
94
95
96         public AltosFlightStats(AltosRecordIterable iterable) throws InterruptedException, IOException {
97                 AltosState      state = null;
98                 AltosState      new_state = null;
99                 double          boost_time = boost_time(iterable);
100                 double          end_time = 0;
101                 double          landed_time = landed_time(iterable);
102
103                 year = month = day = -1;
104                 hour = minute = second = -1;
105                 serial = flight = -1;
106                 lat = lon = -1;
107                 has_gps = false;
108                 has_other_adc = false;
109                 has_rssi = false;
110                 for (AltosRecord record : iterable) {
111                         if (serial < 0)
112                                 serial = record.serial;
113                         if ((record.seen & AltosRecord.seen_flight) != 0 && flight < 0)
114                                 flight = record.flight;
115                         if ((record.seen & AltosRecord.seen_temp_volt) != 0)
116                                 has_other_adc = true;
117                         if (record.rssi != 0)
118                                 has_rssi = true;
119                         new_state = new AltosState(record, state);
120                         end_time = new_state.time;
121                         state = new_state;
122                         if (state.time >= boost_time && state.state < Altos.ao_flight_boost)
123                                 state.state = Altos.ao_flight_boost;
124                         if (state.time >= landed_time && state.state < Altos.ao_flight_landed)
125                                 state.state = Altos.ao_flight_landed;
126                         if (0 <= state.state && state.state < Altos.ao_flight_invalid) {
127                                 if (state.state >= Altos.ao_flight_boost) {
128                                         if (state.gps != null && state.gps.locked &&
129                                             year < 0) {
130                                                 year = state.gps.year;
131                                                 month = state.gps.month;
132                                                 day = state.gps.day;
133                                                 hour = state.gps.hour;
134                                                 minute = state.gps.minute;
135                                                 second = state.gps.second;
136                                         }
137                                 }
138                                 state_accel[state.state] += state.acceleration;
139                                 state_accel_speed[state.state] += state.accel_speed;
140                                 state_baro_speed[state.state] += state.baro_speed;
141                                 state_count[state.state]++;
142                                 if (state_start[state.state] == 0.0)
143                                         state_start[state.state] = state.time;
144                                 if (state_end[state.state] < state.time)
145                                         state_end[state.state] = state.time;
146                                 max_height = state.max_height;
147                                 if (state.max_accel_speed != 0)
148                                         max_speed = state.max_accel_speed;
149                                 else
150                                         max_speed = state.max_baro_speed;
151                                 max_acceleration = state.max_acceleration;
152                         }
153                         if (state.gps != null && state.gps.locked && state.gps.nsat >= 4) {
154                                 if (state.state <= Altos.ao_flight_pad) {
155                                         pad_lat = state.gps.lat;
156                                         pad_lon = state.gps.lon;
157                                 }
158                                 lat = state.gps.lat;
159                                 lon = state.gps.lon;
160                                 has_gps = true;
161                         }
162                 }
163                 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
164                         if (state_count[s] > 0) {
165                                 state_accel_speed[s] /= state_count[s];
166                                 state_baro_speed[s] /= state_count[s];
167                                 state_accel[s] /= state_count[s];
168                         }
169                         if (state_start[s] == 0)
170                                 state_start[s] = end_time;
171                         if (state_end[s] == 0)
172                                 state_end[s] = end_time;
173                 }
174         }
175 }