Steal C code from ao-view
[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         AltosTelemetry prev_data;
30
31         /* derived data */
32
33         double  report_time;
34
35         int     state;
36         boolean ascent; /* going up? */
37
38         double  ground_altitude;
39         double  height;
40         double  speed;
41         double  acceleration;
42         double  battery;
43         double  temperature;
44         double  main_sense;
45         double  drogue_sense;
46         double  baro_speed;
47
48         double  max_height;
49         double  max_acceleration;
50         double  max_speed;
51
52         AltosGPS        gps;
53         AltosGPSTracking        gps_tracking;
54
55         boolean gps_valid;
56         double  pad_lat;
57         double  pad_lon;
58         double  pad_alt;
59         double  pad_lat_total;
60         double  pad_lon_total;
61         double  pad_alt_total;
62         int     npad;
63         int     prev_npad;
64
65         AltosGreatCircle from_pad;
66
67         double  gps_height;
68
69         int     speak_tick;
70         double  speak_altitude;
71
72         static double
73         aoview_time()
74         {
75                 return System.currentTimeMillis() / 1000.0;
76         }
77
78         public AltosState (AltosTelemetry cur, AltosTelemetry prev, int prev_npad) {
79                 int     i;
80                 double  new_height;
81                 double  height_change;
82                 double  time_change;
83                 double  accel_counts_per_mss;
84                 int     tick_count;
85
86                 data = cur;
87                 prev_data = prev;
88                 npad = prev_npad;
89                 tick_count = data.tick;
90                 if (tick_count < prev_data.tick)
91                         tick_count += 65536;
92                 time_change = (tick_count - prev_data.tick) / 100.0;
93
94                 report_time = aoview_time();
95
96                 ground_altitude = AltosConvert.cc_pressure_to_altitude(data.ground_pres);
97                 new_height = AltosConvert.cc_pressure_to_altitude(data.flight_pres) - ground_altitude;
98                 height_change = new_height - height;
99                 height = new_height;
100                 if (time_change > 0)
101                         baro_speed = (baro_speed * 3 + (height_change / time_change)) / 4.0;
102                 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
103                 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
104                 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
105                 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
106                 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
107                 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
108                 battery = AltosConvert.cc_battery_to_voltage(data.batt);
109                 state = data.state();
110                 if (state == AltosTelemetry.ao_flight_pad) {
111                         if (data.gps.gps_locked && data.gps.nsat >= 4) {
112                                 npad++;
113                                 pad_lat_total += data.gps.lat;
114                                 pad_lon_total += data.gps.lon;
115                                 pad_alt_total += data.gps.alt;
116                                 if (npad > 1) {
117                                         pad_lat = (pad_lat * 31 + data.gps.lat) / 32.0;
118                                         pad_lon = (pad_lon * 31 + data.gps.lon) / 32.0;
119                                         pad_alt = (pad_alt * 31 + data.gps.alt) / 32.0;
120                                 } else {
121                                         pad_lat = data.gps.lat;
122                                         pad_lon = data.gps.lon;
123                                         pad_alt = data.gps.alt;
124                                 }
125                         }
126                 }
127                 ascent = (AltosTelemetry.ao_flight_boost <= state &&
128                           state <= AltosTelemetry.ao_flight_coast);
129
130                 /* Only look at accelerometer data on the way up */
131                 if (ascent && acceleration > max_acceleration)
132                         max_acceleration = acceleration;
133                 if (ascent && speed > max_speed)
134                         max_speed = speed;
135
136                 if (height > max_height)
137                         max_height = height;
138                 gps.gps_locked = data.gps.gps_locked;
139                 gps.gps_connected = data.gps.gps_connected;
140                 if (data.gps.gps_locked) {
141                         gps = data.gps;
142                         gps_valid = true;
143                         if (npad > 0)
144                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
145                 }
146                 if (npad > 0) {
147                         gps_height = gps.alt - pad_alt;
148                 } else {
149                         gps_height = 0;
150                 }
151         }
152
153         public AltosState(AltosTelemetry cur) {
154                 this(cur, cur, 0);
155         }
156
157         public AltosState (AltosTelemetry cur, AltosState prev) {
158                 this(cur, prev.data, prev.npad);
159                 if (gps == null) {
160                         gps = prev.gps;
161                         gps_valid = prev.gps_valid;
162                 }
163                 if (gps_tracking == null)
164                         gps_tracking = prev.gps_tracking;
165         }
166 }