altos: Have tracker average 5 GPS samples before moving to pad mode
[fw/altos] / src / product / ao_tracker.c
1 /*
2  * Copyright © 2014 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 <ao.h>
19 #include <ao_flight.h>
20 #include <ao_distance.h>
21 #include <ao_exti.h>
22
23 enum ao_flight_state    ao_flight_state;
24
25 /* Speeds for the various modes, 2m/s seems reasonable for 'not moving' */
26 #define AO_TRACKER_NOT_MOVING   200
27
28 static uint8_t  ao_tracker_force_telem;
29
30 #if HAS_USB_CONNECT
31 static inline uint8_t
32 ao_usb_connected(void)
33 {
34         return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
35 }
36 #else
37 #define ao_usb_connected()      1
38 #endif
39  
40 #define STARTUP_AVERAGE 5
41
42 static void
43 ao_tracker(void)
44 {
45         uint16_t        telem_rate = AO_SEC_TO_TICKS(1), new_telem_rate;
46         uint8_t         gps_rate = 1, new_gps_rate;
47         uint8_t         telem_enabled = 1, new_telem_enabled;
48         int32_t         start_latitude = 0, start_longitude = 0;
49         int16_t         start_altitude = 0;
50         uint32_t        ground_distance;
51         int16_t         height;
52         uint16_t        speed;
53         int64_t         lat_sum = 0, lon_sum = 0;
54         int32_t         alt_sum = 0;
55         int             nsamples = 0;
56
57         ao_timer_set_adc_interval(100);
58
59         ao_flight_state = ao_flight_startup;
60         for (;;) {
61                 ao_sleep(&ao_gps_new);
62
63                 new_gps_rate = gps_rate;
64                 new_telem_rate = telem_rate;
65
66                 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
67
68                 ao_mutex_get(&ao_gps_mutex);
69
70                 /* Don't change anything if GPS isn't locked */
71                 if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
72                     (AO_GPS_VALID|AO_GPS_COURSE_VALID))
73                 {
74                         switch (ao_flight_state) {
75                         case ao_flight_startup:
76                                 /* startup to pad when GPS locks */
77
78                                 lat_sum += ao_gps_data.latitude;
79                                 lon_sum += ao_gps_data.longitude;
80                                 alt_sum += ao_gps_data.altitude;
81
82                                 if (++nsamples >= STARTUP_AVERAGE) {
83                                         ao_flight_state = ao_flight_pad;
84                                         ao_wakeup(&ao_flight_state);
85                                         start_latitude = lat_sum / nsamples;
86                                         start_longitude = lon_sum / nsamples;
87                                         start_altitude = alt_sum / nsamples;
88                                 }
89                                 break;
90                         case ao_flight_pad:
91                                 ground_distance = ao_distance(ao_gps_data.latitude,
92                                                               start_latitude,
93                                                               ao_gps_data.longitude,
94                                                               start_longitude);
95                                 height = ao_gps_data.altitude - start_altitude;
96                                 if (height < 0)
97                                         height = -height;
98                                 if (ground_distance >= ao_config.tracker_start_horiz ||
99                                     height >= ao_config.tracker_start_vert)
100                                 {
101                                         ao_flight_state = ao_flight_drogue;
102                                         ao_log_start();
103                                 }
104                                 break;
105                         case ao_flight_drogue:
106
107                                 /* Modulate data rates based on speed (in cm/s) */
108                                 if (ao_gps_data.climb_rate < 0)
109                                         speed = -ao_gps_data.climb_rate;
110                                 else
111                                         speed = ao_gps_data.climb_rate;
112                                 speed += ao_gps_data.ground_speed;
113
114                                 if (speed < AO_TRACKER_NOT_MOVING) {
115                                         new_telem_rate = AO_SEC_TO_TICKS(10);
116                                         new_gps_rate = 10;
117                                 } else {
118                                         new_telem_rate = AO_SEC_TO_TICKS(1);
119                                         new_gps_rate = 1;
120                                 }
121                                 break;
122                         default:
123                                 break;
124                         }
125                 }
126                 ao_mutex_put(&ao_gps_mutex);
127
128                 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
129                         if (new_telem_enabled)
130                                 ao_telemetry_set_interval(new_telem_rate);
131                         else
132                                 ao_telemetry_set_interval(0);
133                         telem_rate = new_telem_rate;
134                         telem_enabled = new_telem_enabled;
135                 }
136
137                 if (new_gps_rate != gps_rate) {
138                         ao_gps_set_rate(new_gps_rate);
139                         gps_rate = new_gps_rate;
140                 }
141         }
142 }
143
144 static struct ao_task ao_tracker_task;
145
146 static void
147 ao_tracker_set_telem(void)
148 {
149         ao_cmd_hex();
150         if (ao_cmd_status == ao_cmd_success)
151                 ao_tracker_force_telem = ao_cmd_lex_i;
152 }
153
154 static const struct ao_cmds ao_tracker_cmds[] = {
155         { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
156         { 0, NULL },
157 };
158
159 void
160 ao_tracker_init(void)
161 {
162 #if HAS_USB_CONNECT
163         ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
164 #endif
165         ao_cmd_register(&ao_tracker_cmds[0]);
166         ao_add_task(&ao_tracker_task, ao_tracker, "tracker");
167 }