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