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(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(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(label, "%d°%lf'%c",
78 (int) int_part, min, sign);
82 static char *ascent_states[] = {
90 * Fill out the derived data fields
93 aoview_state_derive(struct aostate *state)
97 state->ground_altitude = aoview_pres_to_altitude(state->ground_pres);
98 state->height = aoview_pres_to_altitude(state->flight_pres) - state->ground_altitude;
99 state->acceleration = (state->ground_accel - state->flight_accel) / 27.0;
100 state->speed = state->flight_vel / 2700.0;
101 state->temperature = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
102 state->drogue_sense = state->drogue / 32767.0 * 15.0;
103 state->main_sense = state->main / 32767.0 * 15.0;
104 state->battery = state->batt / 32767.0 * 5.0;
105 if (!strcmp(state->state, "pad")) {
108 state->pad_lat_total += state->lat;
109 state->pad_lon_total += state->lon;
110 state->pad_alt_total += state->alt;
111 state->pad_lat = state->pad_lat_total / state->npad;
112 state->pad_lon = state->pad_lon_total / state->npad;
113 state->pad_alt = state->pad_alt_total / state->npad;
116 state->ascent = FALSE;
117 for (i = 0; ascent_states[i]; i++)
118 if (!strcmp(state->state, ascent_states[i]))
119 state->ascent = TRUE;
121 /* Only look at accelerometer data on the way up */
122 if (state->ascent && state->acceleration > state->max_acceleration)
123 state->max_acceleration = state->acceleration;
124 if (state->ascent && state->speed > state->max_speed)
125 state->max_speed = state->speed;
127 if (state->height > state->max_height)
128 state->max_height = state->height;
129 aoview_great_circle(state->pad_lat, state->pad_lon, state->lat, state->lon,
130 &state->distance, &state->bearing);
134 aoview_state_speak(struct aostate *state)
136 static char last_state[32];
138 gboolean report = FALSE;
140 static int last_tick;
141 static int last_altitude;
144 if (strcmp(state->state, last_state)) {
145 aoview_voice_speak("%s\n", state->state);
146 if (!strcmp(state->state, "drogue"))
147 aoview_voice_speak("apogee %d meters\n",
148 (int) state->max_height);
150 strcpy(last_state, state->state);
152 this_altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
153 this_tick = state->tick;
154 while (this_tick < last_tick)
156 if (strcmp(state->state, "pad") != 0) {
157 if (this_altitude / 1000 != last_altitude / 1000)
159 if (this_tick - last_tick >= 10 * 100)
163 aoview_voice_speak("%d meters\n",
166 aoview_voice_speak("%d meters per second\n",
167 state->flight_vel / 2700);
168 last_tick = state->tick;
169 last_altitude = this_altitude;
170 printf ("report at tick %d height %d\n",
171 state->tick, this_altitude);
176 aoview_state_notify(struct aostate *state)
178 aoview_state_derive(state);
179 aoview_table_start();
181 if (state->npad >= MIN_PAD_SAMPLES)
182 aoview_table_add_row("Ground state", "ready");
184 aoview_table_add_row("Ground state", "waiting for gps (%d)",
185 MIN_PAD_SAMPLES - state->npad);
186 aoview_table_add_row("Rocket state", "%s", state->state);
187 aoview_table_add_row("Callsign", "%s", state->callsign);
188 aoview_table_add_row("Rocket serial", "%d", state->serial);
190 aoview_table_add_row("RSSI", "%ddBm", state->rssi);
191 aoview_table_add_row("Height", "%dm", state->height);
192 aoview_table_add_row("Max height", "%dm", state->max_height);
193 aoview_table_add_row("Acceleration", "%gm/s²", state->acceleration);
194 aoview_table_add_row("Max acceleration", "%gm/s²", state->max_acceleration);
195 aoview_table_add_row("Speed", "%gm/s", state->speed);
196 aoview_table_add_row("Max Speed", "%gm/s", state->max_speed);
197 aoview_table_add_row("Temperature", "%g°C", state->temperature);
198 aoview_table_add_row("Battery", "%gV", state->battery);
199 aoview_table_add_row("Drogue", "%gV", state->drogue_sense);
200 aoview_table_add_row("Main", "%gV", state->main_sense);
201 aoview_table_add_row("Pad altitude", "%dm", state->ground_altitude);
202 aoview_table_add_row("Satellites", "%d", state->nsat);
204 aoview_state_add_deg("Latitude", state->lat, 'N', 'S');
205 aoview_state_add_deg("Longitude", state->lon, 'E', 'W');
206 aoview_table_add_row("GPS alt", "%d", state->alt);
207 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
208 state->gps_time.hour,
209 state->gps_time.minute,
210 state->gps_time.second);
211 aoview_table_add_row("GPS ground speed", "%fm/s %d°",
214 aoview_table_add_row("GPS climb rate", "%fm/s",
216 aoview_table_add_row("GPS precision", "%f(hdop) %dm(h) %dm(v)\n",
217 state->hdop, state->h_error, state->v_error);
218 aoview_table_add_row("Distance from pad", "%gm", state->distance);
219 aoview_table_add_row("Direction from pad", "%g°", state->bearing);
221 aoview_table_add_row("GPS", "unlocked");
224 aoview_state_add_deg("Pad latitude", state->pad_lat, 'N', 'S');
225 aoview_state_add_deg("Pad longitude", state->pad_lon, 'E', 'W');
226 aoview_table_add_row("Pad GPS alt", "%gm", state->pad_alt);
228 aoview_table_finish();
229 aoview_label_show(state);
230 aoview_state_speak(state);
234 aoview_state_new(void)
239 aoview_state_init(GladeXML *xml)
242 aoview_voice_speak("initializing rocket flight monitoring system\n");