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