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