altos: Fix tracker start state
[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 #if HAS_ADC
58         ao_timer_set_adc_interval(100);
59 #endif
60
61         ao_log_scan();
62
63         ao_rdf_set(1);
64
65         ao_telemetry_set_interval(0);
66
67         ao_flight_state = ao_flight_startup;
68         for (;;) {
69                 ao_sleep(&ao_gps_new);
70
71                 new_gps_rate = gps_rate;
72                 new_telem_rate = telem_rate;
73
74                 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
75
76                 ao_mutex_get(&ao_gps_mutex);
77
78                 /* Don't change anything if GPS isn't locked */
79                 if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
80                     (AO_GPS_VALID|AO_GPS_COURSE_VALID))
81                 {
82                         switch (ao_flight_state) {
83                         case ao_flight_startup:
84                                 /* startup to pad when GPS locks */
85
86                                 lat_sum += ao_gps_data.latitude;
87                                 lon_sum += ao_gps_data.longitude;
88                                 alt_sum += ao_gps_data.altitude;
89
90                                 if (++nsamples >= STARTUP_AVERAGE) {
91                                         ao_flight_state = ao_flight_pad;
92                                         ao_wakeup(&ao_flight_state);
93                                         start_latitude = lat_sum / nsamples;
94                                         start_longitude = lon_sum / nsamples;
95                                         start_altitude = alt_sum / nsamples;
96                                 }
97                                 break;
98                         case ao_flight_pad:
99                                 ground_distance = ao_distance(ao_gps_data.latitude,
100                                                               start_latitude,
101                                                               ao_gps_data.longitude,
102                                                               start_longitude);
103                                 height = ao_gps_data.altitude - start_altitude;
104                                 if (height < 0)
105                                         height = -height;
106                                 if (ground_distance >= ao_config.tracker_start_horiz ||
107                                     height >= ao_config.tracker_start_vert)
108                                 {
109                                         ao_flight_state = ao_flight_drogue;
110                                         ao_log_start();
111                                 }
112                                 break;
113                         case ao_flight_drogue:
114
115                                 /* Modulate data rates based on speed (in cm/s) */
116                                 if (ao_gps_data.climb_rate < 0)
117                                         speed = -ao_gps_data.climb_rate;
118                                 else
119                                         speed = ao_gps_data.climb_rate;
120                                 speed += ao_gps_data.ground_speed;
121
122                                 if (speed < AO_TRACKER_NOT_MOVING) {
123                                         new_telem_rate = AO_SEC_TO_TICKS(10);
124                                         new_gps_rate = 10;
125                                 } else {
126                                         new_telem_rate = AO_SEC_TO_TICKS(1);
127                                         new_gps_rate = 1;
128                                 }
129                                 break;
130                         default:
131                                 break;
132                         }
133                 }
134                 ao_mutex_put(&ao_gps_mutex);
135
136                 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
137                         if (new_telem_enabled)
138                                 ao_telemetry_set_interval(new_telem_rate);
139                         else
140                                 ao_telemetry_set_interval(0);
141                         telem_rate = new_telem_rate;
142                         telem_enabled = new_telem_enabled;
143                 }
144
145                 if (new_gps_rate != gps_rate) {
146                         ao_gps_set_rate(new_gps_rate);
147                         gps_rate = new_gps_rate;
148                 }
149         }
150 }
151
152 static struct ao_task ao_tracker_task;
153
154 static void
155 ao_tracker_set_telem(void)
156 {
157         ao_cmd_hex();
158         if (ao_cmd_status == ao_cmd_success)
159                 ao_tracker_force_telem = ao_cmd_lex_i;
160 }
161
162 static const struct ao_cmds ao_tracker_cmds[] = {
163         { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
164         { 0, NULL },
165 };
166
167 void
168 ao_tracker_init(void)
169 {
170 #if HAS_USB_CONNECT
171         ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
172 #endif
173         ao_cmd_register(&ao_tracker_cmds[0]);
174         ao_add_task(&ao_tracker_task, ao_tracker, "tracker");
175 }