altoslib: Clean up random rebase failures
[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                 }
164
165                 gps_waiting = MIN_PAD_SAMPLES - npad;
166                 if (gps_waiting < 0)
167                         gps_waiting = 0;
168
169                 gps_ready = gps_waiting == 0;
170
171                 ascent = (AltosLib.ao_flight_boost <= state &&
172                           state <= AltosLib.ao_flight_coast);
173                 boost = (AltosLib.ao_flight_boost == state);
174
175                 /* Only look at accelerometer data under boost */
176                 if (boost && acceleration > max_acceleration)
177                         max_acceleration = acceleration;
178                 if (boost && speed > max_speed)
179                         max_speed = speed;
180                 if (boost && baro_speed > max_baro_speed)
181                         max_baro_speed = baro_speed;
182
183                 if (height > max_height)
184                         max_height = height;
185                 if (data.gps != null) {
186                         if (gps == null || !gps.locked || data.gps.locked)
187                                 gps = data.gps;
188                         if (ngps > 0 && gps.locked) {
189                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
190                         }
191                 }
192                 elevation = 0;
193                 range = -1;
194                 if (ngps > 0) {
195                         gps_height = gps.alt - pad_alt;
196                         if (from_pad != null) {
197                                 elevation = Math.atan2(height, from_pad.distance) * 180 / Math.PI;
198                                 range = Math.sqrt(height * height + from_pad.distance * from_pad.distance);
199                         }
200                 } else {
201                         gps_height = 0;
202                 }
203         }
204
205         public AltosState(AltosRecord cur) {
206                 init(cur, null);
207         }
208
209         public AltosState (AltosRecord cur, AltosState prev) {
210                 init(cur, prev);
211         }
212 }