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 enum ao_flight_state ao_flight_state;
28 static uint8_t ao_tracker_force_telem;
29 static uint8_t ao_tracker_force_launch;
33 ao_usb_connected(void)
35 return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
38 #define ao_usb_connected() 1
41 #define STARTUP_AVERAGE 5
43 int32_t ao_tracker_start_latitude;
44 int32_t ao_tracker_start_longitude;
45 int16_t ao_tracker_start_altitude;
47 struct ao_tracker_data ao_tracker_data[AO_TRACKER_RING];
48 uint8_t ao_tracker_head;
49 static uint8_t ao_tracker_log_pos;
51 static uint16_t telem_rate;
52 static uint8_t gps_rate;
53 static uint8_t telem_enabled;
55 static int64_t lat_sum, lon_sum;
56 static int32_t alt_sum;
60 ao_tracker_state_update(struct ao_tracker_data *tracker)
62 uint16_t new_telem_rate;
64 uint8_t new_telem_enabled;
65 uint32_t ground_distance;
69 new_gps_rate = gps_rate;
70 new_telem_rate = telem_rate;
72 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
74 /* Don't change anything if GPS isn't locked */
75 if ((tracker->new & AO_GPS_NEW_DATA) &&
76 (tracker->gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
77 (AO_GPS_VALID|AO_GPS_COURSE_VALID))
79 switch (ao_flight_state) {
80 case ao_flight_startup:
81 new_telem_rate = AO_SEC_TO_TICKS(1);
84 /* startup to pad when GPS locks */
86 lat_sum += tracker->gps_data.latitude;
87 lon_sum += tracker->gps_data.longitude;
88 alt_sum += tracker->gps_data.altitude;
92 if (nsamples >= STARTUP_AVERAGE) {
93 ao_flight_state = ao_flight_pad;
94 ao_wakeup(&ao_flight_state);
95 ao_tracker_start_latitude = lat_sum / nsamples;
96 ao_tracker_start_longitude = lon_sum / nsamples;
97 ao_tracker_start_altitude = alt_sum / nsamples;
101 new_telem_rate = AO_SEC_TO_TICKS(1);
104 ground_distance = ao_distance(tracker->gps_data.latitude,
105 tracker->gps_data.longitude,
106 ao_tracker_start_latitude,
107 ao_tracker_start_longitude);
108 height = tracker->gps_data.altitude - ao_tracker_start_altitude;
112 if (ground_distance >= ao_config.tracker_start_horiz ||
113 height >= ao_config.tracker_start_vert ||
114 ao_tracker_force_launch)
116 ao_flight_state = ao_flight_drogue;
117 ao_wakeup(&ao_flight_state);
118 ao_tracker_log_pos = ao_tracker_ring_next(ao_tracker_head);
123 case ao_flight_drogue:
124 /* Modulate data rates based on speed (in cm/s) */
125 if (tracker->gps_data.climb_rate < 0)
126 speed = -tracker->gps_data.climb_rate;
128 speed = tracker->gps_data.climb_rate;
129 speed += tracker->gps_data.ground_speed;
131 if (speed < AO_TRACKER_NOT_MOVING) {
132 new_telem_rate = AO_SEC_TO_TICKS(10);
135 new_telem_rate = AO_SEC_TO_TICKS(1);
144 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
145 if (new_telem_enabled)
146 ao_telemetry_set_interval(new_telem_rate);
148 ao_telemetry_set_interval(0);
149 telem_rate = new_telem_rate;
150 telem_enabled = new_telem_enabled;
153 if (new_gps_rate != gps_rate) {
154 ao_gps_set_rate(new_gps_rate);
155 gps_rate = new_gps_rate;
160 static uint8_t ao_tracker_should_log;
165 struct ao_tracker_data *tracker;
167 if (ao_log_running) {
168 while (ao_tracker_log_pos != ao_tracker_head) {
169 tracker = &ao_tracker_data[ao_tracker_log_pos];
170 if (tracker->new & AO_GPS_NEW_DATA) {
171 ao_tracker_should_log = ao_log_gps_should_log(tracker->gps_data.latitude,
172 tracker->gps_data.longitude,
173 tracker->gps_data.altitude);
174 if (ao_tracker_should_log)
175 ao_log_gps_data(tracker->tick, tracker->state, &tracker->gps_data);
177 if (tracker->new & AO_GPS_NEW_TRACKING) {
178 if (ao_tracker_should_log)
179 ao_log_gps_tracking(tracker->tick, &tracker->gps_tracking_data);
181 ao_tracker_log_pos = ao_tracker_ring_next(ao_tracker_log_pos);
191 struct ao_tracker_data *tracker;
194 ao_timer_set_adc_interval(100);
198 ao_tracker_force_telem = 1;
209 ao_telemetry_set_interval(0);
210 telem_rate = AO_SEC_TO_TICKS(1);
214 ao_flight_state = ao_flight_startup;
216 while (!(new = ao_gps_new))
217 ao_sleep(&ao_gps_new);
219 /* Stick GPS data into the ring */
220 ao_mutex_get(&ao_gps_mutex);
221 tracker = &ao_tracker_data[ao_tracker_head];
222 tracker->tick = ao_gps_tick;
224 tracker->state = ao_flight_state;
225 tracker->gps_data = ao_gps_data;
226 tracker->gps_tracking_data = ao_gps_tracking_data;
227 ao_tracker_head = ao_tracker_ring_next(ao_tracker_head);
230 ao_mutex_put(&ao_gps_mutex);
232 /* Update state based on current GPS data */
233 ao_tracker_state_update(tracker);
236 /* Log all gps data */
242 static struct ao_task ao_tracker_task;
245 ao_tracker_set_telem(void)
247 uint8_t telem, launch;
249 telem = ao_cmd_lex_i;
251 launch = ao_cmd_lex_i;
252 if (ao_cmd_status == ao_cmd_success) {
253 ao_tracker_force_telem = telem;
254 ao_tracker_force_launch = launch;
256 ao_cmd_status = ao_cmd_success;
257 printf ("flight %d force telem %d force launch %d\n",
258 ao_flight_number, ao_tracker_force_telem, ao_tracker_force_launch);
261 static const struct ao_cmds ao_tracker_cmds[] = {
262 { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
267 ao_tracker_init(void)
270 ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
272 ao_cmd_register(&ao_tracker_cmds[0]);
273 ao_add_task(&ao_tracker_task, ao_tracker, "tracker");