Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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         long    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
70         void init (AltosTelemetry cur, AltosState prev_state) {
71                 int             i;
72                 AltosTelemetry prev;
73                 double  accel_counts_per_mss;
74
75                 data = cur;
76
77                 ground_altitude = AltosConvert.cc_barometer_to_altitude(data.ground_pres);
78                 height = AltosConvert.cc_barometer_to_altitude(data.flight_pres) - ground_altitude;
79
80                 report_time = System.currentTimeMillis();
81
82                 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
83                 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
84                 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
85                 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
86                 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
87                 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
88                 battery = AltosConvert.cc_battery_to_voltage(data.batt);
89                 tick = data.tick;
90                 state = data.state();
91
92                 if (prev_state != null) {
93
94                         /* Preserve any existing gps data */
95                         npad = prev_state.npad;
96                         gps = prev_state.gps;
97                         pad_lat = prev_state.pad_lat;
98                         pad_lon = prev_state.pad_lon;
99                         pad_alt = prev_state.pad_alt;
100                         max_height = prev_state.max_height;
101                         max_acceleration = prev_state.max_acceleration;
102                         max_speed = prev_state.max_speed;
103
104                         /* make sure the clock is monotonic */
105                         while (tick < prev_state.tick)
106                                 tick += 65536;
107
108                         time_change = (tick - prev_state.tick) / 100.0;
109
110                         /* compute barometric speed */
111
112                         double height_change = height - prev_state.height;
113                         if (time_change > 0)
114                                 baro_speed = (prev_state.baro_speed * 3 + (height_change / time_change)) / 4.0;
115                         else
116                                 baro_speed = prev_state.baro_speed;
117                 } else {
118                         npad = 0;
119                         gps = null;
120                         baro_speed = 0;
121                         time_change = 0;
122                 }
123
124                 if (state == AltosTelemetry.ao_flight_pad) {
125                         if (data.gps != null && data.gps.gps_locked && data.gps.nsat >= 4) {
126                                 npad++;
127                                 if (npad > 1) {
128                                         /* filter pad position */
129                                         pad_lat = (pad_lat * 31.0 + data.gps.lat) / 32.0;
130                                         pad_lon = (pad_lon * 31.0 + data.gps.lon) / 32.0;
131                                         pad_alt = (pad_alt * 31.0 + data.gps.alt) / 32.0;
132                                 } else {
133                                         pad_lat = data.gps.lat;
134                                         pad_lon = data.gps.lon;
135                                         pad_alt = data.gps.alt;
136                                 }
137                         }
138                 }
139                 ascent = (AltosTelemetry.ao_flight_boost <= state &&
140                           state <= AltosTelemetry.ao_flight_coast);
141
142                 /* Only look at accelerometer data on the way up */
143                 if (ascent && acceleration > max_acceleration)
144                         max_acceleration = acceleration;
145                 if (ascent && speed > max_speed)
146                         max_speed = speed;
147
148                 if (height > max_height)
149                         max_height = height;
150                 if (data.gps != null) {
151                         if (gps == null || !gps.gps_locked || data.gps.gps_locked)
152                                 gps = data.gps;
153                         if (npad > 0 && gps.gps_locked)
154                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
155                 }
156                 if (npad > 0) {
157                         gps_height = gps.alt - pad_alt;
158                 } else {
159                         gps_height = 0;
160                 }
161         }
162
163         public AltosState(AltosTelemetry cur) {
164                 init(cur, null);
165         }
166
167         public AltosState (AltosTelemetry cur, AltosState prev) {
168                 init(cur, prev);
169         }
170 }