2019-01-15 13:46:13 -04:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Plane.h"
|
|
|
|
|
|
|
|
bool ModeRTL::_enter()
|
|
|
|
{
|
|
|
|
plane.prev_WP_loc = plane.current_loc;
|
2021-06-01 03:03:52 -03:00
|
|
|
plane.do_RTL(plane.get_RTL_altitude_cm());
|
2020-05-14 21:12:02 -03:00
|
|
|
plane.rtl.done_climb = false;
|
2021-09-10 03:28:21 -03:00
|
|
|
#if HAL_QUADPLANE_ENABLED
|
2024-08-06 21:58:28 -03:00
|
|
|
plane.vtol_approach_s.approach_stage = Plane::VTOLApproach::Stage::RTL;
|
2022-05-16 05:31:15 -03:00
|
|
|
|
2022-11-24 18:50:36 -04:00
|
|
|
// Quadplane specific checks
|
|
|
|
if (plane.quadplane.available()) {
|
|
|
|
// treat RTL as QLAND if we are in guided wait takeoff state, to cope
|
|
|
|
// with failsafes during GUIDED->AUTO takeoff sequence
|
|
|
|
if (plane.quadplane.guided_wait_takeoff_on_mode_enter) {
|
|
|
|
plane.set_mode(plane.mode_qland, ModeReason::QLAND_INSTEAD_OF_RTL);
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-15 13:46:13 -04:00
|
|
|
|
2022-11-24 18:50:36 -04:00
|
|
|
// if Q_RTL_MODE is QRTL always, immediately switch to QRTL mode
|
|
|
|
if (plane.quadplane.rtl_mode == QuadPlane::RTL_MODE::QRTL_ALWAYS) {
|
|
|
|
plane.set_mode(plane.mode_qrtl, ModeReason::QRTL_INSTEAD_OF_RTL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if VTOL landing is expected and quadplane motors are active and within QRTL radius and under QRTL altitude then switch to QRTL
|
|
|
|
const bool vtol_landing = (plane.quadplane.rtl_mode == QuadPlane::RTL_MODE::SWITCH_QRTL) || (plane.quadplane.rtl_mode == QuadPlane::RTL_MODE::VTOL_APPROACH_QRTL);
|
|
|
|
if (vtol_landing && (quadplane.motors->get_desired_spool_state() == AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED)) {
|
|
|
|
int32_t alt_cm;
|
2023-01-25 06:35:25 -04:00
|
|
|
if ((plane.current_loc.get_distance(plane.next_WP_loc) < plane.mode_qrtl.get_VTOL_return_radius()) &&
|
2022-11-24 18:50:36 -04:00
|
|
|
plane.current_loc.get_alt_cm(Location::AltFrame::ABOVE_HOME, alt_cm) && (alt_cm < plane.quadplane.qrtl_alt*100)) {
|
|
|
|
plane.set_mode(plane.mode_qrtl, ModeReason::QRTL_INSTEAD_OF_RTL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 03:28:21 -03:00
|
|
|
#endif
|
2021-01-30 16:14:19 -04:00
|
|
|
|
2019-01-15 13:46:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeRTL::update()
|
|
|
|
{
|
|
|
|
plane.calc_nav_roll();
|
|
|
|
plane.calc_nav_pitch();
|
|
|
|
plane.calc_throttle();
|
2020-05-14 21:12:02 -03:00
|
|
|
|
2021-03-12 13:21:49 -04:00
|
|
|
bool alt_threshold_reached = false;
|
2023-04-26 00:27:13 -03:00
|
|
|
if (plane.flight_option_enabled(FlightOptions::CLIMB_BEFORE_TURN)) {
|
2024-01-18 01:58:09 -04:00
|
|
|
// Climb to RTL_ALTITUDE before turning. This overrides RTL_CLIMB_MIN.
|
2021-03-12 13:21:49 -04:00
|
|
|
alt_threshold_reached = plane.current_loc.alt > plane.next_WP_loc.alt;
|
|
|
|
} else if (plane.g2.rtl_climb_min > 0) {
|
2020-05-14 21:12:02 -03:00
|
|
|
/*
|
2021-03-12 13:21:49 -04:00
|
|
|
when RTL first starts limit bank angle to LEVEL_ROLL_LIMIT
|
|
|
|
until we have climbed by RTL_CLIMB_MIN meters
|
|
|
|
*/
|
|
|
|
alt_threshold_reached = (plane.current_loc.alt - plane.prev_WP_loc.alt)*0.01 > plane.g2.rtl_climb_min;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plane.rtl.done_climb && alt_threshold_reached) {
|
|
|
|
plane.prev_WP_loc = plane.current_loc;
|
|
|
|
plane.setup_glide_slope();
|
|
|
|
plane.rtl.done_climb = true;
|
|
|
|
}
|
|
|
|
if (!plane.rtl.done_climb) {
|
|
|
|
// Constrain the roll limit as a failsafe, that way if something goes wrong the plane will
|
|
|
|
// eventually turn back and go to RTL instead of going perfectly straight. This also leaves
|
|
|
|
// some leeway for fighting wind.
|
|
|
|
plane.roll_limit_cd = MIN(plane.roll_limit_cd, plane.g.level_roll_limit*100);
|
|
|
|
plane.nav_roll_cd = constrain_int32(plane.nav_roll_cd, -plane.roll_limit_cd, plane.roll_limit_cd);
|
2020-05-14 21:12:02 -03:00
|
|
|
}
|
2019-01-15 13:46:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-19 04:51:00 -03:00
|
|
|
void ModeRTL::navigate()
|
2020-07-29 12:01:34 -03:00
|
|
|
{
|
2021-09-10 03:28:21 -03:00
|
|
|
#if HAL_QUADPLANE_ENABLED
|
2022-12-23 19:56:36 -04:00
|
|
|
if (plane.quadplane.available()) {
|
2022-11-24 18:50:36 -04:00
|
|
|
if (plane.quadplane.rtl_mode == QuadPlane::RTL_MODE::VTOL_APPROACH_QRTL) {
|
2021-03-23 18:27:37 -03:00
|
|
|
// VTOL approach landing
|
|
|
|
AP_Mission::Mission_Command cmd;
|
|
|
|
cmd.content.location = plane.next_WP_loc;
|
|
|
|
plane.verify_landing_vtol_approach(cmd);
|
2024-08-06 21:58:28 -03:00
|
|
|
if (plane.vtol_approach_s.approach_stage == Plane::VTOLApproach::Stage::VTOL_LANDING) {
|
2021-03-23 18:27:37 -03:00
|
|
|
plane.set_mode(plane.mode_qrtl, ModeReason::RTL_COMPLETE_SWITCHING_TO_VTOL_LAND_RTL);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-07-29 12:01:34 -03:00
|
|
|
|
2021-03-23 18:27:37 -03:00
|
|
|
if ((AP_HAL::millis() - plane.last_mode_change_ms > 1000) && switch_QRTL()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-29 12:01:34 -03:00
|
|
|
}
|
2021-09-10 03:28:21 -03:00
|
|
|
#endif
|
2020-07-29 12:01:34 -03:00
|
|
|
|
2024-03-06 02:44:23 -04:00
|
|
|
uint16_t radius = abs(plane.g.rtl_radius);
|
|
|
|
if (radius > 0) {
|
|
|
|
plane.loiter.direction = (plane.g.rtl_radius < 0) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
plane.update_loiter(radius);
|
|
|
|
|
2023-01-06 20:57:18 -04:00
|
|
|
if (!plane.auto_state.checked_for_autoland) {
|
|
|
|
if ((plane.g.rtl_autoland == RtlAutoland::RTL_IMMEDIATE_DO_LAND_START) ||
|
|
|
|
(plane.g.rtl_autoland == RtlAutoland::RTL_THEN_DO_LAND_START &&
|
|
|
|
plane.reached_loiter_target() &&
|
2024-11-10 07:56:58 -04:00
|
|
|
labs(plane.calc_altitude_error_cm()) < 1000)) {
|
2023-01-06 20:57:18 -04:00
|
|
|
// we've reached the RTL point, see if we have a landing sequence
|
2024-03-27 14:58:24 -03:00
|
|
|
if (plane.have_position && plane.mission.jump_to_landing_sequence(plane.current_loc)) {
|
2023-01-06 20:57:18 -04:00
|
|
|
// switch from RTL -> AUTO
|
|
|
|
plane.mission.set_force_resume(true);
|
|
|
|
if (plane.set_mode(plane.mode_auto, ModeReason::RTL_COMPLETE_SWITCHING_TO_FIXEDWING_AUTOLAND)) {
|
|
|
|
// return here so we don't change the radius and don't run the rtl update_loiter()
|
|
|
|
return;
|
|
|
|
}
|
2024-11-10 07:56:58 -04:00
|
|
|
// mode change failed, revert force resume flag
|
|
|
|
plane.mission.set_force_resume(false);
|
2023-01-06 20:57:18 -04:00
|
|
|
}
|
2020-07-29 12:01:34 -03:00
|
|
|
|
2023-01-06 20:57:18 -04:00
|
|
|
// prevent running the expensive jump_to_landing_sequence
|
|
|
|
// on every loop
|
|
|
|
plane.auto_state.checked_for_autoland = true;
|
2024-11-10 07:56:58 -04:00
|
|
|
|
|
|
|
} else if (plane.g.rtl_autoland == RtlAutoland::DO_RETURN_PATH_START) {
|
|
|
|
if (plane.have_position && plane.mission.jump_to_closest_mission_leg(plane.current_loc)) {
|
|
|
|
plane.mission.set_force_resume(true);
|
|
|
|
if (plane.set_mode(plane.mode_auto, ModeReason::RTL_COMPLETE_SWITCHING_TO_FIXEDWING_AUTOLAND)) {
|
|
|
|
// return here so we don't change the radius and don't run the rtl update_loiter()
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// mode change failed, revert force resume flag
|
|
|
|
plane.mission.set_force_resume(false);
|
2023-01-06 20:57:18 -04:00
|
|
|
}
|
2024-11-10 07:56:58 -04:00
|
|
|
plane.auto_state.checked_for_autoland = true;
|
|
|
|
}
|
2020-07-29 12:01:34 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 03:28:21 -03:00
|
|
|
#if HAL_QUADPLANE_ENABLED
|
2021-01-30 16:14:19 -04:00
|
|
|
// Switch to QRTL if enabled and within radius
|
2022-11-24 18:50:36 -04:00
|
|
|
bool ModeRTL::switch_QRTL()
|
2021-09-10 03:28:21 -03:00
|
|
|
{
|
2022-11-24 18:50:36 -04:00
|
|
|
if (plane.quadplane.rtl_mode != QuadPlane::RTL_MODE::SWITCH_QRTL) {
|
2021-01-30 16:14:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t qrtl_radius = abs(plane.g.rtl_radius);
|
|
|
|
if (qrtl_radius == 0) {
|
|
|
|
qrtl_radius = abs(plane.aparm.loiter_radius);
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:50:36 -04:00
|
|
|
if (plane.nav_controller->reached_loiter_target() ||
|
2021-01-30 16:14:19 -04:00
|
|
|
plane.current_loc.past_interval_finish_line(plane.prev_WP_loc, plane.next_WP_loc) ||
|
|
|
|
plane.auto_state.wp_distance < MAX(qrtl_radius, plane.quadplane.stopping_distance())) {
|
|
|
|
/*
|
|
|
|
for a quadplane in RTL mode we switch to QRTL when we
|
|
|
|
are within the maximum of the stopping distance and the
|
|
|
|
RTL_RADIUS
|
|
|
|
*/
|
|
|
|
plane.set_mode(plane.mode_qrtl, ModeReason::RTL_COMPLETE_SWITCHING_TO_VTOL_LAND_RTL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-10 03:28:21 -03:00
|
|
|
|
|
|
|
#endif // HAL_QUADPLANE_ENABLED
|