046ccc92ea5d982d77de2472b01c6493f74e7508
[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 a = (90 - start_lat) * rad;
49         double b = (90 - end_lat) * rad;
50         double phi = (end_lon - start_lon) * rad;
51         double cosr = cos(a) * cos(b) + sin(a) * sin(b) * cos(phi);
52         double r = acos(cosr);
53         double rdist = earth_radius * r;
54         double sinth = sin(phi) * sin(b) / sin(r);
55         double th = asin(sinth) / rad;
56         *dist = rdist;
57         *bearing = th;
58 }
59
60 void
61 aoview_state_notify(struct aostate *state)
62 {
63         int     altitude;
64         double  accel;
65         double  velocity_change;
66         int     ticks;
67         double  dist;
68         double  bearing;
69         double  temp;
70         double  battery;
71         double  drogue_sense, main_sense;
72
73         if (!strcmp(state->state, "pad")) {
74                 if (npad < NUM_PAD_SAMPLES) {
75                         pad_accel_total += state->accel;
76                         pad_pres_total += state->pres;
77                         pad_lat_total += state->lat;
78                         pad_lon_total += state->lon;
79                         pad_alt_total += state->alt;
80                         npad++;
81                         velocity = 0;
82                 }
83                 if (npad <= NUM_PAD_SAMPLES) {
84                         pad_pres = pad_pres_total / npad;
85                         pad_accel = pad_accel_total / npad;
86                         pad_lat = pad_lat_total / npad;
87                         pad_lon = pad_lon_total / npad;
88                         pad_alt = pad_alt_total / npad;
89                 }
90         }
91         altitude = aoview_pres_to_altitude(state->pres) - aoview_pres_to_altitude(pad_pres);
92         accel = (pad_accel - state->accel) / 264.8 *  9.80665;
93         velocity_change = (accel + prev_accel) / 2.0;
94         ticks = state->tick - prev_tick;
95         velocity -= velocity_change * (ticks / 100.0);
96         temp = ((state->temp / 32767.0 * 3.3) - 0.5) / 0.01;
97         battery = (state->batt / 32767.0 * 5.0);
98         drogue_sense = (state->drogue / 32767.0 * 15.0);
99         main_sense = (state->main / 32767.0 * 15.0);
100
101         prev_accel = accel;
102         prev_tick = state->tick;
103         aoview_table_start();
104         aoview_table_add_row("RSSI", "%ddB", state->rssi);
105         aoview_table_add_row("Height", "%dm", altitude);
106         aoview_table_add_row("Acceleration", "%gm/s²", accel);
107         aoview_table_add_row("Velocity", "%gm/s", velocity);
108         aoview_table_add_row("Temperature", "%g°C", temp);
109         aoview_table_add_row("Battery", "%gV", battery);
110         aoview_table_add_row("Drogue", "%gV", drogue_sense);
111         aoview_table_add_row("Main", "%gV", main_sense);
112         aoview_table_add_row("Pad altitude", "%dm", aoview_pres_to_altitude(pad_pres));
113         aoview_table_add_row("Satellites", "%d", state->nsat);
114         if (state->locked) {
115                 aoview_table_add_row("Lat", "%g", state->lat);
116                 aoview_table_add_row("Lon", "%g", state->lon);
117                 aoview_table_add_row("GPS alt", "%d", state->alt);
118                 aoview_table_add_row("GPS time", "%02d:%02d:%02d",
119                                      state->gps_time.hour,
120                                      state->gps_time.minute,
121                                      state->gps_time.second);
122                 aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
123                                     &dist, &bearing);
124                 aoview_table_add_row("Course", "%gkm %g°", dist, bearing);
125         } else {
126                 aoview_table_add_row("GPS", "unlocked");
127         }
128         aoview_table_finish();
129 }
130
131 void
132 aoview_state_init(GladeXML *xml)
133 {
134 }