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