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