Merge remote branch 'origin/master'
[fw/altos] / ao-tools / altosui / 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 data stream
20  */
21
22 package altosui;
23
24 import altosui.AltosTelemetry;
25 import altosui.AltosGPS;
26
27 public class AltosState {
28         AltosTelemetry data;
29
30         /* derived data */
31
32         double  report_time;
33
34         double  time_change;
35         int     tick;
36
37         int     state;
38         boolean ascent; /* going up? */
39
40         double  ground_altitude;
41         double  height;
42         double  speed;
43         double  acceleration;
44         double  battery;
45         double  temperature;
46         double  main_sense;
47         double  drogue_sense;
48         double  baro_speed;
49
50         double  max_height;
51         double  max_acceleration;
52         double  max_speed;
53
54         AltosGPS        gps;
55
56         double  pad_lat;
57         double  pad_lon;
58         double  pad_alt;
59         int     npad;
60         int     prev_npad;
61
62         AltosGreatCircle from_pad;
63
64         double  gps_height;
65
66         int     speak_tick;
67         double  speak_altitude;
68
69         static double
70         aoview_time()
71         {
72                 return System.currentTimeMillis() / 1000.0;
73         }
74
75         void init (AltosTelemetry cur, AltosState prev_state) {
76                 int             i;
77                 AltosTelemetry prev;
78                 double  accel_counts_per_mss;
79
80                 data = cur;
81
82                 ground_altitude = AltosConvert.cc_barometer_to_altitude(data.ground_pres);
83                 height = AltosConvert.cc_barometer_to_altitude(data.flight_pres) - ground_altitude;
84
85                 report_time = aoview_time();
86
87                 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
88                 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
89                 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
90                 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
91                 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
92                 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
93                 battery = AltosConvert.cc_battery_to_voltage(data.batt);
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                         gps = prev_state.gps;
102                         pad_lat = prev_state.pad_lat;
103                         pad_lon = prev_state.pad_lon;
104                         pad_alt = prev_state.pad_alt;
105                         max_height = prev_state.max_height;
106                         max_acceleration = prev_state.max_acceleration;
107                         max_speed = prev_state.max_speed;
108
109                         /* make sure the clock is monotonic */
110                         while (tick < prev_state.tick)
111                                 tick += 65536;
112
113                         time_change = (tick - prev_state.tick) / 100.0;
114
115                         /* compute barometric speed */
116
117                         double height_change = height - prev_state.height;
118                         if (time_change > 0)
119                                 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
120                         else
121                                 baro_speed = prev_state.baro_speed;
122                 } else {
123                         npad = 0;
124                         gps = null;
125                         baro_speed = 0;
126                         time_change = 0;
127                 }
128
129                 if (state == AltosTelemetry.ao_flight_pad) {
130                         if (data.gps != null && data.gps.gps_locked && data.gps.nsat >= 4) {
131                                 npad++;
132                                 if (npad > 1) {
133                                         /* filter pad position */
134                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
135                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
136                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
137                                 } else {
138                                         pad_lat = data.gps.lat;
139                                         pad_lon = data.gps.lon;
140                                         pad_alt = data.gps.alt;
141                                 }
142                         }
143                 }
144                 ascent = (AltosTelemetry.ao_flight_boost <= state &&
145                           state <= AltosTelemetry.ao_flight_coast);
146
147                 /* Only look at accelerometer data on the way up */
148                 if (ascent && acceleration > max_acceleration)
149                         max_acceleration = acceleration;
150                 if (ascent && speed > max_speed)
151                         max_speed = speed;
152
153                 if (height > max_height)
154                         max_height = height;
155                 if (data.gps != null) {
156                         if (gps == null || !gps.gps_locked || data.gps.gps_locked)
157                                 gps = data.gps;
158                         if (npad > 0 && gps.gps_locked)
159                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
160                 }
161                 if (npad > 0) {
162                         gps_height = gps.alt - pad_alt;
163                 } else {
164                         gps_height = 0;
165                 }
166         }
167
168         public AltosState(AltosTelemetry cur) {
169                 init(cur, null);
170         }
171
172         public AltosState (AltosTelemetry cur, AltosState prev) {
173                 init(cur, prev);
174         }
175 }