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