Fix status update
[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  time_change;
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         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         void init (AltosTelemetry cur, AltosTelemetry prev, int prev_npad) {
79                 int     i;
80                 double  new_height;
81                 double  height_change;
82                 double  accel_counts_per_mss;
83                 int     tick_count;
84
85                 data = cur;
86                 prev_data = prev;
87                 npad = prev_npad;
88                 tick_count = data.tick;
89                 if (tick_count < prev_data.tick)
90                         tick_count += 65536;
91                 time_change = (tick_count - prev_data.tick) / 100.0;
92
93                 report_time = aoview_time();
94
95                 ground_altitude = AltosConvert.cc_pressure_to_altitude(data.ground_pres);
96                 new_height = AltosConvert.cc_pressure_to_altitude(data.flight_pres) - ground_altitude;
97                 height_change = new_height - height;
98                 height = new_height;
99                 if (time_change > 0)
100                         baro_speed = (baro_speed * 3 + (height_change / time_change)) / 4.0;
101                 accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
102                 acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
103                 speed = data.flight_vel / (accel_counts_per_mss * 100.0);
104                 temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
105                 drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
106                 main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
107                 battery = AltosConvert.cc_battery_to_voltage(data.batt);
108                 state = data.state();
109                 if (state == AltosTelemetry.ao_flight_pad) {
110                         if (data.gps != null && data.gps.gps_locked && data.gps.nsat >= 4) {
111                                 npad++;
112                                 pad_lat_total += data.gps.lat;
113                                 pad_lon_total += data.gps.lon;
114                                 pad_alt_total += data.gps.alt;
115                                 if (npad > 1) {
116                                         pad_lat = (pad_lat * 31 + data.gps.lat) / 32.0;
117                                         pad_lon = (pad_lon * 31 + data.gps.lon) / 32.0;
118                                         pad_alt = (pad_alt * 31 + data.gps.alt) / 32.0;
119                                 } else {
120                                         pad_lat = data.gps.lat;
121                                         pad_lon = data.gps.lon;
122                                         pad_alt = data.gps.alt;
123                                 }
124                         }
125                 }
126                 ascent = (AltosTelemetry.ao_flight_boost <= state &&
127                           state <= AltosTelemetry.ao_flight_coast);
128
129                 /* Only look at accelerometer data on the way up */
130                 if (ascent && acceleration > max_acceleration)
131                         max_acceleration = acceleration;
132                 if (ascent && speed > max_speed)
133                         max_speed = speed;
134
135                 if (height > max_height)
136                         max_height = height;
137                 if (data.gps != null) {
138                         gps = data.gps;
139                         if (npad > 0 && gps.gps_locked)
140                                 from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
141                 }
142                 if (npad > 0) {
143                         gps_height = gps.alt - pad_alt;
144                 } else {
145                         gps_height = 0;
146                 }
147         }
148
149         public AltosState(AltosTelemetry cur) {
150                 init(cur, cur, 0);
151         }
152
153         public AltosState (AltosTelemetry cur, AltosState prev) {
154                 if (prev == null)
155                         init(cur, cur, 0);
156                 else {
157                         init(cur, prev.data, prev.npad);
158                         if (gps == null)
159                                 gps = prev.gps;
160                 }
161         }
162 }