altosui: Add ascent, descent and landed tabs
[fw/altos] / ao-tools / altosui / AltosState.java
1 /*
2  * Copyright © 2010 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 /*
19  * Track flight state from telemetry or eeprom data stream
20  */
21
22 package altosui;
23
24 import altosui.AltosRecord;
25 import altosui.AltosGPS;
26
27 public class AltosState {
28         AltosRecord data;
29
30         /* derived data */
31
32         long    report_time;
33
34         double  time_change;
35         int     tick;
36
37         int     state;
38         boolean landed;
39         boolean ascent; /* going up? */
40
41         double  ground_altitude;
42         double  height;
43         double  speed;
44         double  acceleration;
45         double  battery;
46         double  temperature;
47         double  main_sense;
48         double  drogue_sense;
49         double  baro_speed;
50
51         double  max_height;
52         double  max_acceleration;
53         double  max_speed;
54
55         AltosGPS        gps;
56
57         double  pad_lat;
58         double  pad_lon;
59         double  pad_alt;
60
61         static final int MIN_PAD_SAMPLES = 10;
62
63         int     npad;
64         int     ngps;
65         int     gps_waiting;
66         boolean gps_ready;
67
68         AltosGreatCircle from_pad;
69         double  elevation;      /* from pad */
70         double  range;          /* total distance */
71
72         double  gps_height;
73
74         int     speak_tick;
75         double  speak_altitude;
76
77
78         void init (AltosRecord cur, AltosState prev_state) {
79                 int             i;
80                 AltosRecord prev;
81
82                 data = cur;
83
84                 ground_altitude = data.ground_altitude();
85                 height = data.filtered_altitude() - ground_altitude;
86
87                 report_time = System.currentTimeMillis();
88
89                 acceleration = data.acceleration();
90                 speed = data.accel_speed();
91                 temperature = data.temperature();
92                 drogue_sense = data.drogue_voltage();
93                 main_sense = data.main_voltage();
94                 battery = data.battery_voltage();
95                 tick = data.tick;
96                 state = data.state;
97
98                 if (prev_state != null) {
99
100                         /* Preserve any existing gps data */
101                         npad = prev_state.npad;
102                         ngps = prev_state.ngps;
103                         gps = prev_state.gps;
104                         pad_lat = prev_state.pad_lat;
105                         pad_lon = prev_state.pad_lon;
106                         pad_alt = prev_state.pad_alt;
107                         max_height = prev_state.max_height;
108                         max_acceleration = prev_state.max_acceleration;
109                         max_speed = prev_state.max_speed;
110
111                         /* make sure the clock is monotonic */
112                         while (tick < prev_state.tick)
113                                 tick += 65536;
114
115                         time_change = (tick - prev_state.tick) / 100.0;
116
117                         /* compute barometric speed */
118
119                         double height_change = height - prev_state.height;
120                         if (time_change > 0)
121                                 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
122                         else
123                                 baro_speed = prev_state.baro_speed;
124                 } else {
125                         npad = 0;
126                         ngps = 0;
127                         gps = null;
128                         baro_speed = 0;
129                         time_change = 0;
130                 }
131
132                 if (state == Altos.ao_flight_pad) {
133
134                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
135                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
136                                 npad++;
137                         else
138                                 npad = 0;
139
140                         /* Average GPS data while on the pad */
141                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
142                                 if (ngps > 1) {
143                                         /* filter pad position */
144                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
145                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
146                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
147                                 } else {
148                                         pad_lat = data.gps.lat;
149                                         pad_lon = data.gps.lon;
150                                         pad_alt = data.gps.alt;
151                                 }
152                                 ngps++;
153                         }
154                 }
155
156                 gps_waiting = MIN_PAD_SAMPLES - npad;
157                 if (gps_waiting < 0)
158                         gps_waiting = 0;
159
160                 gps_ready = gps_waiting == 0;
161
162                 ascent = (Altos.ao_flight_boost <= state &&
163                           state <= Altos.ao_flight_coast);
164
165                 /* Only look at accelerometer data on the way up */
166                 if (ascent && acceleration > max_acceleration)
167                         max_acceleration = acceleration;
168                 if (ascent && speed > max_speed)
169                         max_speed = speed;
170
171                 if (height > max_height)
172                         max_height = height;
173                 if (data.gps != null) {
174                         if (gps == null || !gps.locked || data.gps.locked)
175                                 gps = data.gps;
176                         if (ngps > 0 && gps.locked) {
177                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
178                         }
179                 }
180                 elevation = 0;
181                 range = -1;
182                 if (ngps > 0) {
183                         gps_height = gps.alt - pad_alt;
184                         if (from_pad != null) {
185                                 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
186                                 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
187                         }
188                 } else {
189                         gps_height = 0;
190                 }
191         }
192
193         public AltosState(AltosRecord cur) {
194                 init(cur, null);
195         }
196
197         public AltosState (AltosRecord cur, AltosState prev) {
198                 init(cur, prev);
199         }
200 }