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