Move libaltos to top level
[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;
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                 ground_altitude = data.ground_altitude();
96
97                 altitude = data.altitude();
98
99                 if (data.kalman_height != AltosRecord.MISSING)
100                         height = data.kalman_height;
101                 else {
102                         if (prev_state != null)
103                                 height = (prev_state.height * 15 + altitude - ground_altitude) / 16.0;
104                 }
105
106                 report_time = System.currentTimeMillis();
107
108                 if (data.kalman_acceleration != AltosRecord.MISSING)
109                         acceleration = data.kalman_acceleration;
110                 else
111                         acceleration = data.acceleration();
112                 temperature = data.temperature();
113                 drogue_sense = data.drogue_voltage();
114                 main_sense = data.main_voltage();
115                 battery = data.battery_voltage();
116                 tick = data.tick;
117                 state = data.state;
118
119                 if (prev_state != null) {
120
121                         /* Preserve any existing gps data */
122                         npad = prev_state.npad;
123                         ngps = prev_state.ngps;
124                         gps = prev_state.gps;
125                         pad_lat = prev_state.pad_lat;
126                         pad_lon = prev_state.pad_lon;
127                         pad_alt = prev_state.pad_alt;
128                         max_height = prev_state.max_height;
129                         max_acceleration = prev_state.max_acceleration;
130                         max_accel_speed = prev_state.max_accel_speed;
131                         max_baro_speed = prev_state.max_baro_speed;
132                         imu = prev_state.imu;
133                         mag = prev_state.mag;
134
135                         /* make sure the clock is monotonic */
136                         while (tick < prev_state.tick)
137                                 tick += 65536;
138
139                         time_change = (tick - prev_state.tick) / 100.0;
140
141                         if (data.kalman_speed != AltosRecord.MISSING) {
142                                 baro_speed = accel_speed = data.kalman_speed;
143                         } else {
144                                 /* compute barometric speed */
145
146                                 double height_change = height - prev_state.height;
147                                 if (time_change > 0)
148                                         baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
149                                 else
150                                         baro_speed = prev_state.baro_speed;
151
152                                 if (acceleration == AltosRecord.MISSING) {
153                                         /* Fill in mising acceleration value */
154                                         accel_speed = baro_speed;
155                                         if (time_change > 0)
156                                                 acceleration = (accel_speed - prev_state.accel_speed) / time_change;
157                                         else
158                                                 acceleration = prev_state.acceleration;
159                                 } else {
160                                         /* compute accelerometer speed */
161                                         accel_speed = prev_state.accel_speed + acceleration * time_change;
162                                 }
163                         }
164
165                 } else {
166                         npad = 0;
167                         ngps = 0;
168                         gps = null;
169                         baro_speed = 0;
170                         accel_speed = 0;
171                         time_change = 0;
172                         if (acceleration == AltosRecord.MISSING)
173                                 acceleration = 0;
174                 }
175
176                 time = tick / 100.0;
177
178                 if (cur.new_gps && (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_idle)) {
179
180                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
181                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
182                                 npad++;
183                         else
184                                 npad = 0;
185
186                         /* Average GPS data while on the pad */
187                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
188                                 if (ngps > 1) {
189                                         /* filter pad position */
190                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
191                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
192                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
193                                 } else {
194                                         pad_lat = data.gps.lat;
195                                         pad_lon = data.gps.lon;
196                                         pad_alt = data.gps.alt;
197                                 }
198                                 ngps++;
199                         }
200                 } else
201                         pad_alt = ground_altitude;
202
203                 data.new_gps = false;
204
205                 gps_waiting = MIN_PAD_SAMPLES - npad;
206                 if (gps_waiting < 0)
207                         gps_waiting = 0;
208
209                 gps_ready = gps_waiting == 0;
210
211                 ascent = (AltosLib.ao_flight_boost <= state &&
212                           state <= AltosLib.ao_flight_coast);
213                 boost = (AltosLib.ao_flight_boost == state);
214
215                 /* Only look at accelerometer data under boost */
216                 if (boost && acceleration > max_acceleration && acceleration != AltosRecord.MISSING)
217                         max_acceleration = acceleration;
218                 if (boost && accel_speed > max_accel_speed && accel_speed != AltosRecord.MISSING)
219                         max_accel_speed = accel_speed;
220                 if (boost && baro_speed > max_baro_speed && baro_speed != AltosRecord.MISSING)
221                         max_baro_speed = baro_speed;
222
223                 if (height > max_height && height != AltosRecord.MISSING)
224                         max_height = height;
225                 if (data.gps != null) {
226                         if (gps == null || !gps.locked || data.gps.locked)
227                                 gps = data.gps;
228                         if (ngps > 0 && gps.locked) {
229                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
230                         }
231                 }
232                 elevation = 0;
233                 range = -1;
234                 if (ngps > 0) {
235                         gps_height = gps.alt - pad_alt;
236                         if (from_pad != null) {
237                                 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
238                                 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
239                         }
240                 } else {
241                         gps_height = 0;
242                 }
243         }
244
245         public AltosState(AltosRecord cur) {
246                 init(cur, null);
247         }
248
249         public AltosState (AltosRecord cur, AltosState prev) {
250                 init(cur, prev);
251         }
252 }