Add preliminary aoview code
[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
70         if (!strcmp(state->state, "pad")) {
71                 if (npad < NUM_PAD_SAMPLES) {
72                         pad_accel_total += state->accel;
73                         pad_pres_total += state->pres;
74                         pad_lat_total += state->lat;
75                         pad_lon_total += state->lon;
76                         pad_alt_total += state->alt;
77                         npad++;
78                         velocity = 0;
79                 }
80                 if (npad <= NUM_PAD_SAMPLES) {
81                         pad_pres = pad_pres_total / npad;
82                         pad_accel = pad_accel_total / npad;
83                         pad_lat = pad_lat_total / npad;
84                         pad_lon = pad_lon_total / npad;
85                         pad_alt = pad_alt_total / npad;
86                 }
87         }
88         altitude = aoview_pres_to_altitude(state->pres) - aoview_pres_to_altitude(pad_pres);
89         accel = (pad_accel - state->accel) / 264.8 *  9.80665;
90         velocity_change = (accel + prev_accel) / 2.0;
91         ticks = state->tick - prev_tick;
92         velocity -= velocity_change * (ticks / 100.0);
93
94         prev_accel = accel;
95         prev_tick = state->tick;
96         printf ("Pad altitude: %dm\n", aoview_pres_to_altitude(pad_pres));
97         printf ("AGL: %dm\n", altitude);
98         printf ("Acceleration: %gm/s²\n", accel);
99         printf ("Velocity: %gm/s\n", velocity);
100         printf ("Lat: %g\n", state->lat);
101         printf ("Lon: %g\n", state->lon);
102         printf ("GPS alt: %d\n", state->alt);
103         aoview_great_circle(pad_lat, pad_lon, state->lat, state->lon,
104                             &dist, &bearing);
105         printf ("Course: %gkm %g°\n", dist, bearing);
106 }
107
108 void
109 aoview_state_init(GladeXML *xml)
110 {
111 }