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, 2m/s seems reasonable for 'not moving' */
26 #define AO_TRACKER_NOT_MOVING 200
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
44 ao_tracker_start_flight(void)
46 struct ao_log_mega log;
48 log.type = AO_LOG_FLIGHT;
51 log.u.flight.ground_accel = ao_ground_accel;
54 log.u.flight.ground_accel_along = ao_ground_accel_along;
55 log.u.flight.ground_accel_across = ao_ground_accel_across;
56 log.u.flight.ground_accel_through = ao_ground_accel_through;
57 log.u.flight.ground_roll = ao_ground_roll;
58 log.u.flight.ground_pitch = ao_ground_pitch;
59 log.u.flight.ground_yaw = ao_ground_yaw;
62 log.u.flight.ground_pres = ao_ground_pres;
64 log.u.flight.flight = ao_flight_number;
71 uint16_t telem_rate = AO_SEC_TO_TICKS(1), new_telem_rate;
72 uint8_t gps_rate = 1, new_gps_rate;
73 uint8_t telem_enabled = 1, new_telem_enabled;
74 int32_t start_latitude = 0, start_longitude = 0;
75 int16_t start_altitude = 0;
76 uint32_t ground_distance;
79 int64_t lat_sum = 0, lon_sum = 0;
84 ao_timer_set_adc_interval(100);
91 ao_telemetry_set_interval(0);
93 ao_flight_state = ao_flight_startup;
95 ao_sleep(&ao_gps_new);
97 new_gps_rate = gps_rate;
98 new_telem_rate = telem_rate;
100 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
102 ao_mutex_get(&ao_gps_mutex);
104 /* Don't change anything if GPS isn't locked */
105 if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
106 (AO_GPS_VALID|AO_GPS_COURSE_VALID))
108 switch (ao_flight_state) {
109 case ao_flight_startup:
110 /* startup to pad when GPS locks */
112 lat_sum += ao_gps_data.latitude;
113 lon_sum += ao_gps_data.longitude;
114 alt_sum += ao_gps_data.altitude;
116 if (++nsamples >= STARTUP_AVERAGE) {
117 ao_flight_state = ao_flight_pad;
118 ao_wakeup(&ao_flight_state);
119 start_latitude = lat_sum / nsamples;
120 start_longitude = lon_sum / nsamples;
121 start_altitude = alt_sum / nsamples;
125 ground_distance = ao_distance(ao_gps_data.latitude,
126 ao_gps_data.longitude,
129 height = ao_gps_data.altitude - start_altitude;
133 if (ground_distance >= ao_config.tracker_start_horiz ||
134 height >= ao_config.tracker_start_vert ||
135 ao_tracker_force_launch)
137 ao_flight_state = ao_flight_drogue;
138 ao_wakeup(&ao_flight_state);
140 ao_tracker_start_flight();
143 case ao_flight_drogue:
144 /* Modulate data rates based on speed (in cm/s) */
145 if (ao_gps_data.climb_rate < 0)
146 speed = -ao_gps_data.climb_rate;
148 speed = ao_gps_data.climb_rate;
149 speed += ao_gps_data.ground_speed;
151 if (speed < AO_TRACKER_NOT_MOVING) {
152 new_telem_rate = AO_SEC_TO_TICKS(10);
155 new_telem_rate = AO_SEC_TO_TICKS(1);
163 ao_mutex_put(&ao_gps_mutex);
165 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
166 if (new_telem_enabled)
167 ao_telemetry_set_interval(new_telem_rate);
169 ao_telemetry_set_interval(0);
170 telem_rate = new_telem_rate;
171 telem_enabled = new_telem_enabled;
174 if (new_gps_rate != gps_rate) {
175 ao_gps_set_rate(new_gps_rate);
176 gps_rate = new_gps_rate;
181 static struct ao_task ao_tracker_task;
184 ao_tracker_set_telem(void)
187 if (ao_cmd_status == ao_cmd_success) {
188 ao_tracker_force_telem = (ao_cmd_lex_i & 1) != 0;
189 ao_tracker_force_launch = (ao_cmd_lex_i & 2) != 0;
190 printf ("force telem %d force launch %d\n", ao_tracker_force_telem, ao_tracker_force_launch);
194 static const struct ao_cmds ao_tracker_cmds[] = {
195 { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
200 ao_tracker_init(void)
203 ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
205 ao_cmd_register(&ao_tracker_cmds[0]);
206 ao_add_task(&ao_tracker_task, ao_tracker, "tracker");