Add pad lat/lon, max accel, max height
[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 int      pad_pres;
22 static int      pad_accel;
23
24 static int      pad_pres_total;
25 static int      pad_accel_total;
26 static double   pad_lat_total;
27 static double   pad_lon_total;
28 static int      pad_alt_total;
29 static int      npad;
30 static int      npad_gps;
31 static int      prev_tick;
32 static double   prev_accel;
33 static double   velocity;
34 static double   pad_lat;
35 static double   pad_lon;
36 static double   pad_alt;
37 static double   min_pres;
38 static double   min_accel;
39
40 #define NUM_PAD_SAMPLES 10
41
42 static void
43 aoview_great_circle (double start_lat, double start_lon,
44                      double end_lat, double end_lon,
45                      double *dist, double *bearing)
46 {
47         double rad = M_PI / 180;
48         double earth_radius = 6371.2;
49         double lat1 = rad * start_lat;
50         double lon1 = -rad * start_lon;
51         double lat2 = rad * end_lat;
52         double lon2 = -rad * end_lon;
53
54         double d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2));
55         double argacos = (sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1));
56         double crs;
57         if (sin(lon2-lon1) < 0)
58                 crs = acos(argacos);
59         else
60                 crs = 2 * M_PI - acos(argacos);
61         *dist = d * earth_radius;
62         *bearing = crs * 180/M_PI;
63 }
64
65 static void
66 aoview_state_add_deg(char *label, double deg)
67 {
68         double  int_part;
69         double  min;
70
71         int_part = floor (deg);
72         min = (deg - int_part) * 60.0;
73         aoview_table_add_row(label, "%d°%lf'",
74                              (int) int_part, min);
75
76 }
77
78 void
79 aoview_state_notify(struct aostate *state)
80 {
81         int     altitude;
82         double  accel;
83         double  velocity_change;
84         int     ticks;
85         double  dist;
86         double  bearing;
87         double  temp;
88         double  battery;
89         double  drogue_sense, main_sense;
90         double  max_accel;
91
92         if (!strcmp(state->state, "pad")) {
93                 if (npad < NUM_PAD_SAMPLES) {
94                         pad_accel_total += state->accel;
95                         pad_pres_total += state->pres;
96                         if (state->locked) {
97                                 pad_lat_total += state->lat;
98                                 pad_lon_total += state->lon;
99                                 pad_alt_total += state->alt;
100                                 npad_gps++;
101                         }
102                         npad++;
103                         velocity = 0;
104                 }
105                 if (npad <= NUM_PAD_SAMPLES) {
106                         pad_pres = pad_pres_total / npad;
107                         pad_accel = pad_accel_total / npad;
108                         if (npad_gps) {
109                                 pad_lat = pad_lat_total / npad_gps;
110                                 pad_lon = pad_lon_total / npad_gps;
111                                 pad_alt = pad_alt_total / npad_gps;
112                         }
113                 }
114                 if (npad == NUM_PAD_SAMPLES) {
115                         npad++;
116                         velocity = 0;
117                         min_pres = pad_pres;
118                         min_accel = pad_accel;
119                 }
120         }
121         if (state->pres < min_pres)
122                 min_pres = state->pres;
123         if (state->accel < min_accel)
124                 min_accel = state->accel;
125         altitude = aoview_pres_to_altitude(state->pres) - aoview_pres_to_altitude(pad_pres);
126         accel = (pad_accel - state->accel) / 264.8 *  9.80665;
127         max_accel = (pad_accel - min_accel) / 264.8 * 9.80665;
128         velocity_change = (accel + prev_accel) / 2.0;
129         ticks = state->tick - prev_tick;
130         velocity -= velocity_change * (ticks / 100.0);
131         temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
132         battery = (state->batt / 32767.0 * 5.0);
133         drogue_sense = (state->drogue / 32767.0 * 15.0);
134         main_sense = (state->main / 32767.0 * 15.0);
135
136         prev_accel = accel;
137         prev_tick = state->tick;
138         aoview_table_start();
139
140         if (npad >= NUM_PAD_SAMPLES)
141                 aoview_table_add_row("Ground state", "ready");
142         else
143                 aoview_table_add_row("Ground state", "preparing (%d)",
144                                      NUM_PAD_SAMPLES - npad);
145         aoview_table_add_row("Rocket state", "%s", state->state);
146         aoview_table_add_row("Callsign", "%s", state->callsign);
147         aoview_table_add_row("Rocket serial", "%d", state->serial);
148
149         aoview_table_add_row("RSSI", "%ddB", state->rssi);
150         aoview_table_add_row("Height", "%dm", altitude);
151         aoview_table_add_row("Max height", "%dm",
152                              aoview_pres_to_altitude(min_pres) -
153                              aoview_pres_to_altitude(pad_pres));
154         aoview_table_add_row("Acceleration", "%gm/s²", accel);
155         aoview_table_add_row("Max acceleration", "%gm/s²", max_accel);
156         aoview_table_add_row("Velocity", "%gm/s", velocity);
157         aoview_table_add_row("Temperature", "%g°C", temp);
158         aoview_table_add_row("Battery", "%gV", battery);
159         aoview_table_add_row("Drogue", "%gV", drogue_sense);
160         aoview_table_add_row("Main", "%gV", main_sense);
161         aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(pad_pres));
162         aoview_table_add_row("Satellites", "%d", state->nsat);
163         if (state->locked) {
164                 aoview_state_add_deg("Latitude", state->lat);
165                 aoview_state_add_deg("Longitude", state->lon);
166                 aoview_table_add_row("GPS alt", "%d", state->alt);
167                 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
168                                      state->gps_time.hour,
169                                      state->gps_time.minute,
170                                      state->gps_time.second);
171                 aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
172                                     &dist, &bearing);
173                 aoview_table_add_row("Distance from pad", "%gm", dist * 1000);
174                 aoview_table_add_row("Direction from pad", "%g°", bearing);
175         } else {
176                 aoview_table_add_row("GPS", "unlocked");
177         }
178         if (npad_gps) {
179                 aoview_state_add_deg("Pad latitude", pad_lat);
180                 aoview_state_add_deg("Pad longitude", pad_lon);
181                 aoview_table_add_row("Pad GPS alt", "%gm", pad_alt);
182         }
183         aoview_table_finish();
184 }
185
186 void
187 aoview_state_new(void)
188 {
189         min_pres = 32767;
190         min_accel = 32767;
191         npad = 0;
192 }
193
194 void
195 aoview_state_init(GladeXML *xml)
196 {
197         aoview_state_new();
198 }