summaryrefslogtreecommitdiff
path: root/aoview/aoview_state.c
blob: 13b0f73b8c9ad07e3279974dfcdf04daf665814b (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright © 2009 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.
 */

#include "aoview.h"
#include <math.h>

static double	pad_lat_total;
static double	pad_lon_total;
static int	pad_alt_total;
static int	npad_gps;
static int	prev_tick;
static double	prev_accel;
static double	pad_lat;
static double	pad_lon;
static double	pad_alt;
static double	min_pres;
static double	min_accel;

#define NUM_PAD_SAMPLES	10

static void
aoview_great_circle (double start_lat, double start_lon,
		     double end_lat, double end_lon,
		     double *dist, double *bearing)
{
	double rad = M_PI / 180;
	double earth_radius = 6371.2;
	double lat1 = rad * start_lat;
	double lon1 = -rad * start_lon;
	double lat2 = rad * end_lat;
	double lon2 = -rad * end_lon;

	double d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2));
	double argacos = (sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1));
	double crs;
	if (sin(lon2-lon1) < 0)
		crs = acos(argacos);
	else
		crs = 2 * M_PI - acos(argacos);
	*dist = d * earth_radius;
	*bearing = crs * 180/M_PI;
}

static void
aoview_state_add_deg(char *label, double deg, char pos, char neg)
{
	double	int_part;
	double	min;
	char	sign = pos;

	if (deg < 0) {
		deg = -deg;
		sign = neg;
	}
	int_part = floor (deg);
	min = (deg - int_part) * 60.0;
	aoview_table_add_row(label, "%d°%lf'%c",
			     (int) int_part, min, sign);

}

static char *ascent_states[] = {
	"boost",
	"fast",
	"coast",
	0,
};

void
aoview_state_speak(struct aostate *state)
{
	static char	last_state[32];
	int		i;
	gboolean	report = FALSE;
	static time_t	last_time;
	time_t		this_time;
	static int	last_altitude;
	int		this_altitude;

	if (strcmp(state->state, last_state)) {
		aoview_voice_speak("rocket state now %s\n", state->state);
		if (!strcmp(state->state, "drogue"))
			aoview_voice_speak("maximum altitude %d meters\n",
					   aoview_pres_to_altitude(min_pres) -
					   aoview_pres_to_altitude(state->ground_pres));
		report = TRUE;
		strcpy(last_state, state->state);
	}
	this_time = time(NULL);
	this_altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
	if (this_time - last_time >= 10)
		report = TRUE;
	if (this_altitude / 1000 != last_altitude / 1000)
		report = TRUE;

	if (report) {
		aoview_voice_speak("altitude %d meters\n",
				   this_altitude);
		for (i = 0; ascent_states[i]; i++)
			if (!strcmp(ascent_states[i], state->state)) {
				aoview_voice_speak("speed %d meters per second\n",
						   state->flight_vel / 2700);
				break;
			}
	}

	last_time = this_time;
	last_altitude = this_altitude;
}

