altosui: Move AltosState.java to altoslib
[fw/altos] / altosui / altoslib / src / org / altusmetrum / 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   height;
42         public double   speed;
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   baro_speed;
49
50         public double   max_height;
51         public double   max_acceleration;
52         public double   max_speed;
53         public double   max_baro_speed;
54
55         public AltosGPS gps;
56
57         public double   pad_lat;
58         public double   pad_lon;
59         public double   pad_alt;
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 int      speak_tick;
75         public double   speak_altitude;
76
77         public void init (AltosRecord cur, AltosState prev_state) {
78                 int             i;
79                 AltosRecord prev;
80
81                 data = cur;
82
83                 ground_altitude = data.ground_altitude();
84                 height = data.filtered_height();
85
86                 report_time = System.currentTimeMillis();
87
88                 acceleration = data.acceleration();
89                 speed = data.accel_speed();
90                 temperature = data.temperature();
91                 drogue_sense = data.drogue_voltage();
92                 main_sense = data.main_voltage();
93                 battery = data.battery_voltage();
94                 tick = data.tick;
95                 state = data.state;
96
97                 if (prev_state != null) {
98
99                         /* Preserve any existing gps data */
100                         npad = prev_state.npad;
101                         ngps = prev_state.ngps;
102                         gps = prev_state.gps;
103                         pad_lat = prev_state.pad_lat;
104                         pad_lon = prev_state.pad_lon;
105                         pad_alt = prev_state.pad_alt;
106                         max_height = prev_state.max_height;
107                         max_acceleration = prev_state.max_acceleration;
108                         max_speed = prev_state.max_speed;
109                         max_baro_speed = prev_state.max_baro_speed;
110
111                         /* make sure the clock is monotonic */
112                         while (tick < prev_state.tick)
113                                 tick += 65536;
114
115                         time_change = (tick - prev_state.tick) / 100.0;
116
117                         /* compute barometric speed */
118
119                         double height_change = height - prev_state.height;
120                         if (data.speed != AltosRecord.MISSING)
121                                 baro_speed = data.speed;
122                         else {
123                                 if (time_change > 0)
124                                         baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
125                                 else
126                                         baro_speed = prev_state.baro_speed;
127                         }
128                 } else {
129                         npad = 0;
130                         ngps = 0;
131                         gps = null;
132                         baro_speed = 0;
133                         time_change = 0;
134                 }
135
136                 time = tick / 100.0;
137
138                 if (cur.new_gps && (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_idle)) {
139
140                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
141                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4)
142                                 npad++;
143                         else
144                                 npad = 0;
145
146                         /* Average GPS data while on the pad */
147                         if (data.gps != null && data.gps.locked && data.gps.nsat >= 4) {
148                                 if (ngps > 1) {
149                                         /* filter pad position */
150                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
151                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
152                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
153                                 } else {
154                                         pad_lat = data.gps.lat;
155                                         pad_lon = data.gps.lon;
156                                         pad_alt = data.gps.alt;
157                                 }
158                                 ngps++;
159                         }
160                 }
161
162                 gps_waiting = MIN_PAD_SAMPLES - npad;
163                 if (gps_waiting < 0)
164                         gps_waiting = 0;
165
166                 gps_ready = gps_waiting == 0;
167
168                 ascent = (AltosLib.ao_flight_boost <= state &&
169                           state <= AltosLib.ao_flight_coast);
170                 boost = (AltosLib.ao_flight_boost == state);
171
172                 /* Only look at accelerometer data under boost */
173                 if (boost && acceleration > max_acceleration)
174                         max_acceleration = acceleration;
175                 if (boost && speed > max_speed)
176                         max_speed = speed;
177                 if (boost && baro_speed > max_baro_speed)
178                         max_baro_speed = baro_speed;
179
180                 if (height > max_height)
181                         max_height = height;
182                 if (data.gps != null) {
183                         if (gps == null || !gps.locked || data.gps.locked)
184                                 gps = data.gps;
185                         if (ngps > 0 && gps.locked) {
186                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
187                         }
188                 }
189                 elevation = 0;
190                 range = -1;
191                 if (ngps > 0) {
192                         gps_height = gps.alt - pad_alt;
193                         if (from_pad != null) {
194                                 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
195                                 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
196                         }
197                 } else {
198                         gps_height = 0;
199                 }
200         }
201
202         public AltosState(AltosRecord cur) {
203                 init(cur, null);
204         }
205
206         public AltosState (AltosRecord cur, AltosState prev) {
207                 init(cur, prev);
208         }
209 }