2017-07-26 14:14:40 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2024-09-04 00:48:37 -03:00
|
|
|
#if MODE_SMARTRTL_ENABLED
|
2018-02-21 23:58:28 -04:00
|
|
|
|
2017-07-26 14:14:40 -03:00
|
|
|
/*
|
2017-09-08 23:45:31 -03:00
|
|
|
* Init and run calls for Smart_RTL flight mode
|
2017-07-26 14:14:40 -03:00
|
|
|
*
|
2017-09-14 10:21:33 -03:00
|
|
|
* This code uses the SmartRTL path that is already in memory, and feeds it into WPNav, one point at a time.
|
2017-07-26 14:14:40 -03:00
|
|
|
* Once the copter is close to home, it will run a standard land controller.
|
|
|
|
*/
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeSmartRTL::init(bool ignore_checks)
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
2019-02-27 23:56:36 -04:00
|
|
|
if (g2.smart_rtl.is_active()) {
|
2017-07-26 14:14:40 -03:00
|
|
|
// initialise waypoint and spline controller
|
|
|
|
wp_nav->wp_and_spline_init();
|
|
|
|
|
|
|
|
// set current target to a reasonable stopping point
|
2021-06-17 22:20:26 -03:00
|
|
|
Vector3p stopping_point;
|
2021-06-21 04:22:48 -03:00
|
|
|
pos_control->get_stopping_point_xy_cm(stopping_point.xy());
|
|
|
|
pos_control->get_stopping_point_z_cm(stopping_point.z);
|
2021-06-17 22:20:26 -03:00
|
|
|
wp_nav->set_wp_destination(stopping_point.tofloat());
|
2017-07-26 14:14:40 -03:00
|
|
|
|
|
|
|
// initialise yaw to obey user parameter
|
2017-12-25 23:55:42 -04:00
|
|
|
auto_yaw.set_mode_to_default(true);
|
2017-07-26 14:14:40 -03:00
|
|
|
|
|
|
|
// wait for cleanup of return path
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::WAIT_FOR_PATH_CLEANUP;
|
2017-07-26 14:14:40 -03:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-30 23:23:31 -03:00
|
|
|
|
|
|
|
return false;
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
|
2017-09-08 23:45:31 -03:00
|
|
|
// perform cleanup required when leaving smart_rtl
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::exit()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
2024-07-23 09:43:59 -03:00
|
|
|
// restore last point if we hadn't reached it
|
|
|
|
if (smart_rtl_state == SubMode::PATH_FOLLOW && !dest_NED_backup.is_zero()) {
|
|
|
|
if (!g2.smart_rtl.add_point(dest_NED_backup)) {
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "SmartRTL: lost one point");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dest_NED_backup.zero();
|
|
|
|
|
2017-09-08 23:45:31 -03:00
|
|
|
g2.smart_rtl.cancel_request_for_thorough_cleanup();
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::run()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
2017-09-08 23:45:31 -03:00
|
|
|
switch (smart_rtl_state) {
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::WAIT_FOR_PATH_CLEANUP:
|
2017-09-10 20:46:51 -03:00
|
|
|
wait_cleanup_run();
|
2017-07-26 14:14:40 -03:00
|
|
|
break;
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::PATH_FOLLOW:
|
2017-09-10 20:46:51 -03:00
|
|
|
path_follow_run();
|
2017-07-26 14:14:40 -03:00
|
|
|
break;
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::PRELAND_POSITION:
|
2017-09-10 20:46:51 -03:00
|
|
|
pre_land_position_run();
|
2017-07-26 14:14:40 -03:00
|
|
|
break;
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::DESCEND:
|
2017-09-10 20:46:51 -03:00
|
|
|
descent_run(); // Re-using the descend method from normal rtl mode.
|
2017-07-26 14:14:40 -03:00
|
|
|
break;
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::LAND:
|
2017-09-10 20:46:51 -03:00
|
|
|
land_run(true); // Re-using the land method from normal rtl mode.
|
2017-07-26 14:14:40 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 20:31:08 -03:00
|
|
|
bool ModeSmartRTL::is_landing() const
|
|
|
|
{
|
2021-04-13 12:09:24 -03:00
|
|
|
return smart_rtl_state == SubMode::LAND;
|
2020-09-02 20:31:08 -03:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::wait_cleanup_run()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
|
|
|
// hover at current target position
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2017-07-26 14:14:40 -03:00
|
|
|
wp_nav->update_wpnav();
|
|
|
|
pos_control->update_z_controller();
|
2022-09-24 11:12:33 -03:00
|
|
|
attitude_control->input_thrust_vector_heading(pos_control->get_thrust_vector(), auto_yaw.get_heading());
|
2017-07-26 14:14:40 -03:00
|
|
|
|
|
|
|
// check if return path is computed and if yes, begin journey home
|
2017-09-08 23:45:31 -03:00
|
|
|
if (g2.smart_rtl.request_thorough_cleanup()) {
|
2020-10-22 21:28:41 -03:00
|
|
|
path_follow_last_pop_fail_ms = 0;
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::PATH_FOLLOW;
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::path_follow_run()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
|
|
|
// if we are close to current target point, switch the next point to be our target.
|
|
|
|
if (wp_nav->reached_wp_destination()) {
|
2024-07-23 09:43:59 -03:00
|
|
|
|
|
|
|
// clear destination backup so that it cannot be restored
|
|
|
|
dest_NED_backup.zero();
|
|
|
|
|
2020-10-22 21:28:41 -03:00
|
|
|
// this pop_point can fail if the IO task currently has the
|
|
|
|
// path semaphore.
|
2024-07-23 09:43:59 -03:00
|
|
|
Vector3f dest_NED;
|
2021-01-19 22:50:48 -04:00
|
|
|
if (g2.smart_rtl.pop_point(dest_NED)) {
|
2024-07-23 09:43:59 -03:00
|
|
|
// backup destination in case we exit smart_rtl mode and need to restore it to the path
|
|
|
|
dest_NED_backup = dest_NED;
|
2020-10-22 21:28:41 -03:00
|
|
|
path_follow_last_pop_fail_ms = 0;
|
2017-09-08 23:45:31 -03:00
|
|
|
if (g2.smart_rtl.get_num_points() == 0) {
|
2017-08-30 23:23:31 -03:00
|
|
|
// this is the very last point, add 2m to the target alt and move to pre-land state
|
2021-01-19 22:50:48 -04:00
|
|
|
dest_NED.z -= 2.0f;
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::PRELAND_POSITION;
|
2021-01-19 22:50:48 -04:00
|
|
|
wp_nav->set_wp_destination_NED(dest_NED);
|
2020-01-08 07:18:39 -04:00
|
|
|
} else {
|
2021-03-11 08:07:50 -04:00
|
|
|
// peek at the next point. this can fail if the IO task currently has the path semaphore
|
2021-01-19 22:50:48 -04:00
|
|
|
Vector3f next_dest_NED;
|
|
|
|
if (g2.smart_rtl.peek_point(next_dest_NED)) {
|
|
|
|
wp_nav->set_wp_destination_NED(dest_NED);
|
2021-02-08 23:51:38 -04:00
|
|
|
if (g2.smart_rtl.get_num_points() == 1) {
|
|
|
|
// this is the very last point, add 2m to the target alt
|
|
|
|
next_dest_NED.z -= 2.0f;
|
|
|
|
}
|
2021-01-19 22:50:48 -04:00
|
|
|
wp_nav->set_wp_destination_next_NED(next_dest_NED);
|
2020-01-08 07:18:39 -04:00
|
|
|
} else {
|
2021-03-11 08:07:50 -04:00
|
|
|
// this can only happen if peek failed to take the semaphore
|
|
|
|
// send next point anyway which will cause the vehicle to slow at the next point
|
2021-01-19 22:50:48 -04:00
|
|
|
wp_nav->set_wp_destination_NED(dest_NED);
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2020-01-08 07:18:39 -04:00
|
|
|
}
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
2020-10-22 21:28:41 -03:00
|
|
|
} else if (g2.smart_rtl.get_num_points() == 0) {
|
|
|
|
// We should never get here; should always have at least
|
|
|
|
// two points and the "zero points left" is handled above.
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::PRELAND_POSITION;
|
2020-10-22 21:28:41 -03:00
|
|
|
} else if (path_follow_last_pop_fail_ms == 0) {
|
|
|
|
// first time we've failed to pop off (ever, or after a success)
|
|
|
|
path_follow_last_pop_fail_ms = AP_HAL::millis();
|
|
|
|
} else if (AP_HAL::millis() - path_follow_last_pop_fail_ms > 10000) {
|
|
|
|
// we failed to pop a point off for 10 seconds. This is
|
|
|
|
// almost certainly a bug.
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::PRELAND_POSITION;
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update controllers
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2017-07-26 14:14:40 -03:00
|
|
|
wp_nav->update_wpnav();
|
|
|
|
pos_control->update_z_controller();
|
|
|
|
|
2022-09-24 11:12:33 -03:00
|
|
|
// call attitude controller with auto yaw
|
|
|
|
attitude_control->input_thrust_vector_heading(pos_control->get_thrust_vector(), auto_yaw.get_heading());
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::pre_land_position_run()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
|
|
|
// if we are close to 2m above start point, we are ready to land.
|
|
|
|
if (wp_nav->reached_wp_destination()) {
|
|
|
|
// choose descend and hold, or land based on user parameter rtl_alt_final
|
2017-09-10 20:46:51 -03:00
|
|
|
if (g.rtl_alt_final <= 0 || copter.failsafe.radio) {
|
|
|
|
land_start();
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::LAND;
|
2017-07-26 14:14:40 -03:00
|
|
|
} else {
|
2017-09-10 20:46:51 -03:00
|
|
|
set_descent_target_alt(copter.g.rtl_alt_final);
|
|
|
|
descent_start();
|
2021-04-13 12:09:24 -03:00
|
|
|
smart_rtl_state = SubMode::DESCEND;
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update controllers
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2017-07-26 14:14:40 -03:00
|
|
|
wp_nav->update_wpnav();
|
|
|
|
pos_control->update_z_controller();
|
2022-09-24 11:12:33 -03:00
|
|
|
attitude_control->input_thrust_vector_heading(pos_control->get_thrust_vector(), auto_yaw.get_heading());
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
|
|
|
|
2017-09-08 23:45:31 -03:00
|
|
|
// save current position for use by the smart_rtl flight mode
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeSmartRTL::save_position()
|
2017-07-26 14:14:40 -03:00
|
|
|
{
|
2020-01-30 03:29:36 -04:00
|
|
|
const bool should_save_position = motors->armed() && (copter.flightmode->mode_number() != Mode::Number::SMART_RTL);
|
2017-07-26 14:14:40 -03:00
|
|
|
|
2017-09-10 20:46:51 -03:00
|
|
|
copter.g2.smart_rtl.update(copter.position_ok(), should_save_position);
|
2017-07-26 14:14:40 -03:00
|
|
|
}
|
2018-02-07 22:21:09 -04:00
|
|
|
|
2021-07-06 18:02:26 -03:00
|
|
|
bool ModeSmartRTL::get_wp(Location& destination) const
|
2019-06-05 07:47:32 -03:00
|
|
|
{
|
|
|
|
// provide target in states which use wp_nav
|
|
|
|
switch (smart_rtl_state) {
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::WAIT_FOR_PATH_CLEANUP:
|
|
|
|
case SubMode::PATH_FOLLOW:
|
|
|
|
case SubMode::PRELAND_POSITION:
|
|
|
|
case SubMode::DESCEND:
|
2021-01-19 22:50:48 -04:00
|
|
|
return wp_nav->get_wp_destination_loc(destination);
|
2021-04-13 12:09:24 -03:00
|
|
|
case SubMode::LAND:
|
2019-06-05 07:47:32 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we should never get here but just in case
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
uint32_t ModeSmartRTL::wp_distance() const
|
2018-02-07 22:21:09 -04:00
|
|
|
{
|
|
|
|
return wp_nav->get_wp_distance_to_destination();
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
int32_t ModeSmartRTL::wp_bearing() const
|
2018-02-07 22:21:09 -04:00
|
|
|
{
|
|
|
|
return wp_nav->get_wp_bearing_to_destination();
|
|
|
|
}
|
2018-02-21 23:58:28 -04:00
|
|
|
|
2021-09-03 03:27:49 -03:00
|
|
|
bool ModeSmartRTL::use_pilot_yaw() const
|
|
|
|
{
|
2022-10-06 21:38:52 -03:00
|
|
|
const bool land_repositioning = g.land_repositioning && (smart_rtl_state == SubMode::DESCEND);
|
|
|
|
const bool final_landing = smart_rtl_state == SubMode::LAND;
|
|
|
|
return g2.smart_rtl.use_pilot_yaw() || land_repositioning || final_landing;
|
2021-09-03 03:27:49 -03:00
|
|
|
}
|
|
|
|
|
2018-02-21 23:58:28 -04:00
|
|
|
#endif
|