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
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
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
|
2016-12-23 06:41:34 -04:00
|
|
|
switch (control_mode) {
|
|
|
|
case LOITER:
|
|
|
|
case CIRCLE:
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_loiter_distance_to_target();
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUTO:
|
|
|
|
case RTL:
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_wp_distance_to_destination();
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GUIDED:
|
|
|
|
if (guided_mode == Guided_WP) {
|
|
|
|
wp_distance = wp_nav.get_wp_distance_to_destination();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// no break
|
|
|
|
default:
|
2013-03-20 10:29:08 -03:00
|
|
|
wp_distance = 0;
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
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
|
2016-12-23 06:41:34 -04:00
|
|
|
switch (control_mode) {
|
|
|
|
case LOITER:
|
|
|
|
case CIRCLE:
|
2014-04-17 10:22:05 -03:00
|
|
|
wp_bearing = wp_nav.get_loiter_bearing_to_target();
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUTO:
|
|
|
|
case RTL:
|
2014-04-17 10:22:05 -03:00
|
|
|
wp_bearing = wp_nav.get_wp_bearing_to_destination();
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GUIDED:
|
|
|
|
if (guided_mode == Guided_WP) {
|
|
|
|
wp_bearing = wp_nav.get_wp_bearing_to_destination();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// no break
|
|
|
|
default:
|
2013-01-28 02:59:55 -04:00
|
|
|
wp_bearing = 0;
|
2016-12-23 06:41:34 -04:00
|
|
|
break;
|
2013-01-28 02:59:55 -04:00
|
|
|
}
|
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
|
|
|
}
|