void
aoview_state_notify(struct aostate *state)
{
	int	altitude;
	double	accel;
	int	ticks;
	double	dist;
	double	bearing;
	double	temp;
	double	velocity;
	double	battery;
	double	drogue_sense, main_sense;
	double	max_accel;

	if (!strcmp(state->state, "pad")) {
		if (state->locked && npad_gps < NUM_PAD_SAMPLES) {
			pad_lat_total += state->lat;
			pad_lon_total += state->lon;
			pad_alt_total += state->alt;
			npad_gps++;
		}
		if (state->locked && npad_gps <= NUM_PAD_SAMPLES) {
			pad_lat = pad_lat_total / npad_gps;
			pad_lon = pad_lon_total / npad_gps;
			pad_alt = pad_alt_total / npad_gps;
		}
		min_pres = state->ground_pres;
		min_accel = state->ground_accel;
	}
	if (state->flight_pres < min_pres)
		min_pres = state->flight_pres;
	if (state->flight_accel < min_accel)
		min_accel = state->flight_accel;
	altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
	accel = (state->ground_accel - state->flight_accel) / 27.0;
	velocity = state->flight_vel / 2700.0;
	max_accel = (state->ground_accel - min_accel) / 27.0;
	ticks = state->tick - prev_tick;
	temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
	battery = (state->batt / 32767.0 * 5.0);
	drogue_sense = (state->drogue / 32767.0 * 15.0);
	main_sense = (state->main / 32767.0 * 15.0);

	prev_accel = accel;
	prev_tick = state->tick;
	aoview_table_start();

	if (npad_gps >= NUM_PAD_SAMPLES)
		aoview_table_add_row("Ground state", "ready");
	else
		aoview_table_add_row("Ground state", "waiting for gps (%d)",
				     NUM_PAD_SAMPLES - npad_gps);
	aoview_table_add_row("Rocket state", "%s", state->state);
	aoview_table_add_row("Callsign", "%s", state->callsign);
	aoview_table_add_row("Rocket serial", "%d", state->serial);

	aoview_table_add_row("RSSI", "%ddBm", state->rssi);
	aoview_table_add_row("Height", "%dm", altitude);
	aoview_table_add_row("Max height", "%dm",
			     aoview_pres_to_altitude(min_pres) -
			     aoview_pres_to_altitude(state->ground_pres));
	aoview_table_add_row("Acceleration", "%gm/s²", accel);
	aoview_table_add_row("Max acceleration", "%gm/s²", max_accel);
	aoview_table_add_row("Velocity", "%gm/s", velocity);
	aoview_table_add_row("Temperature", "%g°C", temp);
	aoview_table_add_row("Battery", "%gV", battery);
	aoview_table_add_row("Drogue", "%gV", drogue_sense);
	aoview_table_add_row("Main", "%gV", main_sense);
	aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(state->ground_pres));
	aoview_table_add_row("Satellites", "%d", state->nsat);
	if (state->locked) {
		aoview_state_add_deg("Latitude", state->lat, 'N', 'S');
		aoview_state_add_deg("Longitude", state->lon, 'E', 'W');
		aoview_table_add_row("GPS alt", "%d", state->alt);
		aoview_table_add_row("GPS time", "%02d:%02d:%02d",
				     state->gps_time.hour,
				     state->gps_time.minute,
				     state->gps_time.second);
		aoview_table_add_row("GPS ground speed", "%fm/s %d°",
				     state->ground_speed,
				     state->course);
		aoview_table_add_row("GPS climb rate", "%fm/s",
				     state->climb_rate);
		aoview_table_add_row("GPS precision", "%f(hdop) %dm(h) %dm(v)\n",
				     state->hdop, state->h_error, state->v_error);
		aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
				    &dist, &bearing);
		aoview_table_add_row("Distance from pad", "%gm", dist * 1000);
		aoview_table_add_row("Direction from pad", "%g°", bearing);
	} else {
		aoview_table_add_row("GPS", "unlocked");
	}
	if (npad_gps) {
		aoview_state_add_deg("Pad latitude", pad_lat, 'N', 'S');
		aoview_state_add_deg("Pad longitude", pad_lon, 'E', 'W');
		aoview_table_add_row("Pad GPS alt", "%gm", pad_alt);
	}
	aoview_table_finish();
	aoview_state_speak(state);
}

void
aoview_state_new(void)
{
	pad_lat_total = 0;
	pad_lon_total = 0;
	pad_alt_total = 0;
	npad_gps = 0;
	prev_tick = 0;
	prev_accel = 0;
	pad_lat = 0;
	pad_lon = 0;
	pad_alt = 0;
	min_pres = 32767;
	min_accel = 32767;
}

void
aoview_state_init(GladeXML *xml)
{
	aoview_state_new();
	aoview_voice_speak("initializing rocket flight monitoring system\n");
}