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