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