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