2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2019-02-25 10:32:52 -04:00
|
|
|
#if MODE_GUIDED_ENABLED == ENABLED
|
|
|
|
|
2014-01-28 09:44:37 -04:00
|
|
|
/*
|
2016-07-25 15:45:29 -03:00
|
|
|
* Init and run calls for guided flight mode
|
2014-01-28 09:44:37 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GUIDED_LOOK_AT_TARGET_MIN_DISTANCE_CM
|
|
|
|
# define GUIDED_LOOK_AT_TARGET_MIN_DISTANCE_CM 500 // point nose at target if it is more than 5m away
|
|
|
|
#endif
|
|
|
|
|
2015-01-06 23:33:54 -04:00
|
|
|
#define GUIDED_POSVEL_TIMEOUT_MS 3000 // guided mode's position-velocity controller times out after 3seconds with no new updates
|
2015-10-10 09:41:42 -03:00
|
|
|
#define GUIDED_ATTITUDE_TIMEOUT_MS 1000 // guided mode's attitude controller times out after 1 second with no new updates
|
2015-01-06 23:33:54 -04:00
|
|
|
|
2016-07-21 04:14:21 -03:00
|
|
|
static Vector3f guided_pos_target_cm; // position target (used by posvel controller only)
|
|
|
|
static Vector3f guided_vel_target_cms; // velocity target (used by velocity controller and posvel controller)
|
|
|
|
static uint32_t posvel_update_time_ms; // system time of last target update to posvel controller (i.e. position and velocity update)
|
|
|
|
static uint32_t vel_update_time_ms; // system time of last target update to velocity controller
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
struct {
|
|
|
|
uint32_t update_time_ms;
|
|
|
|
float roll_cd;
|
|
|
|
float pitch_cd;
|
|
|
|
float yaw_cd;
|
2016-08-02 14:20:57 -03:00
|
|
|
float yaw_rate_cds;
|
2020-07-27 06:18:34 -03:00
|
|
|
float climb_rate_cms; // climb rate in cms. Used if use_thrust is false
|
|
|
|
float thrust; // thrust from -1 to 1. Used if use_thrust is true
|
2016-08-02 14:20:57 -03:00
|
|
|
bool use_yaw_rate;
|
2020-07-27 06:18:34 -03:00
|
|
|
bool use_thrust;
|
2017-07-10 09:51:43 -03:00
|
|
|
} static guided_angle_state;
|
2015-10-08 08:13:53 -03:00
|
|
|
|
2014-10-13 05:41:48 -03:00
|
|
|
struct Guided_Limit {
|
|
|
|
uint32_t timeout_ms; // timeout (in seconds) from the time that guided is invoked
|
|
|
|
float alt_min_cm; // lower altitude limit in cm above home (0 = no limit)
|
|
|
|
float alt_max_cm; // upper altitude limit in cm above home (0 = no limit)
|
|
|
|
float horiz_max_cm; // horizontal position limit in cm from where guided mode was initiated (0 = no limit)
|
|
|
|
uint32_t start_time;// system time in milliseconds that control was handed to the external computer
|
|
|
|
Vector3f start_pos; // start position as a distance from home in cm. used for checking horiz_max limit
|
|
|
|
} guided_limit;
|
|
|
|
|
2014-01-28 09:44:37 -04:00
|
|
|
// guided_init - initialise guided controller
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::init(bool ignore_checks)
|
2014-01-28 09:44:37 -04:00
|
|
|
{
|
2019-02-28 20:52:28 -04:00
|
|
|
// start in position control mode
|
|
|
|
pos_control_start();
|
2021-03-05 03:03:56 -04:00
|
|
|
send_notification = false;
|
2019-02-28 20:52:28 -04:00
|
|
|
return true;
|
2014-01-28 09:44:37 -04:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:30:01 -03:00
|
|
|
// guided_run - runs the guided controller
|
|
|
|
// should be called at 100hz or more
|
|
|
|
void ModeGuided::run()
|
|
|
|
{
|
|
|
|
// call the correct auto controller
|
|
|
|
switch (guided_mode) {
|
|
|
|
|
|
|
|
case Guided_TakeOff:
|
|
|
|
// run takeoff controller
|
|
|
|
takeoff_run();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Guided_WP:
|
|
|
|
// run position controller
|
|
|
|
pos_control_run();
|
2021-03-05 03:03:56 -04:00
|
|
|
if (send_notification && wp_nav->reached_wp_destination()) {
|
|
|
|
send_notification = false;
|
|
|
|
gcs().send_mission_item_reached_message(0);
|
|
|
|
}
|
2020-10-14 02:30:01 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Guided_Velocity:
|
|
|
|
// run velocity controller
|
|
|
|
vel_control_run();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Guided_PosVel:
|
|
|
|
// run position-velocity controller
|
|
|
|
posvel_control_run();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Guided_Angle:
|
|
|
|
// run angle controller
|
|
|
|
angle_control_run();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModeGuided::allows_arming(bool from_gcs) const
|
|
|
|
{
|
|
|
|
// always allow arming from the ground station
|
|
|
|
if (from_gcs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// optionally allow arming from the transmitter
|
2020-10-19 01:39:51 -03:00
|
|
|
return (copter.g2.guided_options & (uint32_t)Options::AllowArmingFromTX) != 0;
|
2020-10-14 02:30:01 -03:00
|
|
|
};
|
2014-08-04 11:11:23 -03:00
|
|
|
|
2017-12-12 06:09:48 -04:00
|
|
|
// do_user_takeoff_start - initialises waypoint controller to implement take-off
|
2020-03-06 22:28:43 -04:00
|
|
|
bool ModeGuided::do_user_takeoff_start(float takeoff_alt_cm)
|
2014-08-04 11:11:23 -03:00
|
|
|
{
|
|
|
|
guided_mode = Guided_TakeOff;
|
2016-03-11 04:16:26 -04:00
|
|
|
|
2014-08-04 11:11:23 -03:00
|
|
|
// initialise wpnav destination
|
2019-01-01 22:54:31 -04:00
|
|
|
Location target_loc = copter.current_loc;
|
2020-03-09 07:30:47 -03:00
|
|
|
Location::AltFrame frame = Location::AltFrame::ABOVE_HOME;
|
|
|
|
if (wp_nav->rangefinder_used_and_healthy() &&
|
|
|
|
wp_nav->get_terrain_source() == AC_WPNav::TerrainSource::TERRAIN_FROM_RANGEFINDER &&
|
|
|
|
takeoff_alt_cm < copter.rangefinder.max_distance_cm_orient(ROTATION_PITCH_270)) {
|
|
|
|
// can't takeoff downwards
|
|
|
|
if (takeoff_alt_cm <= copter.rangefinder_state.alt_cm) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
frame = Location::AltFrame::ABOVE_TERRAIN;
|
|
|
|
}
|
|
|
|
target_loc.set_alt_cm(takeoff_alt_cm, frame);
|
2016-03-11 04:16:26 -04:00
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!wp_nav->set_wp_destination(target_loc)) {
|
2016-04-22 08:46:36 -03:00
|
|
|
// failure to set destination can only be because of missing terrain data
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::NAVIGATION, LogErrorCode::FAILED_TO_SET_DESTINATION);
|
2016-04-22 08:46:36 -03:00
|
|
|
// failure is propagated to GCS with NAK
|
2016-03-11 04:16:26 -04:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 11:11:23 -03:00
|
|
|
|
|
|
|
// initialise yaw
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2014-08-04 11:11:23 -03:00
|
|
|
|
2015-07-01 22:37:12 -03:00
|
|
|
// clear i term when we're taking off
|
|
|
|
set_throttle_takeoff();
|
2016-03-11 04:16:26 -04:00
|
|
|
|
2016-08-15 00:57:38 -03:00
|
|
|
// get initial alt for WP_NAVALT_MIN
|
2018-06-26 00:24:54 -03:00
|
|
|
auto_takeoff_set_start_alt();
|
2016-03-21 23:13:34 -03:00
|
|
|
|
2016-03-11 04:16:26 -04:00
|
|
|
return true;
|
2014-08-04 11:11:23 -03:00
|
|
|
}
|
|
|
|
|
2014-06-02 06:06:11 -03:00
|
|
|
// initialise guided mode's position controller
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::pos_control_start()
|
2014-06-02 06:06:11 -03:00
|
|
|
{
|
|
|
|
// set to position control mode
|
|
|
|
guided_mode = Guided_WP;
|
|
|
|
|
|
|
|
// initialise waypoint and spline controller
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->wp_and_spline_init();
|
2014-06-02 06:06:11 -03:00
|
|
|
|
2017-04-27 01:44:08 -03:00
|
|
|
// initialise wpnav to stopping point
|
2014-06-02 06:06:11 -03:00
|
|
|
Vector3f stopping_point;
|
2017-04-27 01:44:08 -03:00
|
|
|
wp_nav->get_wp_stopping_point(stopping_point);
|
2016-04-22 08:46:36 -03:00
|
|
|
|
|
|
|
// no need to check return status because terrain data is not used
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->set_wp_destination(stopping_point, false);
|
2014-06-02 06:06:11 -03:00
|
|
|
|
2014-07-06 04:43:29 -03:00
|
|
|
// initialise yaw
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode_to_default(false);
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialise guided mode's velocity controller
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::vel_control_start()
|
2014-06-02 06:06:11 -03:00
|
|
|
{
|
|
|
|
// set guided_mode to velocity controller
|
|
|
|
guided_mode = Guided_Velocity;
|
|
|
|
|
2017-11-17 01:10:54 -04:00
|
|
|
// initialise horizontal speed, acceleration
|
2019-01-24 01:01:46 -04:00
|
|
|
pos_control->set_max_speed_xy(wp_nav->get_default_speed_xy());
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_accel_xy(wp_nav->get_wp_acceleration());
|
2017-04-27 01:21:31 -03:00
|
|
|
|
|
|
|
// initialize vertical speeds and acceleration
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_speed_z(-get_pilot_speed_dn(), g.pilot_speed_up);
|
|
|
|
pos_control->set_max_accel_z(g.pilot_accel_z);
|
2014-06-02 06:06:11 -03:00
|
|
|
|
|
|
|
// initialise velocity controller
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->init_vel_controller_xyz();
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
|
2014-11-25 16:15:45 -04:00
|
|
|
// initialise guided mode's posvel controller
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::posvel_control_start()
|
2014-11-25 16:15:45 -04:00
|
|
|
{
|
|
|
|
// set guided_mode to velocity controller
|
|
|
|
guided_mode = Guided_PosVel;
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->init_xy_controller();
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2015-01-06 23:33:54 -04:00
|
|
|
// set speed and acceleration from wpnav's speed and acceleration
|
2019-01-24 01:01:46 -04:00
|
|
|
pos_control->set_max_speed_xy(wp_nav->get_default_speed_xy());
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_accel_xy(wp_nav->get_wp_acceleration());
|
2014-11-25 16:15:45 -04:00
|
|
|
|
|
|
|
const Vector3f& curr_pos = inertial_nav.get_position();
|
|
|
|
const Vector3f& curr_vel = inertial_nav.get_velocity();
|
|
|
|
|
2015-01-06 23:33:54 -04:00
|
|
|
// set target position and velocity to current position and velocity
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_xy_target(curr_pos.x, curr_pos.y);
|
|
|
|
pos_control->set_desired_velocity_xy(curr_vel.x, curr_vel.y);
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2015-01-06 23:33:54 -04:00
|
|
|
// set vertical speed and acceleration
|
2019-01-24 01:01:46 -04:00
|
|
|
pos_control->set_max_speed_z(wp_nav->get_default_speed_down(), wp_nav->get_default_speed_up());
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_accel_z(wp_nav->get_accel_z());
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2015-01-06 23:33:54 -04:00
|
|
|
// pilot always controls yaw
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2014-11-25 16:15:45 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::is_taking_off() const
|
2018-04-30 06:50:04 -03:00
|
|
|
{
|
|
|
|
return guided_mode == Guided_TakeOff;
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
// initialise guided mode's angle controller
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::angle_control_start()
|
2015-10-08 08:13:53 -03:00
|
|
|
{
|
|
|
|
// set guided_mode to velocity controller
|
|
|
|
guided_mode = Guided_Angle;
|
|
|
|
|
|
|
|
// set vertical speed and acceleration
|
2019-01-24 01:01:46 -04:00
|
|
|
pos_control->set_max_speed_z(wp_nav->get_default_speed_down(), wp_nav->get_default_speed_up());
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_accel_z(wp_nav->get_accel_z());
|
2015-10-08 08:13:53 -03:00
|
|
|
|
2015-10-27 10:06:50 -03:00
|
|
|
// initialise position and desired velocity
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!pos_control->is_active_z()) {
|
|
|
|
pos_control->set_alt_target_to_current_alt();
|
|
|
|
pos_control->set_desired_velocity_z(inertial_nav.get_velocity_z());
|
2016-10-14 09:28:32 -03:00
|
|
|
}
|
2015-10-08 08:13:53 -03:00
|
|
|
|
|
|
|
// initialise targets
|
|
|
|
guided_angle_state.update_time_ms = millis();
|
2018-03-21 09:24:16 -03:00
|
|
|
guided_angle_state.roll_cd = ahrs.roll_sensor;
|
2015-10-08 08:13:53 -03:00
|
|
|
guided_angle_state.pitch_cd = ahrs.pitch_sensor;
|
|
|
|
guided_angle_state.yaw_cd = ahrs.yaw_sensor;
|
|
|
|
guided_angle_state.climb_rate_cms = 0.0f;
|
2016-08-02 14:20:57 -03:00
|
|
|
guided_angle_state.yaw_rate_cds = 0.0f;
|
|
|
|
guided_angle_state.use_yaw_rate = false;
|
2015-10-08 08:13:53 -03:00
|
|
|
|
|
|
|
// pilot always controls yaw
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2015-10-08 08:13:53 -03:00
|
|
|
}
|
|
|
|
|
2014-01-28 09:44:37 -04:00
|
|
|
// guided_set_destination - sets guided mode's target destination
|
2016-04-28 04:35:29 -03:00
|
|
|
// Returns true if the fence is enabled and guided waypoint is within the fence
|
|
|
|
// else return false if the waypoint is outside the fence
|
2020-02-16 05:42:31 -04:00
|
|
|
bool ModeGuided::set_destination(const Vector3f& destination, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw, bool terrain_alt)
|
2014-01-28 09:44:37 -04:00
|
|
|
{
|
2016-04-28 04:35:29 -03:00
|
|
|
#if AC_FENCE == ENABLED
|
2016-05-19 05:35:44 -03:00
|
|
|
// reject destination if outside the fence
|
2019-01-01 22:54:31 -04:00
|
|
|
const Location dest_loc(destination);
|
2018-02-07 22:21:09 -04:00
|
|
|
if (!copter.fence.check_destination_within_fence(dest_loc)) {
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::NAVIGATION, LogErrorCode::DEST_OUTSIDE_FENCE);
|
2016-04-28 04:35:29 -03:00
|
|
|
// failure is propagated to GCS with NAK
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-19 05:35:44 -03:00
|
|
|
#endif
|
2016-04-28 04:35:29 -03:00
|
|
|
|
2020-09-08 03:16:14 -03:00
|
|
|
// ensure we are in position control mode
|
|
|
|
if (guided_mode != Guided_WP) {
|
|
|
|
pos_control_start();
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:20:14 -03:00
|
|
|
// set yaw state
|
2016-03-21 23:13:34 -03:00
|
|
|
set_yaw_state(use_yaw, yaw_cd, use_yaw_rate, yaw_rate_cds, relative_yaw);
|
2017-06-22 12:20:14 -03:00
|
|
|
|
2016-05-19 05:35:44 -03:00
|
|
|
// no need to check return status because terrain data is not used
|
2020-02-16 05:42:31 -04:00
|
|
|
wp_nav->set_wp_destination(destination, terrain_alt);
|
2016-01-10 22:41:28 -04:00
|
|
|
|
|
|
|
// log target
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.Log_Write_GuidedTarget(guided_mode, destination, Vector3f());
|
2021-03-05 03:03:56 -04:00
|
|
|
|
|
|
|
send_notification = true;
|
|
|
|
|
2016-04-28 04:35:29 -03:00
|
|
|
return true;
|
2014-01-28 09:44:37 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::get_wp(Location& destination)
|
2018-05-11 08:59:05 -03:00
|
|
|
{
|
|
|
|
if (guided_mode != Guided_WP) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-05 07:47:32 -03:00
|
|
|
return wp_nav->get_oa_wp_destination(destination);
|
2018-05-11 08:59:05 -03:00
|
|
|
}
|
|
|
|
|
2016-03-11 04:16:26 -04:00
|
|
|
// sets guided mode's target from a Location object
|
|
|
|
// returns false if destination could not be set (probably caused by missing terrain data)
|
2016-04-28 04:35:29 -03:00
|
|
|
// or if the fence is enabled and guided waypoint is outside the fence
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::set_destination(const Location& dest_loc, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw)
|
2016-03-11 04:16:26 -04:00
|
|
|
{
|
2016-04-28 04:35:29 -03:00
|
|
|
#if AC_FENCE == ENABLED
|
2016-05-19 05:35:44 -03:00
|
|
|
// reject destination outside the fence.
|
|
|
|
// Note: there is a danger that a target specified as a terrain altitude might not be checked if the conversion to alt-above-home fails
|
2018-02-07 22:21:09 -04:00
|
|
|
if (!copter.fence.check_destination_within_fence(dest_loc)) {
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::NAVIGATION, LogErrorCode::DEST_OUTSIDE_FENCE);
|
2016-07-02 05:15:05 -03:00
|
|
|
// failure is propagated to GCS with NAK
|
|
|
|
return false;
|
2016-05-19 05:35:44 -03:00
|
|
|
}
|
2016-04-28 04:35:29 -03:00
|
|
|
#endif
|
|
|
|
|
2020-09-08 03:16:14 -03:00
|
|
|
// ensure we are in position control mode
|
|
|
|
if (guided_mode != Guided_WP) {
|
|
|
|
pos_control_start();
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!wp_nav->set_wp_destination(dest_loc)) {
|
2016-05-19 05:35:44 -03:00
|
|
|
// failure to set destination can only be because of missing terrain data
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::NAVIGATION, LogErrorCode::FAILED_TO_SET_DESTINATION);
|
2016-04-22 08:46:36 -03:00
|
|
|
// failure is propagated to GCS with NAK
|
2016-03-11 04:16:26 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:20:14 -03:00
|
|
|
// set yaw state
|
2016-03-21 23:13:34 -03:00
|
|
|
set_yaw_state(use_yaw, yaw_cd, use_yaw_rate, yaw_rate_cds, relative_yaw);
|
2017-06-22 12:20:14 -03:00
|
|
|
|
2016-03-11 04:16:26 -04:00
|
|
|
// log target
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.Log_Write_GuidedTarget(guided_mode, Vector3f(dest_loc.lat, dest_loc.lng, dest_loc.alt),Vector3f());
|
2021-03-05 03:03:56 -04:00
|
|
|
|
|
|
|
send_notification = true;
|
|
|
|
|
2016-03-11 04:16:26 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-02 06:06:11 -03:00
|
|
|
// guided_set_velocity - sets guided mode's target velocity
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::set_velocity(const Vector3f& velocity, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw, bool log_request)
|
2014-06-02 06:06:11 -03:00
|
|
|
{
|
|
|
|
// check we are in velocity control mode
|
|
|
|
if (guided_mode != Guided_Velocity) {
|
2016-03-21 23:13:34 -03:00
|
|
|
vel_control_start();
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:20:14 -03:00
|
|
|
// set yaw state
|
2016-03-21 23:13:34 -03:00
|
|
|
set_yaw_state(use_yaw, yaw_cd, use_yaw_rate, yaw_rate_cds, relative_yaw);
|
2017-06-22 12:20:14 -03:00
|
|
|
|
2016-07-20 08:45:11 -03:00
|
|
|
// record velocity target
|
|
|
|
guided_vel_target_cms = velocity;
|
2015-08-11 20:36:42 -03:00
|
|
|
vel_update_time_ms = millis();
|
|
|
|
|
2016-01-10 22:41:28 -04:00
|
|
|
// log target
|
2018-02-06 02:16:09 -04:00
|
|
|
if (log_request) {
|
|
|
|
copter.Log_Write_GuidedTarget(guided_mode, Vector3f(), velocity);
|
|
|
|
}
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
|
2014-11-25 16:15:45 -04:00
|
|
|
// set guided mode posvel target
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::set_destination_posvel(const Vector3f& destination, const Vector3f& velocity, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw)
|
2017-07-11 02:06:18 -03:00
|
|
|
{
|
2017-11-27 08:21:44 -04:00
|
|
|
#if AC_FENCE == ENABLED
|
|
|
|
// reject destination if outside the fence
|
2019-01-01 22:54:31 -04:00
|
|
|
const Location dest_loc(destination);
|
2018-02-07 22:21:09 -04:00
|
|
|
if (!copter.fence.check_destination_within_fence(dest_loc)) {
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::NAVIGATION, LogErrorCode::DEST_OUTSIDE_FENCE);
|
2017-11-27 08:21:44 -04:00
|
|
|
// failure is propagated to GCS with NAK
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-08 03:16:14 -03:00
|
|
|
// check we are in velocity control mode
|
|
|
|
if (guided_mode != Guided_PosVel) {
|
|
|
|
posvel_control_start();
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:20:14 -03:00
|
|
|
// set yaw state
|
2016-03-21 23:13:34 -03:00
|
|
|
set_yaw_state(use_yaw, yaw_cd, use_yaw_rate, yaw_rate_cds, relative_yaw);
|
2017-06-22 12:20:14 -03:00
|
|
|
|
2014-11-25 16:15:45 -04:00
|
|
|
posvel_update_time_ms = millis();
|
2016-07-21 04:14:21 -03:00
|
|
|
guided_pos_target_cm = destination;
|
|
|
|
guided_vel_target_cms = velocity;
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.pos_control->set_pos_target(guided_pos_target_cm);
|
2016-01-10 22:41:28 -04:00
|
|
|
|
|
|
|
// log target
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.Log_Write_GuidedTarget(guided_mode, destination, velocity);
|
2017-11-27 08:21:44 -04:00
|
|
|
return true;
|
2014-11-25 16:15:45 -04:00
|
|
|
}
|
|
|
|
|
2020-07-27 06:18:34 -03:00
|
|
|
// set guided mode angle target and climbrate
|
|
|
|
void ModeGuided::set_angle(const Quaternion &q, float climb_rate_cms_or_thrust, bool use_yaw_rate, float yaw_rate_rads, bool use_thrust)
|
2015-10-08 08:13:53 -03:00
|
|
|
{
|
|
|
|
// check we are in velocity control mode
|
|
|
|
if (guided_mode != Guided_Angle) {
|
2016-03-21 23:13:34 -03:00
|
|
|
angle_control_start();
|
2015-10-08 08:13:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert quaternion to euler angles
|
|
|
|
q.to_euler(guided_angle_state.roll_cd, guided_angle_state.pitch_cd, guided_angle_state.yaw_cd);
|
|
|
|
guided_angle_state.roll_cd = ToDeg(guided_angle_state.roll_cd) * 100.0f;
|
|
|
|
guided_angle_state.pitch_cd = ToDeg(guided_angle_state.pitch_cd) * 100.0f;
|
2016-04-27 06:35:18 -03:00
|
|
|
guided_angle_state.yaw_cd = wrap_180_cd(ToDeg(guided_angle_state.yaw_cd) * 100.0f);
|
2016-08-02 14:20:57 -03:00
|
|
|
guided_angle_state.yaw_rate_cds = ToDeg(yaw_rate_rads) * 100.0f;
|
|
|
|
guided_angle_state.use_yaw_rate = use_yaw_rate;
|
2015-10-08 08:13:53 -03:00
|
|
|
|
2020-07-27 06:18:34 -03:00
|
|
|
guided_angle_state.use_thrust = use_thrust;
|
|
|
|
if (use_thrust) {
|
|
|
|
guided_angle_state.thrust = climb_rate_cms_or_thrust;
|
|
|
|
guided_angle_state.climb_rate_cms = 0.0f;
|
|
|
|
} else {
|
|
|
|
guided_angle_state.thrust = 0.0f;
|
|
|
|
guided_angle_state.climb_rate_cms = climb_rate_cms_or_thrust;
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
guided_angle_state.update_time_ms = millis();
|
2016-01-10 22:41:28 -04:00
|
|
|
|
|
|
|
// log target
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.Log_Write_GuidedTarget(guided_mode,
|
2016-01-10 22:41:28 -04:00
|
|
|
Vector3f(guided_angle_state.roll_cd, guided_angle_state.pitch_cd, guided_angle_state.yaw_cd),
|
2020-07-27 06:18:34 -03:00
|
|
|
Vector3f(0.0f, 0.0f, climb_rate_cms_or_thrust));
|
2015-10-08 08:13:53 -03:00
|
|
|
}
|
|
|
|
|
2014-08-04 11:11:23 -03:00
|
|
|
// guided_takeoff_run - takeoff in guided mode
|
|
|
|
// called by guided_run at 100hz or more
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::takeoff_run()
|
2018-06-26 00:16:31 -03:00
|
|
|
{
|
|
|
|
auto_takeoff_run();
|
2018-07-25 14:17:23 -03:00
|
|
|
if (wp_nav->reached_wp_destination()) {
|
2021-02-06 23:29:33 -04:00
|
|
|
#if LANDING_GEAR_ENABLED == ENABLED
|
2020-02-20 23:08:26 -04:00
|
|
|
// optionally retract landing gear
|
|
|
|
copter.landinggear.retract_after_takeoff();
|
2021-02-06 23:29:33 -04:00
|
|
|
#endif
|
2020-02-20 23:08:26 -04:00
|
|
|
|
|
|
|
// switch to position control mode but maintain current target
|
2020-04-22 02:31:09 -03:00
|
|
|
const Vector3f target = wp_nav->get_wp_destination();
|
|
|
|
set_destination(target, false, 0, false, 0, false, wp_nav->origin_and_destination_are_terrain_alt());
|
2018-07-25 14:17:23 -03:00
|
|
|
}
|
2018-06-26 00:16:31 -03:00
|
|
|
}
|
|
|
|
|
2014-06-02 06:06:11 -03:00
|
|
|
// guided_pos_control_run - runs the guided position controller
|
|
|
|
// called from guided_run
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::pos_control_run()
|
2014-06-02 06:06:11 -03:00
|
|
|
{
|
2014-01-28 09:44:37 -04:00
|
|
|
// process pilot's yaw input
|
|
|
|
float target_yaw_rate = 0;
|
2020-11-12 20:15:53 -04:00
|
|
|
if (!copter.failsafe.radio && use_pilot_yaw()) {
|
2014-01-28 09:44:37 -04:00
|
|
|
// get pilot's desired yaw rate
|
ArduCopter: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:46:59 -03:00
|
|
|
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
2015-05-04 23:34:21 -03:00
|
|
|
if (!is_zero(target_yaw_rate)) {
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2014-01-28 09:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2019-04-08 05:15:57 -03:00
|
|
|
if (is_disarmed_or_landed()) {
|
2019-03-14 20:45:00 -03:00
|
|
|
make_safe_spool_down();
|
2019-02-28 05:03:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 03:10:45 -04:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-01-13 03:10:45 -04:00
|
|
|
|
2014-01-28 09:44:37 -04:00
|
|
|
// run waypoint controller
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.failsafe_terrain_set_status(wp_nav->update_wpnav());
|
2014-01-28 09:44:37 -04:00
|
|
|
|
|
|
|
// call z-axis position controller (wpnav should have already updated it's alt target)
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->update_z_controller();
|
2014-01-28 09:44:37 -04:00
|
|
|
|
|
|
|
// call attitude controller
|
2017-12-25 23:55:42 -04:00
|
|
|
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
|
2014-01-28 09:44:37 -04:00
|
|
|
// roll & pitch from waypoint controller, yaw rate from pilot
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), target_yaw_rate);
|
2017-12-25 23:55:42 -04:00
|
|
|
} else if (auto_yaw.mode() == AUTO_YAW_RATE) {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll & pitch from waypoint controller, yaw rate from mavlink command or mission item
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), auto_yaw.rate_cds());
|
2017-06-22 12:20:14 -03:00
|
|
|
} else {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll, pitch from waypoint controller, yaw heading from GCS or auto_heading()
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), auto_yaw.yaw(), true);
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// guided_vel_control_run - runs the guided velocity controller
|
|
|
|
// called from guided_run
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::vel_control_run()
|
2014-06-02 06:06:11 -03:00
|
|
|
{
|
|
|
|
// process pilot's yaw input
|
|
|
|
float target_yaw_rate = 0;
|
2020-11-12 20:15:53 -04:00
|
|
|
if (!copter.failsafe.radio && use_pilot_yaw()) {
|
2014-06-02 06:06:11 -03:00
|
|
|
// get pilot's desired yaw rate
|
ArduCopter: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:46:59 -03:00
|
|
|
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
2015-05-04 23:34:21 -03:00
|
|
|
if (!is_zero(target_yaw_rate)) {
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2014-06-02 06:06:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:26:35 -03:00
|
|
|
// landed with positive desired climb rate, initiate takeoff
|
|
|
|
if (motors->armed() && copter.ap.auto_armed && copter.ap.land_complete && is_positive(guided_vel_target_cms.z)) {
|
|
|
|
zero_throttle_and_relax_ac();
|
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
|
|
|
if (motors->get_spool_state() == AP_Motors::SpoolState::THROTTLE_UNLIMITED) {
|
|
|
|
set_land_complete(false);
|
|
|
|
set_throttle_takeoff();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2019-04-08 05:15:57 -03:00
|
|
|
if (is_disarmed_or_landed()) {
|
2019-03-14 20:45:00 -03:00
|
|
|
make_safe_spool_down();
|
2019-02-28 05:03:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 03:10:45 -04:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-01-13 03:10:45 -04:00
|
|
|
|
2017-07-11 02:39:53 -03:00
|
|
|
// set velocity to zero and stop rotating if no updates received for 3 seconds
|
2015-08-11 20:36:42 -03:00
|
|
|
uint32_t tnow = millis();
|
2017-07-11 02:39:53 -03:00
|
|
|
if (tnow - vel_update_time_ms > GUIDED_POSVEL_TIMEOUT_MS) {
|
|
|
|
if (!pos_control->get_desired_velocity().is_zero()) {
|
2016-03-21 23:13:34 -03:00
|
|
|
set_desired_velocity_with_accel_and_fence_limits(Vector3f(0.0f, 0.0f, 0.0f));
|
2017-07-11 02:39:53 -03:00
|
|
|
}
|
2017-12-25 23:55:42 -04:00
|
|
|
if (auto_yaw.mode() == AUTO_YAW_RATE) {
|
|
|
|
auto_yaw.set_rate(0.0f);
|
2017-07-11 02:39:53 -03:00
|
|
|
}
|
2016-07-20 08:45:11 -03:00
|
|
|
} else {
|
2016-03-21 23:13:34 -03:00
|
|
|
set_desired_velocity_with_accel_and_fence_limits(guided_vel_target_cms);
|
2015-08-11 20:36:42 -03:00
|
|
|
}
|
|
|
|
|
2015-11-17 02:51:25 -04:00
|
|
|
// call velocity controller which includes z axis controller
|
2018-09-05 03:47:50 -03:00
|
|
|
pos_control->update_vel_controller_xyz();
|
2014-06-02 06:06:11 -03:00
|
|
|
|
|
|
|
// call attitude controller
|
2017-12-25 23:55:42 -04:00
|
|
|
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
|
2014-06-02 06:06:11 -03:00
|
|
|
// roll & pitch from waypoint controller, yaw rate from pilot
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(pos_control->get_roll(), pos_control->get_pitch(), target_yaw_rate);
|
2017-12-25 23:55:42 -04:00
|
|
|
} else if (auto_yaw.mode() == AUTO_YAW_RATE) {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll & pitch from velocity controller, yaw rate from mavlink command or mission item
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(pos_control->get_roll(), pos_control->get_pitch(), auto_yaw.rate_cds());
|
2017-06-22 12:20:14 -03:00
|
|
|
} else {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll, pitch from waypoint controller, yaw heading from GCS or auto_heading()
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(pos_control->get_roll(), pos_control->get_pitch(), auto_yaw.yaw(), true);
|
2014-01-28 09:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-13 05:41:48 -03:00
|
|
|
|
2014-11-25 16:15:45 -04:00
|
|
|
// guided_posvel_control_run - runs the guided spline controller
|
|
|
|
// called from guided_run
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::posvel_control_run()
|
2014-11-25 16:15:45 -04:00
|
|
|
{
|
|
|
|
// process pilot's yaw input
|
|
|
|
float target_yaw_rate = 0;
|
|
|
|
|
2021-01-06 08:38:35 -04:00
|
|
|
if (!copter.failsafe.radio && use_pilot_yaw()) {
|
2014-11-25 16:15:45 -04:00
|
|
|
// get pilot's desired yaw rate
|
ArduCopter: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:46:59 -03:00
|
|
|
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
2015-05-04 23:34:21 -03:00
|
|
|
if (!is_zero(target_yaw_rate)) {
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode(AUTO_YAW_HOLD);
|
2014-11-25 16:15:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2019-04-08 05:15:57 -03:00
|
|
|
if (is_disarmed_or_landed()) {
|
2019-03-14 20:45:00 -03:00
|
|
|
make_safe_spool_down();
|
2019-02-28 05:03:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 03:10:45 -04:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-01-13 03:10:45 -04:00
|
|
|
|
2017-07-11 02:39:53 -03:00
|
|
|
// set velocity to zero and stop rotating if no updates received for 3 seconds
|
2014-11-25 16:15:45 -04:00
|
|
|
uint32_t tnow = millis();
|
2017-07-11 02:39:53 -03:00
|
|
|
if (tnow - posvel_update_time_ms > GUIDED_POSVEL_TIMEOUT_MS) {
|
2016-07-21 04:14:21 -03:00
|
|
|
guided_vel_target_cms.zero();
|
2017-12-25 23:55:42 -04:00
|
|
|
if (auto_yaw.mode() == AUTO_YAW_RATE) {
|
|
|
|
auto_yaw.set_rate(0.0f);
|
2017-07-11 02:39:53 -03:00
|
|
|
}
|
2014-11-25 16:15:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:51:16 -03:00
|
|
|
// calculate dt
|
2017-01-09 03:31:26 -04:00
|
|
|
float dt = pos_control->time_since_last_xy_update();
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2018-03-08 22:42:17 -04:00
|
|
|
// sanity check dt
|
|
|
|
if (dt >= 0.2f) {
|
|
|
|
dt = 0.0f;
|
|
|
|
}
|
2015-04-13 22:51:16 -03:00
|
|
|
|
2018-03-08 22:42:17 -04:00
|
|
|
// advance position target using velocity target
|
|
|
|
guided_pos_target_cm += guided_vel_target_cms * dt;
|
2015-04-13 22:51:16 -03:00
|
|
|
|
2018-03-08 22:42:17 -04:00
|
|
|
// send position and velocity targets to position controller
|
|
|
|
pos_control->set_pos_target(guided_pos_target_cm);
|
|
|
|
pos_control->set_desired_velocity_xy(guided_vel_target_cms.x, guided_vel_target_cms.y);
|
2014-11-25 16:15:45 -04:00
|
|
|
|
2018-03-08 22:42:17 -04:00
|
|
|
// run position controllers
|
2018-09-05 03:47:50 -03:00
|
|
|
pos_control->update_xy_controller();
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->update_z_controller();
|
2015-04-22 16:04:27 -03:00
|
|
|
|
2014-11-25 16:15:45 -04:00
|
|
|
// call attitude controller
|
2017-12-25 23:55:42 -04:00
|
|
|
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
|
2014-11-25 16:15:45 -04:00
|
|
|
// roll & pitch from waypoint controller, yaw rate from pilot
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(pos_control->get_roll(), pos_control->get_pitch(), target_yaw_rate);
|
2017-12-25 23:55:42 -04:00
|
|
|
} else if (auto_yaw.mode() == AUTO_YAW_RATE) {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll & pitch from position-velocity controller, yaw rate from mavlink command or mission item
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(pos_control->get_roll(), pos_control->get_pitch(), auto_yaw.rate_cds());
|
2017-06-22 12:20:14 -03:00
|
|
|
} else {
|
2017-07-10 09:51:43 -03:00
|
|
|
// roll, pitch from waypoint controller, yaw heading from GCS or auto_heading()
|
2017-12-25 23:55:42 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(pos_control->get_roll(), pos_control->get_pitch(), auto_yaw.yaw(), true);
|
2014-11-25 16:15:45 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 17:26:45 -04:00
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
// guided_angle_control_run - runs the guided angle controller
|
|
|
|
// called from guided_run
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::angle_control_run()
|
2015-10-08 08:13:53 -03:00
|
|
|
{
|
|
|
|
// constrain desired lean angles
|
|
|
|
float roll_in = guided_angle_state.roll_cd;
|
|
|
|
float pitch_in = guided_angle_state.pitch_cd;
|
2016-04-16 06:58:46 -03:00
|
|
|
float total_in = norm(roll_in, pitch_in);
|
2018-02-07 22:21:09 -04:00
|
|
|
float angle_max = MIN(attitude_control->get_althold_lean_angle_max(), copter.aparm.angle_max);
|
2015-10-08 08:13:53 -03:00
|
|
|
if (total_in > angle_max) {
|
|
|
|
float ratio = angle_max / total_in;
|
|
|
|
roll_in *= ratio;
|
|
|
|
pitch_in *= ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap yaw request
|
2016-04-27 06:35:18 -03:00
|
|
|
float yaw_in = wrap_180_cd(guided_angle_state.yaw_cd);
|
2021-02-22 13:05:28 -04:00
|
|
|
float yaw_rate_in = guided_angle_state.yaw_rate_cds;
|
2015-10-08 08:13:53 -03:00
|
|
|
|
2020-07-27 06:18:34 -03:00
|
|
|
float climb_rate_cms = 0.0f;
|
|
|
|
if (!guided_angle_state.use_thrust) {
|
|
|
|
// constrain climb rate
|
|
|
|
climb_rate_cms = constrain_float(guided_angle_state.climb_rate_cms, -fabsf(wp_nav->get_default_speed_down()), wp_nav->get_default_speed_up());
|
2015-10-08 08:13:53 -03:00
|
|
|
|
2020-07-27 06:18:34 -03:00
|
|
|
// get avoidance adjusted climb rate
|
|
|
|
climb_rate_cms = get_avoidance_adjusted_climbrate(climb_rate_cms);
|
|
|
|
}
|
2017-01-05 02:24:02 -04:00
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
// check for timeout - set lean angles and climb rate to zero if no updates received for 3 seconds
|
|
|
|
uint32_t tnow = millis();
|
2015-10-10 09:41:42 -03:00
|
|
|
if (tnow - guided_angle_state.update_time_ms > GUIDED_ATTITUDE_TIMEOUT_MS) {
|
2015-10-08 08:13:53 -03:00
|
|
|
roll_in = 0.0f;
|
|
|
|
pitch_in = 0.0f;
|
|
|
|
climb_rate_cms = 0.0f;
|
2016-08-02 14:20:57 -03:00
|
|
|
yaw_rate_in = 0.0f;
|
2020-07-27 06:18:34 -03:00
|
|
|
guided_angle_state.use_thrust = false;
|
2015-10-08 08:13:53 -03:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:29:33 -03:00
|
|
|
// interpret positive climb rate or thrust as triggering take-off
|
2020-10-19 02:49:32 -03:00
|
|
|
const bool positive_thrust_or_climbrate = is_positive(guided_angle_state.use_thrust ? guided_angle_state.thrust : climb_rate_cms);
|
|
|
|
if (motors->armed() && positive_thrust_or_climbrate) {
|
2020-10-14 02:29:33 -03:00
|
|
|
copter.set_auto_armed(true);
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2020-10-19 02:49:32 -03:00
|
|
|
if (!motors->armed() || !copter.ap.auto_armed || (copter.ap.land_complete && !positive_thrust_or_climbrate)) {
|
2019-03-14 20:45:00 -03:00
|
|
|
make_safe_spool_down();
|
2019-02-28 05:03:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use get_alt_hold_state
|
|
|
|
// landed with positive desired climb rate, takeoff
|
2019-05-09 23:18:49 -03:00
|
|
|
if (copter.ap.land_complete && (guided_angle_state.climb_rate_cms > 0.0f)) {
|
2019-02-28 05:03:23 -04:00
|
|
|
zero_throttle_and_relax_ac();
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
|
|
|
if (motors->get_spool_state() == AP_Motors::SpoolState::THROTTLE_UNLIMITED) {
|
2019-02-28 05:03:23 -04:00
|
|
|
set_land_complete(false);
|
|
|
|
set_throttle_takeoff();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 03:10:45 -04:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-01-13 03:10:45 -04:00
|
|
|
|
2015-10-08 08:13:53 -03:00
|
|
|
// call attitude controller
|
2016-08-02 14:20:57 -03:00
|
|
|
if (guided_angle_state.use_yaw_rate) {
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(roll_in, pitch_in, yaw_rate_in);
|
2016-08-02 14:20:57 -03:00
|
|
|
} else {
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(roll_in, pitch_in, yaw_in, true);
|
2016-08-02 14:20:57 -03:00
|
|
|
}
|
2015-10-08 08:13:53 -03:00
|
|
|
|
|
|
|
// call position controller
|
2020-07-27 06:18:34 -03:00
|
|
|
if (guided_angle_state.use_thrust) {
|
|
|
|
attitude_control->set_throttle_out(guided_angle_state.thrust, true, copter.g.throttle_filt);
|
|
|
|
} else {
|
|
|
|
pos_control->set_alt_target_from_climb_rate_ff(climb_rate_cms, G_Dt, false);
|
|
|
|
pos_control->update_z_controller();
|
|
|
|
}
|
2015-10-08 08:13:53 -03:00
|
|
|
}
|
|
|
|
|
2016-07-20 08:45:11 -03:00
|
|
|
// helper function to update position controller's desired velocity while respecting acceleration limits
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::set_desired_velocity_with_accel_and_fence_limits(const Vector3f& vel_des)
|
2016-07-20 08:45:11 -03:00
|
|
|
{
|
|
|
|
// get current desired velocity
|
2017-01-09 03:31:26 -04:00
|
|
|
Vector3f curr_vel_des = pos_control->get_desired_velocity();
|
2016-07-20 08:45:11 -03:00
|
|
|
|
|
|
|
// get change in desired velocity
|
|
|
|
Vector3f vel_delta = vel_des - curr_vel_des;
|
|
|
|
|
|
|
|
// limit xy change
|
|
|
|
float vel_delta_xy = safe_sqrt(sq(vel_delta.x)+sq(vel_delta.y));
|
2018-09-20 02:45:02 -03:00
|
|
|
float vel_delta_xy_max = G_Dt * pos_control->get_max_accel_xy();
|
2016-07-20 08:45:11 -03:00
|
|
|
float ratio_xy = 1.0f;
|
|
|
|
if (!is_zero(vel_delta_xy) && (vel_delta_xy > vel_delta_xy_max)) {
|
|
|
|
ratio_xy = vel_delta_xy_max / vel_delta_xy;
|
|
|
|
}
|
2016-07-24 23:47:44 -03:00
|
|
|
curr_vel_des.x += (vel_delta.x * ratio_xy);
|
|
|
|
curr_vel_des.y += (vel_delta.y * ratio_xy);
|
2016-07-20 08:45:11 -03:00
|
|
|
|
|
|
|
// limit z change
|
2018-09-20 02:45:02 -03:00
|
|
|
float vel_delta_z_max = G_Dt * pos_control->get_max_accel_z();
|
2016-07-24 23:40:08 -03:00
|
|
|
curr_vel_des.z += constrain_float(vel_delta.z, -vel_delta_z_max, vel_delta_z_max);
|
2016-07-20 08:45:11 -03:00
|
|
|
|
2017-01-02 20:36:26 -04:00
|
|
|
#if AC_AVOID_ENABLED
|
2020-12-06 08:48:21 -04:00
|
|
|
// limit the velocity for obstacle/fence avoidance
|
|
|
|
copter.avoid.adjust_velocity(curr_vel_des, pos_control->get_pos_xy_p().kP(), pos_control->get_max_accel_xy(), pos_control->get_pos_z_p().kP(), pos_control->get_max_accel_z(), G_Dt);
|
2017-01-02 20:36:26 -04:00
|
|
|
#endif
|
2016-07-24 23:47:44 -03:00
|
|
|
|
2016-07-20 08:45:11 -03:00
|
|
|
// update position controller with new target
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_desired_velocity(curr_vel_des);
|
2016-07-20 08:45:11 -03:00
|
|
|
}
|
|
|
|
|
2017-07-10 09:51:43 -03:00
|
|
|
// helper function to set yaw state and targets
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::set_yaw_state(bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_angle)
|
2017-06-22 12:20:14 -03:00
|
|
|
{
|
2017-07-10 09:51:43 -03:00
|
|
|
if (use_yaw) {
|
2019-04-23 14:26:30 -03:00
|
|
|
auto_yaw.set_fixed_yaw(yaw_cd * 0.01f, 0.0f, 0, relative_angle);
|
2017-07-10 09:51:43 -03:00
|
|
|
} else if (use_yaw_rate) {
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_rate(yaw_rate_cds);
|
2017-07-10 09:51:43 -03:00
|
|
|
}
|
2017-06-22 12:20:14 -03:00
|
|
|
}
|
|
|
|
|
2020-11-12 20:15:53 -04:00
|
|
|
// returns true if pilot's yaw input should be used to adjust vehicle's heading
|
|
|
|
bool ModeGuided::use_pilot_yaw(void) const
|
|
|
|
{
|
|
|
|
return (copter.g2.guided_options.get() & uint32_t(Options::IgnorePilotYaw)) == 0;
|
|
|
|
}
|
|
|
|
|
2014-10-13 05:41:48 -03:00
|
|
|
// Guided Limit code
|
|
|
|
|
|
|
|
// guided_limit_clear - clear/turn off guided limits
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::limit_clear()
|
2014-10-13 05:41:48 -03:00
|
|
|
{
|
|
|
|
guided_limit.timeout_ms = 0;
|
|
|
|
guided_limit.alt_min_cm = 0.0f;
|
|
|
|
guided_limit.alt_max_cm = 0.0f;
|
|
|
|
guided_limit.horiz_max_cm = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// guided_limit_set - set guided timeout and movement limits
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::limit_set(uint32_t timeout_ms, float alt_min_cm, float alt_max_cm, float horiz_max_cm)
|
2014-10-13 05:41:48 -03:00
|
|
|
{
|
|
|
|
guided_limit.timeout_ms = timeout_ms;
|
|
|
|
guided_limit.alt_min_cm = alt_min_cm;
|
|
|
|
guided_limit.alt_max_cm = alt_max_cm;
|
|
|
|
guided_limit.horiz_max_cm = horiz_max_cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// guided_limit_init_time_and_pos - initialise guided start time and position as reference for limit checking
|
|
|
|
// only called from AUTO mode's auto_nav_guided_start function
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeGuided::limit_init_time_and_pos()
|
2014-10-13 05:41:48 -03:00
|
|
|
{
|
|
|
|
// initialise start time
|
2015-11-19 23:04:45 -04:00
|
|
|
guided_limit.start_time = AP_HAL::millis();
|
2014-10-13 05:41:48 -03:00
|
|
|
|
|
|
|
// initialise start position from current position
|
|
|
|
guided_limit.start_pos = inertial_nav.get_position();
|
|
|
|
}
|
|
|
|
|
|
|
|
// guided_limit_check - returns true if guided mode has breached a limit
|
|
|
|
// used when guided is invoked from the NAV_GUIDED_ENABLE mission command
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeGuided::limit_check()
|
2014-10-13 05:41:48 -03:00
|
|
|
{
|
|
|
|
// check if we have passed the timeout
|
|
|
|
if ((guided_limit.timeout_ms > 0) && (millis() - guided_limit.start_time >= guided_limit.timeout_ms)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current location
|
|
|
|
const Vector3f& curr_pos = inertial_nav.get_position();
|
|
|
|
|
|
|
|
// check if we have gone below min alt
|
2015-05-04 23:34:21 -03:00
|
|
|
if (!is_zero(guided_limit.alt_min_cm) && (curr_pos.z < guided_limit.alt_min_cm)) {
|
2014-10-13 05:41:48 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have gone above max alt
|
2015-05-04 23:34:21 -03:00
|
|
|
if (!is_zero(guided_limit.alt_max_cm) && (curr_pos.z > guided_limit.alt_max_cm)) {
|
2014-10-13 05:41:48 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have gone beyond horizontal limit
|
|
|
|
if (guided_limit.horiz_max_cm > 0.0f) {
|
2017-12-03 17:07:39 -04:00
|
|
|
float horiz_move = get_horizontal_distance_cm(guided_limit.start_pos, curr_pos);
|
2014-10-13 05:41:48 -03:00
|
|
|
if (horiz_move > guided_limit.horiz_max_cm) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got this far we must be within limits
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 21:28:32 -04:00
|
|
|
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
uint32_t ModeGuided::wp_distance() const
|
2017-12-05 21:28:32 -04:00
|
|
|
{
|
|
|
|
switch(mode()) {
|
|
|
|
case Guided_WP:
|
|
|
|
return wp_nav->get_wp_distance_to_destination();
|
|
|
|
break;
|
|
|
|
case Guided_PosVel:
|
|
|
|
return pos_control->get_distance_to_target();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
int32_t ModeGuided::wp_bearing() const
|
2017-12-05 21:28:32 -04:00
|
|
|
{
|
|
|
|
switch(mode()) {
|
|
|
|
case Guided_WP:
|
|
|
|
return wp_nav->get_wp_bearing_to_destination();
|
|
|
|
break;
|
|
|
|
case Guided_PosVel:
|
|
|
|
return pos_control->get_bearing_to_target();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 12:34:24 -03:00
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
float ModeGuided::crosstrack_error() const
|
2018-05-23 12:34:24 -03:00
|
|
|
{
|
|
|
|
if (mode() == Guided_WP) {
|
|
|
|
return wp_nav->crosstrack_error();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-02-25 10:32:52 -04:00
|
|
|
|
|
|
|
#endif
|