altos: Change tracker not-moving speed to 2m/s and document
[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 static void
41 ao_tracker(void)
42 {
43         uint16_t        telem_rate = AO_SEC_TO_TICKS(1), new_telem_rate;
44         uint8_t         gps_rate = 1, new_gps_rate;
45         uint8_t         telem_enabled = 1, new_telem_enabled;
46         int32_t         start_latitude = 0, start_longitude = 0;
47         int16_t         start_altitude = 0;
48         uint32_t        ground_distance;
49         int16_t         height;
50         uint16_t        speed;
51
52         ao_timer_set_adc_interval(100);
53
54         ao_flight_state = ao_flight_startup;
55         for (;;) {
56                 ao_sleep(&ao_gps_new);
57
58                 new_gps_rate = gps_rate;
59                 new_telem_rate = telem_rate;
60
61                 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
62
63                 ao_mutex_get(&ao_gps_mutex);
64
65                 /* Don't change anything if GPS isn't locked */
66                 if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
67                     (AO_GPS_VALID|AO_GPS_COURSE_VALID))
68                 {
69                         switch (ao_flight_state) {
70                         case ao_flight_startup:
71                                 /* startup to pad when GPS locks */
72                                 ao_flight_state = ao_flight_pad;
73                                 start_latitude = ao_gps_data.longitude;
74                                 start_longitude = ao_gps_data.latitude;
75                                 start_altitude = ao_gps_data.altitude;
76                                 break;
77                         case ao_flight_pad:
78                                 ground_distance = ao_distance(ao_gps_data.latitude,
79                                                               start_latitude,
80                                                               ao_gps_data.longitude,
81                                                               start_longitude);
82                                 height = ao_gps_data.altitude - start_altitude;
83                                 if (height < 0)
84                                         height = -height;
85                                 if (ground_distance >= ao_config.tracker_start_horiz ||
86                                     height >= ao_config.tracker_start_vert)
87                                 {
88                                         ao_flight_state = ao_flight_drogue;
89                                         ao_log_start();
90                                 }
91                                 break;
92                         case ao_flight_drogue:
93
94                                 /* Modulate data rates based on speed (in cm/s) */
95                                 if (ao_gps_data.climb_rate < 0)
96                                         speed = -ao_gps_data.climb_rate;
97                                 else
98                                         speed = ao_gps_data.climb_rate;
99                                 speed += ao_gps_data.ground_speed;
100
101                                 if (speed < AO_TRACKER_NOT_MOVING) {
102                                         new_telem_rate = AO_SEC_TO_TICKS(10);
103                                         new_gps_rate = 10;
104                                 } else {
105                                         new_telem_rate = AO_SEC_TO_TICKS(1);
106                                         new_gps_rate = 1;
107                                 }
108                                 break;
109                         default:
110                                 break;
111                         }
112                 }
113                 ao_mutex_put(&ao_gps_mutex);
114
115                 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
116                         if (new_telem_enabled)
117                                 ao_telemetry_set_interval(new_telem_rate);
118                         else
119                                 ao_telemetry_set_interval(0);
120                         telem_rate = new_telem_rate;
121                         telem_enabled = new_telem_enabled;
122                 }
123
124                 if (new_gps_rate != gps_rate) {
125                         ao_gps_set_rate(new_gps_rate);
126                         gps_rate = new_gps_rate;
127                 }
128         }
129 }
130
131 static struct ao_task ao_tracker_task;
132
133 static void
134 ao_tracker_set_telem(void)
135 {
136         ao_cmd_hex();
137         if (ao_cmd_status == ao_cmd_success)
138                 ao_tracker_force_telem = ao_cmd_lex_i;
139 }
140
141 static const struct ao_cmds ao_tracker_cmds[] = {
142         { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
143         { 0, NULL },
144 };
145
146 void
147 ao_tracker_init(void)
148 {
149 #if HAS_USB_CONNECT
150         ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
151 #endif
152         ao_cmd_register(&ao_tracker_cmds[0]);
153         ao_add_task(&ao_tracker_task, ao_tracker, "tracker");
154 }