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