2019-01-15 13:46:13 -04:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Plane.h"
|
|
|
|
|
2021-09-10 03:28:21 -03:00
|
|
|
#if HAL_QUADPLANE_ENABLED
|
|
|
|
|
2019-01-15 13:46:13 -04:00
|
|
|
bool ModeQRTL::_enter()
|
2021-08-14 15:57:45 -03:00
|
|
|
{
|
2022-05-16 05:31:15 -03:00
|
|
|
// treat QRTL 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;
|
|
|
|
}
|
2022-05-30 22:38:55 -03:00
|
|
|
submode = SubMode::RTL;
|
|
|
|
plane.prev_WP_loc = plane.current_loc;
|
|
|
|
|
2023-01-11 20:56:15 -04:00
|
|
|
int32_t RTL_alt_abs_cm = plane.home.alt + quadplane.qrtl_alt*100UL;
|
2022-05-30 22:38:55 -03:00
|
|
|
if (quadplane.motors->get_desired_spool_state() == AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED) {
|
|
|
|
// VTOL motors are active, either in VTOL flight or assisted flight
|
2023-04-07 05:25:34 -03:00
|
|
|
Location destination = plane.calc_best_rally_or_home_location(plane.current_loc, RTL_alt_abs_cm);
|
2022-05-30 22:38:55 -03:00
|
|
|
const float dist = plane.current_loc.get_distance(destination);
|
2023-01-25 06:35:25 -04:00
|
|
|
const float radius = get_VTOL_return_radius();
|
2022-05-30 22:38:55 -03:00
|
|
|
|
2023-01-25 06:35:25 -04:00
|
|
|
// Climb at least to a cone around home of hight of QRTL alt and radius of radius
|
2023-01-12 20:35:09 -04:00
|
|
|
// Always climb up to at least Q_RTL_ALT_MIN, constrain Q_RTL_ALT_MIN between Q_LAND_FINAL_ALT and Q_RTL_ALT
|
|
|
|
const float min_climb = constrain_float(quadplane.qrtl_alt_min, quadplane.land_final_alt, quadplane.qrtl_alt);
|
2023-01-25 06:35:25 -04:00
|
|
|
const float target_alt = MAX(quadplane.qrtl_alt * (dist / MAX(radius, dist)), min_climb);
|
2023-01-11 20:56:15 -04:00
|
|
|
|
2022-05-30 22:38:55 -03:00
|
|
|
|
2023-01-11 20:56:15 -04:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
|
|
|
const bool use_terrain = plane.terrain_enabled_in_mode(mode_number());
|
|
|
|
#else
|
|
|
|
const bool use_terrain = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const float dist_to_climb = target_alt - plane.relative_ground_altitude(plane.g.rangefinder_landing, use_terrain);
|
|
|
|
if (is_positive(dist_to_climb)) {
|
2022-05-30 22:38:55 -03:00
|
|
|
// climb before returning, only next waypoint altitude is used
|
|
|
|
submode = SubMode::climb;
|
|
|
|
plane.next_WP_loc = plane.current_loc;
|
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2023-01-11 20:56:15 -04:00
|
|
|
int32_t curent_alt_terrain_cm;
|
|
|
|
if (use_terrain && plane.current_loc.get_alt_cm(Location::AltFrame::ABOVE_TERRAIN, curent_alt_terrain_cm)) {
|
|
|
|
plane.next_WP_loc.set_alt_cm(curent_alt_terrain_cm + dist_to_climb * 100UL, Location::AltFrame::ABOVE_TERRAIN);
|
2022-05-30 22:38:55 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
plane.next_WP_loc.set_alt_cm(plane.current_loc.alt + dist_to_climb * 100UL, plane.current_loc.get_alt_frame());
|
|
|
|
return true;
|
2023-01-11 20:56:15 -04:00
|
|
|
|
2023-01-25 06:35:25 -04:00
|
|
|
} else if (dist < radius) {
|
2023-01-11 20:56:15 -04:00
|
|
|
// Above home "cone", return at curent altitude if lower than QRTL alt
|
|
|
|
int32_t current_alt_abs_cm;
|
|
|
|
if (plane.current_loc.get_alt_cm(Location::AltFrame::ABSOLUTE, current_alt_abs_cm)) {
|
|
|
|
RTL_alt_abs_cm = MIN(RTL_alt_abs_cm, current_alt_abs_cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're close to destination and already running VTOL motors, don't transition and don't climb
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"VTOL position1 d=%.1f r=%.1f", dist, radius);
|
|
|
|
poscontrol.set_state(QuadPlane::QPOS_POSITION1);
|
2022-05-30 22:38:55 -03:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 05:31:15 -03:00
|
|
|
|
2021-08-14 15:57:45 -03:00
|
|
|
// use do_RTL() to setup next_WP_loc
|
2022-05-30 22:38:55 -03:00
|
|
|
plane.do_RTL(RTL_alt_abs_cm);
|
2021-08-14 15:57:45 -03:00
|
|
|
quadplane.poscontrol_init_approach();
|
2022-05-30 22:38:55 -03:00
|
|
|
|
2021-08-14 15:57:45 -03:00
|
|
|
int32_t from_alt;
|
|
|
|
int32_t to_alt;
|
|
|
|
if (plane.current_loc.get_alt_cm(Location::AltFrame::ABSOLUTE,from_alt) && plane.next_WP_loc.get_alt_cm(Location::AltFrame::ABSOLUTE,to_alt)) {
|
|
|
|
poscontrol.slow_descent = from_alt > to_alt;
|
2021-09-04 16:21:11 -03:00
|
|
|
return true;
|
2021-08-14 15:57:45 -03:00
|
|
|
}
|
2022-05-30 22:38:55 -03:00
|
|
|
// default back to old method
|
2021-08-14 15:57:45 -03:00
|
|
|
poscontrol.slow_descent = (plane.current_loc.alt > plane.next_WP_loc.alt);
|
2021-09-04 16:21:11 -03:00
|
|
|
return true;
|
2021-08-14 15:57:45 -03:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:46:13 -04:00
|
|
|
void ModeQRTL::update()
|
|
|
|
{
|
|
|
|
plane.mode_qstabilize.update();
|
|
|
|
}
|
|
|
|
|
2021-08-14 15:57:45 -03:00
|
|
|
/*
|
|
|
|
handle QRTL mode
|
|
|
|
*/
|
|
|
|
void ModeQRTL::run()
|
|
|
|
{
|
2023-05-01 22:05:00 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
if (quadplane.tailsitter.in_vtol_transition(now)) {
|
|
|
|
// Tailsitters in FW pull up phase of VTOL transition run FW controllers
|
|
|
|
Mode::run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-30 22:38:55 -03:00
|
|
|
switch (submode) {
|
|
|
|
case SubMode::climb: {
|
|
|
|
// request zero velocity
|
|
|
|
Vector2f vel, accel;
|
|
|
|
pos_control->input_vel_accel_xy(vel, accel);
|
|
|
|
quadplane.run_xy_controller();
|
|
|
|
|
|
|
|
// nav roll and pitch are controller by position controller
|
|
|
|
plane.nav_roll_cd = pos_control->get_roll_cd();
|
|
|
|
plane.nav_pitch_cd = pos_control->get_pitch_cd();
|
2023-08-13 04:08:28 -03:00
|
|
|
|
|
|
|
plane.quadplane.assign_tilt_to_fwd_thr();
|
|
|
|
|
2022-05-30 22:38:55 -03:00
|
|
|
if (quadplane.transition->set_VTOL_roll_pitch_limit(plane.nav_roll_cd, plane.nav_pitch_cd)) {
|
|
|
|
pos_control->set_externally_limited_xy();
|
|
|
|
}
|
|
|
|
// weathervane with no pilot input
|
2022-07-03 19:39:05 -03:00
|
|
|
quadplane.disable_yaw_rate_time_constant();
|
2022-05-30 22:38:55 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(plane.nav_roll_cd,
|
|
|
|
plane.nav_pitch_cd,
|
|
|
|
quadplane.get_weathervane_yaw_rate_cds());
|
|
|
|
|
|
|
|
// climb at full WP nav speed
|
2022-12-17 14:27:48 -04:00
|
|
|
quadplane.set_climb_rate_cms(quadplane.wp_nav->get_default_speed_up());
|
2022-05-30 22:38:55 -03:00
|
|
|
quadplane.run_z_controller();
|
|
|
|
|
2023-01-13 10:39:19 -04:00
|
|
|
// Climb done when stopping point reaches target altitude
|
|
|
|
Vector3p stopping_point;
|
|
|
|
pos_control->get_stopping_point_z_cm(stopping_point.z);
|
|
|
|
Location stopping_loc = Location(stopping_point.tofloat(), Location::AltFrame::ABOVE_ORIGIN);
|
|
|
|
|
2022-05-30 22:38:55 -03:00
|
|
|
ftype alt_diff;
|
2023-01-13 10:39:19 -04:00
|
|
|
if (!stopping_loc.get_alt_distance(plane.next_WP_loc, alt_diff) || is_positive(alt_diff)) {
|
2023-10-11 04:41:54 -03:00
|
|
|
// climb finished or cant get alt diff, head home
|
2022-05-30 22:38:55 -03:00
|
|
|
submode = SubMode::RTL;
|
|
|
|
plane.prev_WP_loc = plane.current_loc;
|
2023-01-11 20:56:15 -04:00
|
|
|
|
|
|
|
int32_t RTL_alt_abs_cm = plane.home.alt + quadplane.qrtl_alt*100UL;
|
2023-04-07 05:25:34 -03:00
|
|
|
Location destination = plane.calc_best_rally_or_home_location(plane.current_loc, RTL_alt_abs_cm);
|
2023-01-11 20:56:15 -04:00
|
|
|
const float dist = plane.current_loc.get_distance(destination);
|
2023-01-25 06:35:25 -04:00
|
|
|
const float radius = get_VTOL_return_radius();
|
|
|
|
if (dist < radius) {
|
2023-01-13 10:39:19 -04:00
|
|
|
// if close to home return at current target altitude
|
|
|
|
int32_t target_alt_abs_cm;
|
|
|
|
if (plane.next_WP_loc.get_alt_cm(Location::AltFrame::ABSOLUTE, target_alt_abs_cm)) {
|
|
|
|
RTL_alt_abs_cm = MIN(RTL_alt_abs_cm, target_alt_abs_cm);
|
2023-01-11 20:56:15 -04:00
|
|
|
}
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"VTOL position1 d=%.1f r=%.1f", dist, radius);
|
|
|
|
poscontrol.set_state(QuadPlane::QPOS_POSITION1);
|
|
|
|
}
|
|
|
|
|
|
|
|
plane.do_RTL(RTL_alt_abs_cm);
|
2022-05-30 22:38:55 -03:00
|
|
|
quadplane.poscontrol_init_approach();
|
|
|
|
if (plane.current_loc.get_alt_distance(plane.next_WP_loc, alt_diff)) {
|
|
|
|
poscontrol.slow_descent = is_positive(alt_diff);
|
|
|
|
} else {
|
|
|
|
// default back to old method
|
|
|
|
poscontrol.slow_descent = (plane.current_loc.alt > plane.next_WP_loc.alt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SubMode::RTL: {
|
|
|
|
quadplane.vtol_position_controller();
|
|
|
|
if (poscontrol.get_state() > QuadPlane::QPOS_POSITION2) {
|
|
|
|
// change target altitude to home alt
|
|
|
|
plane.next_WP_loc.alt = plane.home.alt;
|
|
|
|
}
|
|
|
|
if (poscontrol.get_state() >= QuadPlane::QPOS_POSITION2) {
|
|
|
|
// start landing logic
|
|
|
|
quadplane.verify_vtol_land();
|
|
|
|
}
|
2021-12-24 20:27:59 -04:00
|
|
|
|
2022-05-30 22:38:55 -03:00
|
|
|
// when in approach allow stick mixing
|
|
|
|
if (quadplane.poscontrol.get_state() == QuadPlane::QPOS_AIRBRAKE ||
|
|
|
|
quadplane.poscontrol.get_state() == QuadPlane::QPOS_APPROACH) {
|
|
|
|
plane.stabilize_stick_mixing_fbw();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-12-24 20:27:59 -04:00
|
|
|
}
|
2023-05-01 22:05:00 -03:00
|
|
|
|
|
|
|
// Stabilize with fixed wing surfaces
|
|
|
|
plane.stabilize_roll();
|
|
|
|
plane.stabilize_pitch();
|
2021-08-14 15:57:45 -03:00
|
|
|
}
|
|
|
|
|
2021-06-01 03:03:52 -03:00
|
|
|
/*
|
|
|
|
update target altitude for QRTL profile
|
|
|
|
*/
|
2022-12-14 08:39:29 -04:00
|
|
|
void ModeQRTL::update_target_altitude()
|
2021-06-01 03:03:52 -03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
update height target in approach
|
|
|
|
*/
|
2022-05-30 22:38:55 -03:00
|
|
|
if ((submode != SubMode::RTL) || (plane.quadplane.poscontrol.get_state() != QuadPlane::QPOS_APPROACH)) {
|
2022-12-14 08:39:29 -04:00
|
|
|
Mode::update_target_altitude();
|
|
|
|
return;
|
2021-06-01 03:03:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initially approach at RTL_ALT_CM, then drop down to QRTL_ALT based on maximum sink rate from TECS,
|
|
|
|
giving time to lose speed before we transition
|
|
|
|
*/
|
2023-06-14 12:26:50 -03:00
|
|
|
const float radius = MAX(fabsf(float(plane.aparm.loiter_radius)), fabsf(float(plane.g.rtl_radius)));
|
2024-01-18 01:58:09 -04:00
|
|
|
const float rtl_alt_delta = MAX(0, plane.g.RTL_altitude - plane.quadplane.qrtl_alt);
|
2021-11-12 13:53:28 -04:00
|
|
|
const float sink_time = rtl_alt_delta / MAX(0.6*plane.TECS_controller.get_max_sinkrate(), 1);
|
2024-01-17 22:34:22 -04:00
|
|
|
const float sink_dist = plane.aparm.airspeed_cruise * sink_time;
|
2021-06-01 03:03:52 -03:00
|
|
|
const float dist = plane.auto_state.wp_distance;
|
|
|
|
const float rad_min = 2*radius;
|
|
|
|
const float rad_max = 20*radius;
|
|
|
|
float alt = linear_interpolate(0, rtl_alt_delta,
|
|
|
|
dist,
|
|
|
|
rad_min, MAX(rad_min, MIN(rad_max, rad_min+sink_dist)));
|
|
|
|
Location loc = plane.next_WP_loc;
|
|
|
|
loc.alt += alt*100;
|
|
|
|
plane.set_target_altitude_location(loc);
|
|
|
|
}
|
|
|
|
|
2021-12-24 20:27:59 -04:00
|
|
|
// only nudge during approach
|
|
|
|
bool ModeQRTL::allows_throttle_nudging() const
|
|
|
|
{
|
2022-05-30 22:38:55 -03:00
|
|
|
return (submode == SubMode::RTL) && (plane.quadplane.poscontrol.get_state() == QuadPlane::QPOS_APPROACH);
|
2021-12-24 20:27:59 -04:00
|
|
|
}
|
|
|
|
|
2023-01-25 06:35:25 -04:00
|
|
|
// Return the radius from destination at which pure VTOL flight should be used, no transition to FW
|
|
|
|
float ModeQRTL::get_VTOL_return_radius() const
|
|
|
|
{
|
2023-06-14 12:26:50 -03:00
|
|
|
return MAX(fabsf(float(plane.aparm.loiter_radius)), fabsf(float(plane.g.rtl_radius))) * 1.5;
|
2023-01-25 06:35:25 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 03:28:21 -03:00
|
|
|
#endif
|