356828c65a3b0bc1e58e063974e11bbe0e6a10a8
[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 void
77 aoview_state_notify(struct aostate *state)
78 {
79         int     altitude;
80         double  accel;
81         int     ticks;
82         double  dist;
83         double  bearing;
84         double  temp;
85         double  velocity;
86         double  battery;
87         double  drogue_sense, main_sense;
88         double  max_accel;
89
90         if (!strcmp(state->state, "pad")) {
91                 if (state->locked && npad_gps < NUM_PAD_SAMPLES) {
92                         pad_lat_total += state->lat;
93                         pad_lon_total += state->lon;
94                         pad_alt_total += state->alt;
95                         npad_gps++;
96                 }
97                 if (state->locked && npad_gps <= NUM_PAD_SAMPLES) {
98                         pad_lat = pad_lat_total / npad_gps;
99                         pad_lon = pad_lon_total / npad_gps;
100                         pad_alt = pad_alt_total / npad_gps;
101                 }
102                 min_pres = state->ground_pres;
103                 min_accel = state->ground_accel;
104         }
105         if (state->flight_pres < min_pres)
106                 min_pres = state->flight_pres;
107         if (state->flight_accel < min_accel)
108                 min_accel = state->flight_accel;
109         altitude = aoview_pres_to_altitude(state->flight_pres) - aoview_pres_to_altitude(state->ground_pres);
110         accel = (state->ground_accel - state->flight_accel) / 27.0;
111         velocity = state->flight_vel / 2700.0;
112         max_accel = (state->ground_accel - min_accel) / 27.0;
113         ticks = state->tick - prev_tick;
114         temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
115         battery = (state->batt / 32767.0 * 5.0);
116         drogue_sense = (state->drogue / 32767.0 * 15.0);
117         main_sense = (state->main / 32767.0 * 15.0);
118
119         prev_accel = accel;
120         prev_tick = state->tick;
121         aoview_table_start();
122
123         if (npad_gps >= NUM_PAD_SAMPLES)
124                 aoview_table_add_row("Ground state", "ready");
125         else
126                 aoview_table_add_row("Ground state", "waiting for gps (%d)",
127                                      NUM_PAD_SAMPLES - npad_gps);
128         aoview_table_add_row("Rocket state", "%s", state->state);
129         aoview_table_add_row("Callsign", "%s", state->callsign);
130         aoview_table_add_row("Rocket serial", "%d", state->serial);
131
132         aoview_table_add_row("RSSI", "%ddBm", state->rssi);
133         aoview_table_add_row("Height", "%dm", altitude);
134         aoview_table_add_row("Max height", "%dm",
135                              aoview_pres_to_altitude(min_pres) -
136                              aoview_pres_to_altitude(state->ground_pres));
137         aoview_table_add_row("Acceleration", "%gm/s²", accel);
138         aoview_table_add_row("Max acceleration", "%gm/s²", max_accel);
139         aoview_table_add_row("Velocity", "%gm/s", velocity);
140         aoview_table_add_row("Temperature", "%g°C", temp);
141         aoview_table_add_row("Battery", "%gV", battery);
142         aoview_table_add_row("Drogue", "%gV", drogue_sense);
143         aoview_table_add_row("Main", "%gV", main_sense);
144         aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(state->ground_pres));
145         aoview_table_add_row("Satellites", "%d", state->nsat);
146         if (state->locked) {
147                 aoview_state_add_deg("Latitude", state->lat, 'N', 'S');
148                 aoview_state_add_deg("Longitude", state->lon, 'E', 'W');
149                 aoview_table_add_row("GPS alt", "%d", state->alt);
150                 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
151                                      state->gps_time.hour,
152                                      state->gps_time.minute,
153                                      state->gps_time.second);
154                 aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
155                                     &dist, &bearing);
156                 aoview_table_add_row("Distance from pad", "%gm", dist * 1000);
157                 aoview_table_add_row("Direction from pad", "%g°", bearing);
158         } else {
159                 aoview_table_add_row("GPS", "unlocked");
160         }
161         if (npad_gps) {
162                 aoview_state_add_deg("Pad latitude", pad_lat, 'N', 'S');
163                 aoview_state_add_deg("Pad longitude", pad_lon, 'E', 'W');
164                 aoview_table_add_row("Pad GPS alt", "%gm", pad_alt);
165         }
166         aoview_table_finish();
167 }
168
169 void
170 aoview_state_new(void)
171 {
172         pad_lat_total = 0;
173         pad_lon_total = 0;
174         pad_alt_total = 0;
175         npad_gps = 0;
176         prev_tick = 0;
177         prev_accel = 0;
178         pad_lat = 0;
179         pad_lon = 0;
180         pad_alt = 0;
181         min_pres = 32767;
182         min_accel = 32767;
183 }
184
185 void
186 aoview_state_init(GladeXML *xml)
187 {
188         aoview_state_new();
189 }