altosui: Add raw pressure to the AltosUI graph
[fw/altos] / altoslib / 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 org.altusmetrum.altoslib_1;
23
24 public class AltosState {
25         public AltosRecord data;
26
27         /* derived data */
28
29         public long     report_time;
30
31         public double   time;
32         public double   time_change;
33         public int      tick;
34
35         public int      state;
36         public boolean  landed;
37         public boolean  ascent; /* going up? */
38         public boolean boost;   /* under power */
39
40         public double   ground_altitude;
41         public double   altitude;
42         public double   height;
43         public double   pressure;
44         public double   acceleration;
45         public double   battery;
46         public double   temperature;
47         public double   main_sense;
48         public double   drogue_sense;
49         public double   accel_speed;
50         public double   baro_speed;
51
52         public double   max_height;
53         public double   max_acceleration;
54         public double   max_accel_speed;
55         public double   max_baro_speed;
56
57         public AltosGPS gps;
58         public int gps_sequence;
59
60         public AltosIMU imu;
61         public AltosMag mag;
62
63         public static final int MIN_PAD_SAMPLES = 10;
64
65         public int      npad;
66         public int      ngps;
67         public int      gps_waiting;
68         public boolean  gps_ready;
69
70         public AltosGreatCircle from_pad;
71         public double   elevation;      /* from pad */
72         public double   range;          /* total distance */
73
74         public double   gps_height;
75
76         public double pad_lat, pad_lon, pad_alt;
77
78         public int      speak_tick;
79         public double   speak_altitude;
80
81         public double speed() {
82                 if (ascent)
83                         return accel_speed;
84                 else
85                         return baro_speed;
86         }
87
88         public double max_speed() {
89                 if (max_accel_speed != 0)
90                         return max_accel_speed;
91                 return max_baro_speed;
92         }
93
94         public void init (AltosRecord cur, AltosState prev_state) {
95                 data = cur;
96
97                 /* Discard previous state if it was for a different board */
98                 if (prev_state != null && prev_state.data.serial != data.serial)
99                         prev_state = null;
100                 ground_altitude = data.ground_altitude();
101
102                 altitude = data.altitude();
103                 if (altitude == AltosRecord.MISSING && data.gps != null)
104                         altitude = data.gps.alt;
105
106                 height = AltosRecord.MISSING;
107                 if (data.kalman_height != AltosRecord.MISSING)
108                         height = data.kalman_height;
109                 else {
110                         if (altitude != AltosRecord.MISSING && ground_altitude != AltosRecord.MISSING) {
111                                 double  cur_height = altitude - ground_altitude;
112                                 if (prev_state == null || prev_state.height == AltosRecord.MISSING)
113                                         height = cur_height;
114                                 else
115                                         height = (prev_state.height * 15 + cur_height) / 16.0;
116                         }
117                 }
118
119                 report_time = System.currentTimeMillis();
120
121                 if (data.kalman_acceleration != AltosRecord.MISSING)
122                         acceleration = data.kalman_acceleration;
123                 else
124                         acceleration = data.acceleration();
125                 temperature = data.temperature();
126                 drogue_sense = data.drogue_voltage();
127                 main_sense = data.main_voltage();
128                 battery = data.battery_voltage();
129                 pressure = data.pressure();
130                 tick = data.tick;
131                 state = data.state;
132
133                 if (prev_state != null) {
134
135                         /* Preserve any existing gps data */
136                         npad = prev_state.npad;
137                         ngps = prev_state.ngps;
138                         gps = prev_state.gps;
139                         gps_sequence = prev_state.gps_sequence;
140                         pad_lat = prev_state.pad_lat;
141                         pad_lon = prev_state.pad_lon;
142                         pad_alt = prev_state.pad_alt;
143                         max_height = prev_state.max_height;
144                         max_acceleration = prev_state.max_acceleration;
145                         max_accel_speed = prev_state.max_accel_speed;
146                         max_baro_speed = prev_state.max_baro_speed;
147                         imu = prev_state.imu;
148                         mag = prev_state.mag;
149
150                         /* make sure the clock is monotonic */
151                         while (tick < prev_state.tick)
152                                 tick += 65536;
153
154                         time_change = (tick - prev_state.tick) / 100.0;
155
156                         if (data.kalman_speed != AltosRecord.MISSING) {
157                                 baro_speed = accel_speed = data.kalman_speed;
158                         } else {
159                                 /* compute barometric speed */
160
161                                 double height_change = height - prev_state.height;
162
163                                 double prev_baro_speed = prev_state.baro_speed;
164                                 if (prev_baro_speed == AltosRecord.MISSING)
165                                         prev_baro_speed = 0;
166
167                                 if (time_change > 0)
168                                         baro_speed = (prev_baro_speed * 3 + (height_change / time_change)) / 4.0;
169                                 else
170                                         baro_speed = prev_state.baro_speed;
171
172                                 double prev_accel_speed = prev_state.accel_speed;
173
174                                 if (prev_accel_speed == AltosRecord.MISSING)
175                                         prev_accel_speed = 0;
176
177                                 if (acceleration == AltosRecord.MISSING) {
178                                         /* Fill in mising acceleration value */
179                                         accel_speed = baro_speed;
180
181                                         if (time_change > 0 && accel_speed != AltosRecord.MISSING)
182                                                 acceleration = (accel_speed - prev_accel_speed) / time_change;
183                                         else
184                                                 acceleration = prev_state.acceleration;
185                                 } else {
186                                         /* compute accelerometer speed */
187                                         accel_speed = prev_accel_speed + acceleration * time_change;
188                                 }
189                         }
190                 } else {
191                         npad = 0;
192                         ngps = 0;
193                         gps = null;
194                         gps_sequence = 0;
195                         baro_speed = AltosRecord.MISSING;
196                         accel_speed = AltosRecord.MISSING;
197                         pad_alt = AltosRecord.MISSING;
198                         max_baro_speed = 0;
199                         max_accel_speed = 0;
200                         max_height = 0;
201                         max_acceleration = 0;
202                         time_change = 0;
203                 }
204
205                 time = tick / 100.0;
206
207                 if (data.gps != null && data.gps_sequence != gps_sequence && (state < AltosLib.ao_flight_boost)) {
208
209                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
210                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
211                                 npad++;
212                         else
213                                 npad = 0;
214
215                         /* Average GPS data while on the pad */
216                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
217                                 if (ngps > 1 && state == AltosLib.ao_flight_pad) {
218                                         /* filter pad position */
219                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
220                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
221                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
222                                 } else {
223                                         pad_lat = data.gps.lat;
224                                         pad_lon = data.gps.lon;
225                                         pad_alt = data.gps.alt;
226                                 }
227                                 ngps++;
228                         }
229                 } else {
230                         if (ngps == 0 && ground_altitude != AltosRecord.MISSING)
231                                 pad_alt = ground_altitude;
232                 }
233
234                 gps_sequence = data.gps_sequence;
235
236                 gps_waiting = MIN_PAD_SAMPLES - npad;
237                 if (gps_waiting < 0)
238                         gps_waiting = 0;
239
240                 gps_ready = gps_waiting == 0;
241
242                 ascent = (AltosLib.ao_flight_boost <= state &&
243                           state <= AltosLib.ao_flight_coast);
244                 boost = (AltosLib.ao_flight_boost == state);
245
246                 /* Only look at accelerometer data under boost */
247                 if (boost && acceleration != AltosRecord.MISSING && (max_acceleration == AltosRecord.MISSING || acceleration > max_acceleration))
248                         max_acceleration = acceleration;
249                 if (boost && accel_speed != AltosRecord.MISSING && accel_speed > max_accel_speed)
250                         max_accel_speed = accel_speed;
251                 if (boost && baro_speed != AltosRecord.MISSING && baro_speed > max_baro_speed)
252                         max_baro_speed = baro_speed;
253
254                 if (height != AltosRecord.MISSING && height > max_height)
255                         max_height = height;
256                 elevation = 0;
257                 range = -1;
258                 gps_height = 0;
259                 if (data.gps != null) {
260                         gps = data.gps;
261                         if (ngps > 0 && gps.locked) {
262                                 double h = height;
263
264                                 if (h == AltosRecord.MISSING) h = 0;
265                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
266                                 elevation = from_pad.elevation;
267                                 range = from_pad.range;
268                                 gps_height = gps.alt - pad_alt;
269                         }
270                 }
271         }
272
273         public AltosState(AltosRecord cur) {
274                 init(cur, null);
275         }
276
277         public AltosState (AltosRecord cur, AltosState prev) {
278                 init(cur, prev);
279         }
280 }