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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_flight.h>
22 #include <ao_log_gps.h>
23 #include <ao_distance.h>
24 #include <ao_tracker.h>
27 static uint8_t ao_tracker_force_telem;
31 ao_usb_connected(void)
33 return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
36 #define ao_usb_connected() 1
47 struct gps_position gps_position[GPS_RING];
49 #define ao_gps_ring_next(n) (((n) + 1) & (GPS_RING - 1))
50 #define ao_gps_ring_prev(n) (((n) - 1) & (GPS_RING - 1))
52 static uint8_t gps_head;
54 static uint8_t tracker_mutex;
55 static uint8_t log_started;
56 static struct ao_telemetry_location gps_data;
57 static uint8_t tracker_running;
58 static uint16_t tracker_interval;
64 int32_t ground_distance;
67 uint8_t new_tracker_running;
70 ao_timer_set_adc_interval(100);
74 ao_tracker_force_telem = 1;
76 log_started = ao_log_scan();
80 tracker_interval = ao_config.tracker_interval;
81 ao_gps_set_rate(tracker_interval);
85 /** Wait for new GPS data
87 while (!(new = ao_gps_new))
88 ao_sleep(&ao_gps_new);
89 ao_mutex_get(&ao_gps_mutex);
90 gps_data = ao_gps_data;
91 gps_tick = ao_gps_tick;
93 ao_mutex_put(&ao_gps_mutex);
95 new_tracker_running = ao_tracker_force_telem || !ao_usb_connected();
97 if (ao_config.tracker_interval != tracker_interval) {
98 tracker_interval = ao_config.tracker_interval;
99 ao_gps_set_rate(tracker_interval);
101 /* force telemetry interval to be reset */
105 if (new_tracker_running && !tracker_running)
106 ao_telemetry_set_interval(AO_SEC_TO_TICKS(tracker_interval));
107 else if (!new_tracker_running && tracker_running)
108 ao_telemetry_set_interval(0);
110 tracker_running = new_tracker_running;
112 if (new_tracker_running && !ao_log_running)
114 else if (!new_tracker_running && ao_log_running)
120 if (new & AO_GPS_NEW_DATA) {
121 if ((gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
122 (AO_GPS_VALID|AO_GPS_COURSE_VALID))
126 gps_alt_t altitude = AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data);
128 for (ring = ao_gps_ring_next(gps_head); ring != gps_head; ring = ao_gps_ring_next(ring)) {
129 ground_distance = ao_distance(gps_data.latitude, gps_data.longitude,
130 gps_position[ring].latitude,
131 gps_position[ring].longitude);
132 height = gps_position[ring].altitude - altitude;
136 if (ao_tracker_force_telem > 1)
137 printf("head %d ring %d ground_distance %d height %d\n", gps_head, ring, ground_distance, height);
138 if (ground_distance > ao_config.tracker_motion ||
139 height > (ao_config.tracker_motion << 1))
145 if (ao_tracker_force_telem > 1) {
146 printf ("moving %d started %d\n", moving, log_started);
150 ao_mutex_get(&tracker_mutex);
155 ao_log_gps_data(gps_tick, &gps_data);
156 gps_position[gps_head].latitude = gps_data.latitude;
157 gps_position[gps_head].longitude = gps_data.longitude;
158 gps_position[gps_head].altitude = altitude;
159 gps_head = ao_gps_ring_next(gps_head);
160 ao_mutex_put(&tracker_mutex);
167 #ifdef AO_LED_GPS_LOCK
169 static struct ao_task ao_gps_lock_task;
175 if ((gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
176 (AO_GPS_VALID|AO_GPS_COURSE_VALID))
178 ao_led_for(AO_LED_GPS_LOCK, AO_MS_TO_TICKS(20));
180 ao_delay(AO_SEC_TO_TICKS(3));
186 static uint8_t erasing_current;
189 ao_tracker_erase_start(uint16_t flight)
191 erasing_current = flight == ao_flight_number;
192 if (erasing_current) {
193 ao_mutex_get(&tracker_mutex);
195 if (++ao_flight_number == 0)
196 ao_flight_number = 1;
201 ao_tracker_erase_end(void)
203 if (erasing_current) {
204 log_started = ao_log_scan();
205 ao_mutex_put(&tracker_mutex);
209 static struct ao_task ao_tracker_task;
212 ao_tracker_set_telem(void)
215 if (ao_cmd_status == ao_cmd_success)
216 ao_tracker_force_telem = ao_cmd_lex_i;
217 ao_cmd_status = ao_cmd_success;
218 printf ("flight: %d\n", ao_flight_number);
219 printf ("force_telem: %d\n", ao_tracker_force_telem);
220 printf ("tracker_running: %d\n", tracker_running);
221 printf ("log_started: %d\n", log_started);
222 printf ("latitude: %ld\n", (long) gps_data.latitude);
223 printf ("longitude: %ld\n", (long) gps_data.longitude);
224 printf ("altitude: %ld\n", AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data));
225 printf ("log_running: %d\n", ao_log_running);
226 printf ("log_start_pos: %ld\n", (long) ao_log_start_pos);
227 printf ("log_cur_pos: %ld\n", (long) ao_log_current_pos);
228 printf ("log_end_pos: %ld\n", (long) ao_log_end_pos);
231 static const struct ao_cmds ao_tracker_cmds[] = {
232 { ao_tracker_set_telem, "t <d>\0Set telem on USB (0 off, 1 on, 2 dbg)" },
237 ao_tracker_init(void)
240 ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
242 ao_cmd_register(&ao_tracker_cmds[0]);
243 ao_add_task(&ao_tracker_task, ao_tracker, "tracker");
244 #ifdef AO_LED_GPS_LOCK
245 ao_add_task(&ao_gps_lock_task, ao_gps_lock, "gps lock");