Use flite to announce flight state
[fw/altos] / aoview / aoview_state.c
1 /*
2  * Copyright © 2009 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 #include "aoview.h"
19 #include <math.h>
20
21 static double   pad_lat_total;
22 static double   pad_lon_total;
23 static int      pad_alt_total;
24 static int      npad_gps;
25 static int      prev_tick;
26 static double   prev_accel;
27 static double   pad_lat;
28 static double   pad_lon;
29 static double   pad_alt;
30 static double   min_pres;
31 static double   min_accel;
32
33 #define NUM_PAD_SAMPLES 10
34
35 static void
36 aoview_great_circle (double start_lat, double start_lon,
37                      double end_lat, double end_lon,
38                      double *dist, double *bearing)
39 {
40         double rad = M_PI / 180;
41         double earth_radius = 6371.2;
42         double lat1 = rad * start_lat;
43         double lon1 = -rad * start_lon;
44         double lat2 = rad * end_lat;
45         double lon2 = -rad * end_lon;
46
47         double d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2));
48         double argacos = (sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1));
49         double crs;
50         if (sin(lon2-lon1) < 0)
51                 crs = acos(argacos);
52         else
53                 crs = 2 * M_PI - acos(argacos);
54         *dist = d * earth_radius;
55         *bearing = crs * 180/M_PI;
56 }
57
58 static void
59 aoview_state_add_deg(char *label, double deg, char pos, char neg)
60 {
61         double  int_part;
62         double  min;
63         char    sign = pos;
64
65         if (deg < 0) {
66                 deg = -deg;
67                 sign = neg;
68         }
69         int_part = floor (deg);
70         min = (deg - int_part) * 60.0;
71         aoview_table_add_row(label, "%d°%lf'%c",
72                              (int) int_part, min, sign);
73
74 }
75
76 static char *ascent_states[] = {
77         "boost",
78         "fast",
79         "coast",
80         0,
81 };
82
83 void
84 aoview_state_speak(struct aostate *state)
85 {
86         static char     last_state[32];
87         int             i;
88         gboolean        report = FALSE;
89         static time_t   last_time;
90         time_t          this_time;
91         static int      last_altitude;
92         int             this_altitude;
93
94         if (strcmp(state->state, last_state)) {
95                 aoview_voice_speak("rocket state now %s\n", state->state);
96                 if (!strcmp(state->state, "drogue"))
97                         aoview_voice_speak("maximum altitude %d meters\n",
98                                            aoview_pres_to_altitude(min_pres) -
99                                            aoview_pres_to_altitude(state->ground_pres));
100                 report = TRUE;
101                 strcpy(last_state, state->state);
102         }
103         this_time = time(NULL);
104         this_altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
105         if (this_time - last_time >= 10)
106                 report = TRUE;
107         if (this_altitude / 1000 != last_altitude / 1000)
108                 report = TRUE;
109
110         if (report) {
111                 aoview_voice_speak("altitude %d meters\n",
112                                    this_altitude);
113                 for (i = 0; ascent_states[i]; i++)
114                         if (!strcmp(ascent_states[i], state->state)) {
115                                 aoview_voice_speak("speed %d meters per second\n",
116                                                    state->flight_vel / 2700);
117                                 break;
118                         }
119         }
120
121         last_time = this_time;
122         last_altitude = this_altitude;
123 }
124
125 void
126 aoview_state_notify(struct aostate *state)
127 {
128         int     altitude;
129         double  accel;
130         int     ticks;
131         double  dist;
132         double  bearing;
133         double  temp;
134         double  velocity;
135         double  battery;
136         double  drogue_sense, main_sense;
137         double  max_accel;
138
139         if (!strcmp(state->state, "pad")) {
140                 if (state->locked && npad_gps < NUM_PAD_SAMPLES) {
141                         pad_lat_total += state->lat;
142                         pad_lon_total += state->lon;
143                         pad_alt_total += state->alt;
144                         npad_gps++;
145                 }
146                 if (state->locked && npad_gps <= NUM_PAD_SAMPLES) {
147                         pad_lat = pad_lat_total / npad_gps;
148                         pad_lon = pad_lon_total / npad_gps;
149                         pad_alt = pad_alt_total / npad_gps;
150                 }
151                 min_pres = state->ground_pres;
152                 min_accel = state->ground_accel;
153         }
154         if (state->flight_pres < min_pres)
155                 min_pres = state->flight_pres;
156         if (state->flight_accel < min_accel)
157                 min_accel = state->flight_accel;
158         altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
159         accel = (state->ground_accel - state->flight_accel) / 27.0;
160         velocity = state->flight_vel / 2700.0;
161         max_accel = (state->ground_accel - min_accel) / 27.0;
162         ticks = state->tick - prev_tick;
163         temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
164         battery = (state->batt / 32767.0 * 5.0);
165         drogue_sense = (state->drogue / 32767.0 * 15.0);
166         main_sense = (state->main / 32767.0 * 15.0);
167
168         prev_accel = accel;
169         prev_tick = state->tick;
170         aoview_table_start();
171
172         if (npad_gps >= NUM_PAD_SAMPLES)
173                 aoview_table_add_row("Ground state", "ready");
174         else
175                 aoview_table_add_row("Ground state", "waiting for gps (%d)",
176                                      NUM_PAD_SAMPLES - npad_gps);
177         aoview_table_add_row("Rocket state", "%s", state->state);
178         aoview_table_add_row("Callsign", "%s", state->callsign);
179         aoview_table_add_row("Rocket serial", "%d", state->serial);
180
181         aoview_table_add_row("RSSI", "%ddBm", state->rssi);
182         aoview_table_add_row("Height", "%dm", altitude);
183         aoview_table_add_row("Max height", "%dm",
184                              aoview_pres_to_altitude(min_pres) -
185                              aoview_pres_to_altitude(state->ground_pres));
186         aoview_table_add_row("Acceleration", "%gm/s²", accel);
187         aoview_table_add_row("Max acceleration", "%gm/s²", max_accel);
188         aoview_table_add_row("Velocity", "%gm/s", velocity);
189         aoview_table_add_row("Temperature", "%g°C", temp);
190         aoview_table_add_row("Battery", "%gV", battery);
191         aoview_table_add_row("Drogue", "%gV", drogue_sense);
192         aoview_table_add_row("Main", "%gV", main_sense);
193         aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(state->ground_pres));
194         aoview_table_add_row("Satellites", "%d", state->nsat);
195         if (state->locked) {
196                 aoview_state_add_deg("Latitude", state->lat, 'N', 'S');
197                 aoview_state_add_deg("Longitude", state->lon, 'E', 'W');
198                 aoview_table_add_row("GPS alt", "%d", state->alt);
199                 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
200                                      state->gps_time.hour,
201                                      state->gps_time.minute,
202                                      state->gps_time.second);
203                 aoview_table_add_row("GPS ground speed", "%fm/s %d°",
204                                      state->ground_speed,
205                                      state->course);
206                 aoview_table_add_row("GPS climb rate", "%fm/s",
207                                      state->climb_rate);
208                 aoview_table_add_row("GPS precision", "%f(hdop) %dm(h) %dm(v)\n",
209                                      state->hdop, state->h_error, state->v_error);
210                 aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
211                                     &dist, &bearing);
212                 aoview_table_add_row("Distance from pad", "%gm", dist * 1000);
213                 aoview_table_add_row("Direction from pad", "%g°", bearing);
214         } else {
215                 aoview_table_add_row("GPS", "unlocked");
216         }
217         if (npad_gps) {
218                 aoview_state_add_deg("Pad latitude", pad_lat, 'N', 'S');
219                 aoview_state_add_deg("Pad longitude", pad_lon, 'E', 'W');
220                 aoview_table_add_row("Pad GPS alt", "%gm", pad_alt);
221         }
222         aoview_table_finish();
223         aoview_state_speak(state);
224 }
225
226 void
227 aoview_state_new(void)
228 {
229         pad_lat_total = 0;
230         pad_lon_total = 0;
231         pad_alt_total = 0;
232         npad_gps = 0;
233         prev_tick = 0;
234         prev_accel = 0;
235         pad_lat = 0;
236         pad_lon = 0;
237         pad_alt = 0;
238         min_pres = 32767;
239         min_accel = 32767;
240 }
241
242 void
243 aoview_state_init(GladeXML *xml)
244 {
245         aoview_state_new();
246         aoview_voice_speak("initializing rocket flight monitoring system\n");
247 }