2011-03-19 07:20:11 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// run_nav_updates - top level call for the autopilot
|
|
|
|
// ensures calculations such as "distance to waypoint" are calculated before autopilot makes decisions
|
|
|
|
// To-Do - rename and move this function to make it's purpose more clear
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::run_nav_updates(void)
|
2013-01-08 01:45:12 -04:00
|
|
|
{
|
2013-02-18 01:58:24 -04:00
|
|
|
// fetch position from inertial navigation
|
|
|
|
calc_position();
|
2013-01-08 01:45:12 -04:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// calculate distance and bearing for reporting and autopilot decisions
|
|
|
|
calc_distance_and_bearing();
|
2013-01-25 02:11:09 -04:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// run autopilot to make high level decisions about control modes
|
|
|
|
run_autopilot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc_position - get lat and lon positions from inertial nav library
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::calc_position()
|
2015-02-09 07:26:18 -04:00
|
|
|
{
|
|
|
|
// pull position from interial nav library
|
|
|
|
current_loc.lng = inertial_nav.get_longitude();
|
|
|
|
current_loc.lat = inertial_nav.get_latitude();
|
2012-11-07 06:03:30 -04:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:22:05 -03:00
|
|
|
// calc_distance_and_bearing - calculate distance and bearing to next waypoint and home
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::calc_distance_and_bearing()
|
2012-11-07 06:03:30 -04:00
|
|
|
{
|
2014-04-17 10:22:05 -03:00
|
|
|
calc_wp_distance();
|
|
|
|
calc_wp_bearing();
|
|
|
|
calc_home_distance_and_bearing();
|
|
|
|
}
|
2013-10-29 01:28:27 -03:00
|
|
|
|
2014-04-17 10:22:05 -03:00
|
|
|
// calc_wp_distance - calculate distance to next waypoint for reporting and autopilot decisions
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::calc_wp_distance()
|
2014-04-17 10:22:05 -03:00
|
|
|
{
|
2013-02-18 01:58:24 -04:00
|
|
|
// get target from loiter or wpinav controller
|
2014-02-03 03:21:52 -04:00
|
|
|
if (control_mode == LOITER || control_mode == CIRCLE) {
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_loiter_distance_to_target();
|
2015-01-06 03:38:34 -04:00
|
|
|
}else if (control_mode == AUTO || control_mode == RTL || (control_mode == GUIDED && guided_mode == Guided_WP)) {
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_wp_distance_to_destination();
|
2013-01-28 02:59:55 -04:00
|
|
|
}else{
|
2013-03-20 10:29:08 -03:00
|
|
|
wp_distance = 0;
|
2014-04-17 10:22:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc_wp_bearing - calculate bearing to next waypoint for reporting and autopilot decisions
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::calc_wp_bearing()
|
2014-04-17 10:22:05 -03:00
|
|
|
{
|
|
|
|
// get target from loiter or wpinav controller
|
|
|
|
if (control_mode == LOITER || control_mode == CIRCLE) {
|
|
|
|
wp_bearing = wp_nav.get_loiter_bearing_to_target();
|
2015-01-06 03:38:34 -04:00
|
|
|
} else if (control_mode == AUTO || control_mode == RTL || (control_mode == GUIDED && guided_mode == Guided_WP)) {
|
2014-04-17 10:22:05 -03:00
|
|
|
wp_bearing = wp_nav.get_wp_bearing_to_destination();
|
|
|
|
} else {
|
2013-01-28 02:59:55 -04:00
|
|
|
wp_bearing = 0;
|
|
|
|
}
|
2014-04-17 10:22:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// calc_home_distance_and_bearing - calculate distance and bearing to home for reporting and autopilot decisions
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::calc_home_distance_and_bearing()
|
2014-04-17 10:22:05 -03:00
|
|
|
{
|
2013-01-28 02:59:55 -04:00
|
|
|
// calculate home distance and bearing
|
2015-01-02 07:43:50 -04:00
|
|
|
if (position_ok()) {
|
2015-02-02 22:12:31 -04:00
|
|
|
Vector3f home = pv_location_to_vector(ahrs.get_home());
|
2015-07-08 03:22:16 -03:00
|
|
|
Vector3f curr = inertial_nav.get_position();
|
|
|
|
home_distance = pv_get_horizontal_distance_cm(curr, home);
|
2015-02-02 22:12:31 -04:00
|
|
|
home_bearing = pv_get_bearing_cd(curr,home);
|
2012-12-08 01:23:32 -04:00
|
|
|
|
2013-01-28 02:59:55 -04:00
|
|
|
// update super simple bearing (if required) because it relies on home_bearing
|
2013-10-05 06:25:03 -03:00
|
|
|
update_super_simple_bearing(false);
|
2013-02-18 01:58:24 -04:00
|
|
|
}
|
2012-04-19 01:06:15 -03:00
|
|
|
}
|
|
|
|
|
2013-01-24 00:36:55 -04:00
|
|
|
// run_autopilot - highest level call to process mission commands
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::run_autopilot()
|
2012-11-07 06:03:30 -04:00
|
|
|
{
|
2014-02-03 03:21:52 -04:00
|
|
|
if (control_mode == AUTO) {
|
2014-02-27 22:19:05 -04:00
|
|
|
// update state of mission
|
|
|
|
// may call commands_process.pde's start_command and verify_command functions
|
|
|
|
mission.update();
|
2013-01-24 00:36:55 -04:00
|
|
|
}
|
2012-12-09 02:24:19 -04:00
|
|
|
}
|