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