6530847ed98ed339a23de24329c96f169bb1c30f
[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      prev_tick;
31 static double   prev_accel;
32 static double   velocity;
33 static double   pad_lat;
34 static double   pad_lon;
35 static double   pad_alt;
36
37 #define NUM_PAD_SAMPLES 50
38
39
40
41 static void
42 aoview_great_circle (double start_lat, double start_lon,
43                      double end_lat, double end_lon,
44                      double *dist, double *bearing)
45 {
46         double rad = M_PI / 180;
47         double earth_radius = 6371.2;
48         double lat1 = rad * start_lat;
49         double lon1 = -rad * start_lon;
50         double lat2 = rad * end_lat;
51         double lon2 = -rad * end_lon;
52
53         double d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2));
54         double argacos = (sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1));
55         double crs;
56         if (sin(lon2-lon1) < 0)
57                 crs = acos(argacos);
58         else
59                 crs = 2 * M_PI - acos(argacos);
60         *dist = d * earth_radius;
61         *bearing = crs * 180/M_PI;
62 }
63
64 static void
65 aoview_state_add_deg(char *label, double deg)
66 {
67         double  int_part;
68         double  min;
69
70         int_part = floor (deg);
71         min = (deg - int_part) * 60.0;
72         aoview_table_add_row(label, "%d°%lf'",
73                              (int) int_part, min);
74
75 }
76
77 void
78 aoview_state_notify(struct aostate *state)
79 {
80         int     altitude;
81         double  accel;
82         double  velocity_change;
83         int     ticks;
84         double  dist;
85         double  bearing;
86         double  temp;
87         double  battery;
88         double  drogue_sense, main_sense;
89
90         if (!strcmp(state->state, "pad")) {
91                 if (npad < NUM_PAD_SAMPLES) {
92                         pad_accel_total += state->accel;
93                         pad_pres_total += state->pres;
94                         pad_lat_total += state->lat;
95                         pad_lon_total += state->lon;
96                         pad_alt_total += state->alt;
97                         npad++;
98                         velocity = 0;
99                 }
100                 if (npad <= NUM_PAD_SAMPLES) {
101                         pad_pres = pad_pres_total / npad;
102                         pad_accel = pad_accel_total / npad;
103                         pad_lat = pad_lat_total / npad;
104                         pad_lon = pad_lon_total / npad;
105                         pad_alt = pad_alt_total / npad;
106                 }
107         }
108         altitude = aoview_pres_to_altitude(state->pres) - aoview_pres_to_altitude(pad_pres);
109         accel = (pad_accel - state->accel) / 264.8 *  9.80665;
110         velocity_change = (accel + prev_accel) / 2.0;
111         ticks = state->tick - prev_tick;
112         velocity -= velocity_change * (ticks / 100.0);
113         temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
114         battery = (state->batt / 32767.0 * 5.0);
115         drogue_sense = (state->drogue / 32767.0 * 15.0);
116         main_sense = (state->main / 32767.0 * 15.0);
117
118         prev_accel = accel;
119         prev_tick = state->tick;
120         aoview_table_start();
121         aoview_table_add_row("RSSI", "%ddB", state->rssi);
122         aoview_table_add_row("Height", "%dm", altitude);
123         aoview_table_add_row("Acceleration", "%gm/s²", accel);
124         aoview_table_add_row("Velocity", "%gm/s", velocity);
125         aoview_table_add_row("Temperature", "%g°C", temp);
126         aoview_table_add_row("Battery", "%gV", battery);
127         aoview_table_add_row("Drogue", "%gV", drogue_sense);
128         aoview_table_add_row("Main", "%gV", main_sense);
129         aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(pad_pres));
130         aoview_table_add_row("Satellites", "%d", state->nsat);
131         if (state->locked) {
132                 aoview_state_add_deg("Latitude", state->lat);
133                 aoview_state_add_deg("Longitude", state->lon);
134                 aoview_table_add_row("GPS alt", "%d", state->alt);
135                 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
136                                      state->gps_time.hour,
137                                      state->gps_time.minute,
138                                      state->gps_time.second);
139                 aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
140                                     &dist, &bearing);
141                 aoview_table_add_row("Distance from pad", "%gm", dist * 1000);
142                 aoview_table_add_row("Direction from pad", "%g°", bearing);
143         } else {
144                 aoview_table_add_row("GPS", "unlocked");
145         }
146         aoview_table_finish();
147 }
148
149 void
150 aoview_state_init(GladeXML *xml)
151 {
152 }