2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 static inline double sqr(double a) { return a * a; };
24 aoview_great_circle (double start_lat, double start_lon,
25 double end_lat, double end_lon,
26 double *dist, double *bearing)
28 const double rad = M_PI / 180;
29 const double earth_radius = 6371.2 * 1000; /* in meters */
30 double lat1 = rad * start_lat;
31 double lon1 = rad * -start_lon;
32 double lat2 = rad * end_lat;
33 double lon2 = rad * -end_lon;
35 double d_lat = lat2 - lat1;
36 double d_lon = lon2 - lon1;
38 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
39 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
40 sqr(cos(lat1) * sin(lat2) -
41 sin(lat1) * cos(lat2) * cos(d_lon)));
42 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
43 double d = atan2(vdn,vdd);
46 if (cos(lat1) < 1e-20) {
55 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
57 if (sin(lon2-lon1) > 0)
58 course = 2 * M_PI-course;
60 *dist = d * earth_radius;
61 *bearing = course * 180/M_PI;
65 aoview_state_add_deg(int column, char *label, double deg, char pos, char neg)
75 int_part = floor (deg);
76 min = (deg - int_part) * 60.0;
77 aoview_table_add_row(column, label, "%d°%lf'%c",
78 (int) int_part, min, sign);
82 static char *ascent_states[] = {
94 clock_gettime(CLOCK_MONOTONIC, &now);
95 return (double) now.tv_sec + (double) now.tv_nsec / 1.0e9;
99 * Fill out the derived data fields
102 aoview_state_derive(struct cc_telem *data, struct aostate *state)
106 double height_change;
108 double accel_counts_per_mss;
111 state->report_time = aoview_time();
113 state->prev_data = state->data;
114 state->prev_npad = state->npad;
116 tick_count = data->tick;
117 if (tick_count < state->prev_data.tick)
119 time_change = (tick_count - state->prev_data.tick) / 100.0;
121 state->ground_altitude = aoview_pres_to_altitude(data->ground_pres);
122 new_height = aoview_pres_to_altitude(data->flight_pres) - state->ground_altitude;
123 height_change = new_height - state->height;
124 state->height = new_height;
126 state->baro_speed = (state->baro_speed * 3 + (height_change / time_change)) / 4.0;
127 accel_counts_per_mss = ((data->accel_minus_g - data->accel_plus_g) / 2.0) / 9.80665;
128 state->acceleration = (data->ground_accel - data->flight_accel) / accel_counts_per_mss;
129 state->speed = data->flight_vel / (accel_counts_per_mss * 100.0);
130 state->temperature = cc_thermometer_to_temperature(data->temp);
131 state->drogue_sense = cc_ignitor_to_voltage(data->drogue);
132 state->main_sense = cc_ignitor_to_voltage(data->main);
133 state->battery = cc_battery_to_voltage(data->batt);
134 if (!strcmp(data->state, "pad")) {
135 if (data->gps.gps_locked && data->gps.nsat >= 4) {
137 state->pad_lat_total += data->gps.lat;
138 state->pad_lon_total += data->gps.lon;
139 state->pad_alt_total += data->gps.alt;
140 if (state->npad > 1) {
141 state->pad_lat = (state->pad_lat * 31 + data->gps.lat) / 32.0;
142 state->pad_lon = (state->pad_lon * 31 + data->gps.lon) / 32.0;
143 state->pad_alt = (state->pad_alt * 31 + data->gps.alt) / 32.0;
145 state->pad_lat = data->gps.lat;
146 state->pad_lon = data->gps.lon;
147 state->pad_alt = data->gps.alt;
151 state->ascent = FALSE;
152 for (i = 0; ascent_states[i]; i++)
153 if (!strcmp(data->state, ascent_states[i]))
154 state->ascent = TRUE;
156 /* Only look at accelerometer data on the way up */
157 if (state->ascent && state->acceleration > state->max_acceleration)
158 state->max_acceleration = state->acceleration;
159 if (state->ascent && state->speed > state->max_speed)
160 state->max_speed = state->speed;
162 if (state->height > state->max_height)
163 state->max_height = state->height;
164 state->gps.gps_locked = data->gps.gps_locked;
165 state->gps.gps_connected = data->gps.gps_connected;
166 if (data->gps.gps_locked) {
167 state->gps = data->gps;
168 state->gps_valid = 1;
170 aoview_great_circle(state->pad_lat, state->pad_lon, state->gps.lat, state->gps.lon,
171 &state->distance, &state->bearing);
173 if (data->gps_tracking.channels)
174 state->gps_tracking = data->gps_tracking;
176 state->gps_height = state->gps.alt - state->pad_alt;
178 state->gps_height = 0;
183 aoview_speak_state(struct aostate *state)
185 if (strcmp(state->data.state, state->prev_data.state)) {
186 aoview_voice_speak("%s\n", state->data.state);
187 if (!strcmp(state->data.state, "drogue"))
188 aoview_voice_speak("apogee %d meters\n",
189 (int) state->max_height);
190 if (!strcmp(state->prev_data.state, "boost"))
191 aoview_voice_speak("max speed %d meters per second\n",
192 (int) state->max_speed);
194 if (state->prev_npad < MIN_PAD_SAMPLES && state->npad >= MIN_PAD_SAMPLES)
195 aoview_voice_speak("g p s ready\n");
199 aoview_speak_height(struct aostate *state)
201 aoview_voice_speak("%d meters\n", state->height);
204 struct aostate aostate;
206 static guint aostate_timeout;
208 #define COMPASS_LIMIT(n) ((n * 22.5) + 22.5/2)
210 static char *compass_points[] = {
230 aoview_compass_point(double bearing)
235 while (bearing >= 360.0)
238 i = floor ((bearing - 22.5/2) / 22.5 + 0.5);
240 if (i >= sizeof (compass_points) / sizeof (compass_points[0]))
242 return compass_points[i];
246 aoview_state_timeout(gpointer data)
248 double now = aoview_time();
250 if (strlen(aostate.data.state) > 0 && strcmp(aostate.data.state, "pad") != 0)
251 aoview_speak_height(&aostate);
252 if (now - aostate.report_time >= 20 || !strcmp(aostate.data.state, "landed")) {
253 if (!aostate.ascent) {
254 if (fabs(aostate.baro_speed) < 20 && aostate.height < 100)
255 aoview_voice_speak("rocket landed safely\n");
257 aoview_voice_speak("rocket may have crashed\n");
258 if (aostate.gps_valid) {
259 aoview_voice_speak("rocket reported %s of pad distance %d meters\n",
260 aoview_compass_point(aostate.bearing),
261 (int) aostate.distance);
271 aoview_state_reset(void)
273 memset(&aostate, '\0', sizeof (aostate));
277 aoview_state_notify(struct cc_telem *data)
279 struct aostate *state = &aostate;
280 aoview_state_derive(data, state);
281 aoview_table_start();
283 if (state->npad >= MIN_PAD_SAMPLES)
284 aoview_table_add_row(0, "Ground state", "ready");
286 aoview_table_add_row(0, "Ground state", "waiting for gps (%d)",
287 MIN_PAD_SAMPLES - state->npad);
288 aoview_table_add_row(0, "Rocket state", "%s", state->data.state);
289 aoview_table_add_row(0, "Callsign", "%s", state->data.callsign);
290 aoview_table_add_row(0, "Rocket serial", "%d", state->data.serial);
291 aoview_table_add_row(0, "Rocket flight", "%d", state->data.flight);
293 aoview_table_add_row(0, "RSSI", "%6ddBm", state->data.rssi);
294 aoview_table_add_row(0, "Height", "%6dm", state->height);
295 aoview_table_add_row(0, "Max height", "%6dm", state->max_height);
296 aoview_table_add_row(0, "Acceleration", "%7.1fm/s²", state->acceleration);
297 aoview_table_add_row(0, "Max acceleration", "%7.1fm/s²", state->max_acceleration);
298 aoview_table_add_row(0, "Speed", "%7.1fm/s", state->ascent ? state->speed : state->baro_speed);
299 aoview_table_add_row(0, "Max Speed", "%7.1fm/s", state->max_speed);
300 aoview_table_add_row(0, "Temperature", "%6.2f°C", state->temperature);
301 aoview_table_add_row(0, "Battery", "%5.2fV", state->battery);
302 aoview_table_add_row(0, "Drogue", "%5.2fV", state->drogue_sense);
303 aoview_table_add_row(0, "Main", "%5.2fV", state->main_sense);
304 aoview_table_add_row(0, "Pad altitude", "%dm", state->ground_altitude);
305 aoview_table_add_row(1, "Satellites", "%d", state->gps.nsat);
306 if (state->gps.gps_locked) {
307 aoview_table_add_row(1, "GPS", "locked");
308 } else if (state->gps.gps_connected) {
309 aoview_table_add_row(1, "GPS", "unlocked");
311 aoview_table_add_row(1, "GPS", "not available");
313 if (state->gps_valid) {
314 aoview_state_add_deg(1, "Latitude", state->gps.lat, 'N', 'S');
315 aoview_state_add_deg(1, "Longitude", state->gps.lon, 'E', 'W');
316 aoview_table_add_row(1, "GPS altitude", "%d", state->gps.alt);
317 aoview_table_add_row(1, "GPS height", "%d", state->gps_height);
318 aoview_table_add_row(1, "GPS date", "%04d-%02d-%02d",
319 state->gps.gps_time.year,
320 state->gps.gps_time.month,
321 state->gps.gps_time.day);
322 aoview_table_add_row(1, "GPS time", "%02d:%02d:%02d",
323 state->gps.gps_time.hour,
324 state->gps.gps_time.minute,
325 state->gps.gps_time.second);
327 if (state->gps.gps_extended) {
328 aoview_table_add_row(1, "GPS ground speed", "%7.1fm/s %d°",
329 state->gps.ground_speed,
331 aoview_table_add_row(1, "GPS climb rate", "%7.1fm/s",
332 state->gps.climb_rate);
333 aoview_table_add_row(1, "GPS precision", "%4.1f(hdop) %3dm(h) %3dm(v)",
334 state->gps.hdop, state->gps.h_error, state->gps.v_error);
337 aoview_table_add_row(1, "Distance from pad", "%5.0fm", state->distance);
338 aoview_table_add_row(1, "Direction from pad", "%4.0f°", state->bearing);
339 aoview_state_add_deg(1, "Pad latitude", state->pad_lat, 'N', 'S');
340 aoview_state_add_deg(1, "Pad longitude", state->pad_lon, 'E', 'W');
341 aoview_table_add_row(1, "Pad GPS alt", "%gm", state->pad_alt);
343 if (state->gps.gps_connected) {
347 aoview_table_add_row(2, "Satellites Visible", "%d", state->gps_tracking.channels);
348 for (c = 0; c < state->gps_tracking.channels; c++) {
349 aoview_table_add_row(2, "Satellite id,C/N0",
351 state->gps_tracking.sats[c].svid,
352 state->gps_tracking.sats[c].c_n0);
355 aoview_table_finish();
356 aoview_label_show(state);
357 aoview_speak_state(state);
358 if (!aostate_timeout && strcmp(state->data.state, "pad") != 0)
359 aostate_timeout = g_timeout_add_seconds(10, aoview_state_timeout, NULL);
363 aoview_state_new(void)
368 aoview_state_init(GladeXML *xml)