2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_flight.h>
20 #include <ao_distance.h>
23 enum ao_flight_state ao_flight_state;
25 /* Speeds for the various modes */
26 #define AO_TRACKER_NOT_MOVING 100
28 static uint8_t ao_tracker_force_telem;
32 ao_usb_connected(void)
34 return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
37 #define ao_usb_connected() 1
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;
52 ao_timer_set_adc_interval(100);
54 ao_flight_state = ao_flight_startup;
56 ao_sleep(&ao_gps_new);
58 new_gps_rate = gps_rate;
59 new_telem_rate = telem_rate;
61 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
63 ao_mutex_get(&ao_gps_mutex);
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))
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;
78 ground_distance = ao_distance(ao_gps_data.latitude,
80 ao_gps_data.longitude,
82 height = ao_gps_data.altitude - start_altitude;
85 if (ground_distance >= ao_config.tracker_start_horiz ||
86 height >= ao_config.tracker_start_vert)
88 ao_flight_state = ao_flight_drogue;
92 case ao_flight_drogue:
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;
98 speed = ao_gps_data.climb_rate;
99 speed += ao_gps_data.ground_speed;
101 if (speed < AO_TRACKER_NOT_MOVING) {
102 new_telem_rate = AO_SEC_TO_TICKS(10);
105 new_telem_rate = AO_SEC_TO_TICKS(1);
113 ao_mutex_put(&ao_gps_mutex);
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);
119 ao_telemetry_set_interval(0);
120 telem_rate = new_telem_rate;
121 telem_enabled = new_telem_enabled;
124 if (new_gps_rate != gps_rate) {
125 ao_gps_set_rate(new_gps_rate);
126 gps_rate = new_gps_rate;
131 static struct ao_task ao_tracker_task;
134 ao_tracker_set_telem(void)
137 if (ao_cmd_status == ao_cmd_success)
138 ao_tracker_force_telem = ao_cmd_lex_i;
141 static const struct ao_cmds ao_tracker_cmds[] = {
142 { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
147 ao_tracker_init(void)
150 ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
152 ao_cmd_register(&ao_tracker_cmds[0]);
153 ao_add_task(&ao_tracker_task, ao_tracker, "tracker");