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>
21 #include <ao_log_gps.h>
22 #include <ao_distance.h>
23 #include <ao_tracker.h>
26 static uint8_t ao_tracker_force_telem;
30 ao_usb_connected(void)
32 return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
35 #define ao_usb_connected() 1
46 struct gps_position gps_position[GPS_RING];
48 #define ao_gps_ring_next(n) (((n) + 1) & (GPS_RING - 1))
49 #define ao_gps_ring_prev(n) (((n) - 1) & (GPS_RING - 1))
51 static uint8_t gps_head;
53 static uint8_t tracker_mutex;
54 static uint8_t log_started;
55 static struct ao_telemetry_location gps_data;
56 static uint8_t tracker_running;
57 static uint16_t tracker_interval;
63 int32_t ground_distance;
66 uint8_t new_tracker_running;
69 ao_timer_set_adc_interval(100);
73 ao_tracker_force_telem = 1;
75 log_started = ao_log_scan();
79 tracker_interval = ao_config.tracker_interval;
80 ao_gps_set_rate(tracker_interval);
84 /** Wait for new GPS data
86 while (!(new = ao_gps_new))
87 ao_sleep(&ao_gps_new);
88 ao_mutex_get(&ao_gps_mutex);
89 gps_data = ao_gps_data;
90 gps_tick = ao_gps_tick;
92 ao_mutex_put(&ao_gps_mutex);
94 new_tracker_running = ao_tracker_force_telem || !ao_usb_connected();
96 if (ao_config.tracker_interval != tracker_interval) {
97 tracker_interval = ao_config.tracker_interval;
98 ao_gps_set_rate(tracker_interval);
100 /* force telemetry interval to be reset */
104 if (new_tracker_running && !tracker_running)
105 ao_telemetry_set_interval(AO_SEC_TO_TICKS(tracker_interval));
106 else if (!new_tracker_running && tracker_running)
107 ao_telemetry_set_interval(0);
109 tracker_running = new_tracker_running;
111 if (new_tracker_running && !ao_log_running)
113 else if (!new_tracker_running && ao_log_running)
119 if (new & AO_GPS_NEW_DATA) {
120 if ((gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
121 (AO_GPS_VALID|AO_GPS_COURSE_VALID))
125 gps_alt_t altitude = AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data);
127 for (ring = ao_gps_ring_next(gps_head); ring != gps_head; ring = ao_gps_ring_next(ring)) {
128 ground_distance = ao_distance(gps_data.latitude, gps_data.longitude,
129 gps_position[ring].latitude,
130 gps_position[ring].longitude);
131 height = gps_position[ring].altitude - altitude;
135 if (ao_tracker_force_telem > 1)
136 printf("head %d ring %d ground_distance %d height %d\n", gps_head, ring, ground_distance, height);
137 if (ground_distance > ao_config.tracker_motion ||
138 height > (ao_config.tracker_motion << 1))
144 if (ao_tracker_force_telem > 1) {
145 printf ("moving %d started %d\n", moving, log_started);
149 ao_mutex_get(&tracker_mutex);
154 ao_log_gps_data(gps_tick, &gps_data);
155 gps_position[gps_head].latitude = gps_data.latitude;
156 gps_position[gps_head].longitude = gps_data.longitude;
157 gps_position[gps_head].altitude = altitude;
158 gps_head = ao_gps_ring_next(gps_head);
159 ao_mutex_put(&tracker_mutex);
166 static uint8_t erasing_current;
169 ao_tracker_erase_start(uint16_t flight)
171 erasing_current = flight == ao_flight_number;
172 if (erasing_current) {
173 ao_mutex_get(&tracker_mutex);
175 if (++ao_flight_number == 0)
176 ao_flight_number = 1;
181 ao_tracker_erase_end(void)
183 if (erasing_current) {
184 log_started = ao_log_scan();
185 ao_mutex_put(&tracker_mutex);
189 static struct ao_task ao_tracker_task;
192 ao_tracker_set_telem(void)
195 if (ao_cmd_status == ao_cmd_success)
196 ao_tracker_force_telem = ao_cmd_lex_i;
197 ao_cmd_status = ao_cmd_success;
198 printf ("flight: %d\n", ao_flight_number);
199 printf ("force_telem: %d\n", ao_tracker_force_telem);
200 printf ("tracker_running: %d\n", tracker_running);
201 printf ("log_started: %d\n", log_started);
202 printf ("latitude: %ld\n", (long) gps_data.latitude);
203 printf ("longitude: %ld\n", (long) gps_data.longitude);
204 printf ("altitude: %ld\n", AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data));
205 printf ("log_running: %d\n", ao_log_running);
206 printf ("log_start_pos: %ld\n", (long) ao_log_start_pos);
207 printf ("log_cur_pos: %ld\n", (long) ao_log_current_pos);
208 printf ("log_end_pos: %ld\n", (long) ao_log_end_pos);
211 static const struct ao_cmds ao_tracker_cmds[] = {
212 { ao_tracker_set_telem, "t <d>\0Set telem on USB (0 off, 1 on, 2 dbg)" },
217 ao_tracker_init(void)
220 ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
222 ao_cmd_register(&ao_tracker_cmds[0]);
223 ao_add_task(&ao_tracker_task, ao_tracker, "tracker");