summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosState.java
blob: da465c75d8a8b01dc993502747ece147c793796f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

/*
 * Track flight state from telemetry data stream
 */

package altosui;

import altosui.AltosTelemetry;
import altosui.AltosGPS;

public class AltosState {
	AltosTelemetry data;
	AltosTelemetry prev_data;

	/* derived data */

	double  report_time;

	int	state;
	boolean	ascent;	/* going up? */

	double	ground_altitude;
	double	height;
	double	speed;
	double	acceleration;
	double	battery;
	double	temperature;
	double	main_sense;
	double	drogue_sense;
	double	baro_speed;

	double	max_height;
	double	max_acceleration;
	double	max_speed;

	AltosGPS	gps;
	AltosGPSTracking	gps_tracking;

	boolean	gps_valid;
	double	pad_lat;
	double	pad_lon;
	double	pad_alt;
	double	pad_lat_total;
	double	pad_lon_total;
	double	pad_alt_total;
	int	npad;
	int	prev_npad;

	AltosGreatCircle from_pad;

	double	gps_height;

	int	speak_tick;
	double	speak_altitude;

	static double
	aoview_time()
	{
		return System.currentTimeMillis() / 1000.0;
	}

	public AltosState (AltosTelemetry cur, AltosTelemetry prev, int prev_npad) {
		int	i;
		double	new_height;
		double	height_change;
		double	time_change;
		double	accel_counts_per_mss;
		int	tick_count;

		data = cur;
		prev_data = prev;
		npad = prev_npad;
		tick_count = data.tick;
		if (tick_count < prev_data.tick)
			tick_count += 65536;
		time_change = (tick_count - prev_data.tick) / 100.0;

		report_time = aoview_time();

		ground_altitude = AltosConvert.cc_pressure_to_altitude(data.ground_pres);
		new_height = AltosConvert.cc_pressure_to_altitude(data.flight_pres) - ground_altitude;
		height_change = new_height - height;
		height = new_height;
		if (time_change > 0)
			baro_speed = (baro_speed * 3 + (height_change / time_change)) / 4.0;
		accel_counts_per_mss = ((data.accel_minus_g - data.accel_plus_g) / 2.0) / 9.80665;
		acceleration = (data.ground_accel - data.flight_accel) / accel_counts_per_mss;
		speed = data.flight_vel / (accel_counts_per_mss * 100.0);
		temperature = AltosConvert.cc_thermometer_to_temperature(data.temp);
		drogue_sense = AltosConvert.cc_ignitor_to_voltage(data.drogue);
		main_sense = AltosConvert.cc_ignitor_to_voltage(data.main);
		battery = AltosConvert.cc_battery_to_voltage(data.batt);
		state = data.state();
		if (state == AltosTelemetry.ao_flight_pad) {
			if (data.gps.gps_locked && data.gps.nsat >= 4) {
				npad++;
				pad_lat_total += data.gps.lat;
				pad_lon_total += data.gps.lon;
				pad_alt_total += data.gps.alt;
				if (npad > 1) {
					pad_lat = (pad_lat * 31 + data.gps.lat) / 32.0;
					pad_lon = (pad_lon * 31 + data.gps.lon) / 32.0;
					pad_alt = (pad_alt * 31 + data.gps.alt) / 32.0;
				} else {
					pad_lat = data.gps.lat;
					pad_lon = data.gps.lon;
					pad_alt = data.gps.alt;
				}
			}
		}
		ascent = (AltosTelemetry.ao_flight_boost <= state &&
			  state <= AltosTelemetry.ao_flight_coast);

		/* Only look at accelerometer data on the way up */
		if (ascent && acceleration > max_acceleration)
			max_acceleration = acceleration;
		if (ascent && speed > max_speed)
			max_speed = speed;

		if (height > max_height)
			max_height = height;
		gps.gps_locked = data.gps.gps_locked;
		gps.gps_connected = data.gps.gps_connected;
		if (data.gps.gps_locked) {
			gps = data.gps;
			gps_valid = true;
			if (npad > 0)
				from_pad = new AltosGreatCircle(pad_lat, pad_lon, gps.lat, gps.lon);
		}
		if (npad > 0) {
			gps_height = gps.alt - pad_alt;
		} else {
			gps_height = 0;
		}
	}

	public AltosState(AltosTelemetry cur) {
		this(cur, cur, 0);
	}

	public AltosState (AltosTelemetry cur, AltosState prev) {
		this(cur, prev.data, prev.npad);
		if (gps == null) {
			gps = prev.gps;
			gps_valid = prev.gps_valid;
		}
		if (gps_tracking == null)
			gps_tracking = prev.gps_tracking;
	}
}