deeb4c771dcec9fb825d918bea922f75c3ff4024
[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 ascent; /* going up? */
39
40         double  ground_altitude;
41         double  height;
42         double  speed;
43         double  acceleration;
44         double  battery;
45         double  temperature;
46         double  main_sense;
47         double  drogue_sense;
48         double  baro_speed;
49
50         double  max_height;
51         double  max_acceleration;
52         double  max_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     gps_waiting;
64         boolean gps_ready;
65
66         AltosGreatCircle from_pad;
67
68         double  gps_height;
69
70         int     speak_tick;
71         double  speak_altitude;
72
73
74         void init (AltosRecord cur, AltosState prev_state) {
75                 int             i;
76                 AltosRecord prev;
77
78                 data = cur;
79
80                 ground_altitude = data.ground_altitude();
81                 height = data.altitude() - ground_altitude;
82
83                 report_time = System.currentTimeMillis();
84
85                 acceleration = data.acceleration();
86                 speed = data.accel_speed();
87                 temperature = data.temperature();
88                 drogue_sense = data.drogue_voltage();
89                 main_sense = data.main_voltage();
90                 battery = data.battery_voltage();
91                 tick = data.tick;
92                 state = data.state;
93
94                 if (prev_state != null) {
95
96                         /* Preserve any existing gps data */
97                         npad = prev_state.npad;
98                         gps = prev_state.gps;
99                         pad_lat = prev_state.pad_lat;
100                         pad_lon = prev_state.pad_lon;
101                         pad_alt = prev_state.pad_alt;
102                         max_height = prev_state.max_height;
103                         max_acceleration = prev_state.max_acceleration;
104                         max_speed = prev_state.max_speed;
105
106                         /* make sure the clock is monotonic */
107                         while (tick < prev_state.tick)
108                                 tick += 65536;
109
110                         time_change = (tick - prev_state.tick) / 100.0;
111
112                         /* compute barometric speed */
113
114                         double height_change = height - prev_state.height;
115                         if (time_change > 0)
116                                 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
117                         else
118                                 baro_speed = prev_state.baro_speed;
119                 } else {
120                         npad = 0;
121                         gps = null;
122                         baro_speed = 0;
123                         time_change = 0;
124                 }
125
126                 if (state == Altos.ao_flight_pad) {
127                         if (data.gps == null)
128                                 System.out.printf("on pad, gps null\n");
129                         else
130                                 System.out.printf ("on pad gps lat %f lon %f locked %d nsat %d\n",
131                                                    data.gps.lat, data.gps.lon, data.gps.locked ? 1 : 0, data.gps.nsat);
132                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
133                                 npad++;
134                                 if (npad > 1) {
135                                         /* filter pad position */
136                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
137                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
138                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
139                                 } else {
140                                         pad_lat = data.gps.lat;
141                                         pad_lon = data.gps.lon;
142                                         pad_alt = data.gps.alt;
143                                 }
144                         } else {
145                                 npad = 0;
146                         }
147                 }
148
149                 gps_waiting = MIN_PAD_SAMPLES - npad;
150                 if (gps_waiting < 0)
151                         gps_waiting = 0;
152
153                 gps_ready = gps_waiting == 0;
154
155                 ascent = (Altos.ao_flight_boost <= state &&
156                           state <= Altos.ao_flight_coast);
157
158                 /* Only look at accelerometer data on the way up */
159                 if (ascent && acceleration > max_acceleration)
160                         max_acceleration = acceleration;
161                 if (ascent && speed > max_speed)
162                         max_speed = speed;
163
164                 if (height > max_height)
165                         max_height = height;
166                 if (data.gps != null) {
167                         if (gps == null || !gps.locked || data.gps.locked)
168                                 gps = data.gps;
169                         if (npad > 0 && gps.locked)
170                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
171                 }
172                 if (npad > 0) {
173                         gps_height = gps.alt - pad_alt;
174                 } else {
175                         gps_height = 0;
176                 }
177         }
178
179         public AltosState(AltosRecord cur) {
180                 init(cur, null);
181         }
182
183         public AltosState (AltosRecord cur, AltosState prev) {
184                 init(cur, prev);
185         }
186 }