2014-04-21 17:36:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
2020-06-02 09:10:44 -04:00
|
|
|
* Copyright (c) 2013-2020 PX4 Development Team. All rights reserved.
|
2014-04-21 17:36:59 +02:00
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
|
* distribution.
|
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/**
|
2019-01-24 08:20:34 +01:00
|
|
|
* @file rtl.cpp
|
|
|
|
|
*
|
2014-04-21 17:36:59 +02:00
|
|
|
* Helper class to access RTL
|
2019-01-24 08:20:34 +01:00
|
|
|
*
|
2014-04-21 17:36:59 +02:00
|
|
|
* @author Julian Oes <julian@oes.ch>
|
|
|
|
|
* @author Anton Babushkin <anton.babushkin@me.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2017-07-08 14:48:27 -04:00
|
|
|
#include "rtl.h"
|
|
|
|
|
#include "navigator.h"
|
2019-08-14 10:07:09 +02:00
|
|
|
#include <dataman/dataman.h>
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2017-11-24 13:15:42 -05:00
|
|
|
static constexpr float DELAY_SIGMA = 0.01f;
|
|
|
|
|
|
2020-06-02 09:08:00 -04:00
|
|
|
using namespace time_literals;
|
|
|
|
|
|
2018-03-02 13:58:43 +01:00
|
|
|
RTL::RTL(Navigator *navigator) :
|
|
|
|
|
MissionBlock(navigator),
|
|
|
|
|
ModuleParams(navigator)
|
2014-04-21 17:36:59 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::on_inactivation()
|
2019-09-03 15:29:23 +02:00
|
|
|
{
|
|
|
|
|
if (_navigator->get_precland()->is_activated()) {
|
|
|
|
|
_navigator->get_precland()->on_inactivation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::on_inactive()
|
2014-06-06 20:02:38 +02:00
|
|
|
{
|
2018-07-25 14:43:20 -06:00
|
|
|
// Reset RTL state.
|
2017-02-25 18:50:16 +01:00
|
|
|
_rtl_state = RTL_STATE_NONE;
|
2014-06-06 20:02:38 +02:00
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
find_RTL_destination();
|
2018-01-04 23:42:01 -05:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::find_RTL_destination()
|
2014-06-03 16:01:28 +02:00
|
|
|
{
|
2020-06-02 09:08:00 -04:00
|
|
|
// don't update RTL destination faster than 1 Hz
|
|
|
|
|
if (hrt_elapsed_time(&_destination_check_time) < 1_s) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_destination_check_time = hrt_absolute_time();
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// get home position:
|
|
|
|
|
home_position_s &home_landing_position = *_navigator->get_home_position();
|
2020-06-02 09:10:44 -04:00
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// get global position
|
2019-08-14 10:07:09 +02:00
|
|
|
const vehicle_global_position_s &global_position = *_navigator->get_global_position();
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// set destination to home per default, then check if other valid landing spot is closer
|
|
|
|
|
_destination.set(home_landing_position);
|
2020-06-02 09:10:44 -04:00
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// get distance to home position
|
|
|
|
|
double dlat = home_landing_position.lat - global_position.lat;
|
|
|
|
|
double dlon = home_landing_position.lon - global_position.lon;
|
2020-08-19 09:55:15 +02:00
|
|
|
|
|
|
|
|
double lon_scale = cos(math::radians(global_position.lat));
|
|
|
|
|
|
|
|
|
|
auto coord_dist_sq = [lon_scale](double lat_diff, double lon_diff) -> double {
|
|
|
|
|
double lon_diff_scaled = lon_scale * matrix::wrap(lon_diff, -180., 180.);
|
|
|
|
|
return lat_diff * lat_diff + lon_diff_scaled * lon_diff_scaled;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
double min_dist_squared = coord_dist_sq(dlat, dlon);
|
2019-10-10 17:35:26 +02:00
|
|
|
|
|
|
|
|
_destination.type = RTL_DESTINATION_HOME;
|
|
|
|
|
|
2019-11-22 08:18:25 +01:00
|
|
|
// consider the mission landing if not RTL_HOME type set
|
|
|
|
|
if (rtl_type() != RTL_HOME && _navigator->get_mission_start_land_available()) {
|
2019-10-10 17:35:26 +02:00
|
|
|
double mission_landing_lat = _navigator->get_mission_landing_lat();
|
|
|
|
|
double mission_landing_lon = _navigator->get_mission_landing_lon();
|
|
|
|
|
|
|
|
|
|
// compare home position to landing position to decide which is closer
|
|
|
|
|
dlat = mission_landing_lat - global_position.lat;
|
|
|
|
|
dlon = mission_landing_lon - global_position.lon;
|
2020-08-19 09:55:15 +02:00
|
|
|
double dist_squared = coord_dist_sq(dlat, dlon);
|
2019-10-10 17:35:26 +02:00
|
|
|
|
2019-11-22 08:18:25 +01:00
|
|
|
// set destination to mission landing if closest or in RTL_LAND or RTL_MISSION (so not in RTL_CLOSEST)
|
|
|
|
|
if (dist_squared < min_dist_squared || rtl_type() != RTL_CLOSEST) {
|
2019-10-10 17:35:26 +02:00
|
|
|
min_dist_squared = dist_squared;
|
|
|
|
|
_destination.lat = _navigator->get_mission_landing_lat();
|
|
|
|
|
_destination.lon = _navigator->get_mission_landing_lon();
|
|
|
|
|
_destination.alt = _navigator->get_mission_landing_alt();
|
|
|
|
|
_destination.type = RTL_DESTINATION_MISSION_LANDING;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 08:18:25 +01:00
|
|
|
// do not consider rally point if RTL type is set to RTL_MISSION, so exit function and use either home or mission landing
|
|
|
|
|
if (rtl_type() == RTL_MISSION) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// compare to safe landing positions
|
|
|
|
|
mission_safe_point_s closest_safe_point {} ;
|
2019-08-14 10:07:09 +02:00
|
|
|
mission_stats_entry_s stats;
|
|
|
|
|
int ret = dm_read(DM_KEY_SAFE_POINTS, 0, &stats, sizeof(mission_stats_entry_s));
|
|
|
|
|
int num_safe_points = 0;
|
|
|
|
|
|
|
|
|
|
if (ret == sizeof(mission_stats_entry_s)) {
|
|
|
|
|
num_safe_points = stats.num_items;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// check if a safe point is closer than home or landing
|
2019-08-14 10:07:09 +02:00
|
|
|
int closest_index = 0;
|
|
|
|
|
|
|
|
|
|
for (int current_seq = 1; current_seq <= num_safe_points; ++current_seq) {
|
|
|
|
|
mission_safe_point_s mission_safe_point;
|
|
|
|
|
|
|
|
|
|
if (dm_read(DM_KEY_SAFE_POINTS, current_seq, &mission_safe_point, sizeof(mission_safe_point_s)) !=
|
|
|
|
|
sizeof(mission_safe_point_s)) {
|
|
|
|
|
PX4_ERR("dm_read failed");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: take altitude into account for distance measurement
|
|
|
|
|
dlat = mission_safe_point.lat - global_position.lat;
|
|
|
|
|
dlon = mission_safe_point.lon - global_position.lon;
|
2020-08-19 09:55:15 +02:00
|
|
|
double dist_squared = coord_dist_sq(dlat, dlon);
|
2019-08-14 10:07:09 +02:00
|
|
|
|
|
|
|
|
if (dist_squared < min_dist_squared) {
|
|
|
|
|
closest_index = current_seq;
|
|
|
|
|
min_dist_squared = dist_squared;
|
|
|
|
|
closest_safe_point = mission_safe_point;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
if (closest_index > 0) {
|
|
|
|
|
_destination.type = RTL_DESTINATION_SAFE_POINT;
|
2019-08-14 10:07:09 +02:00
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
// There is a safe point closer than home/mission landing
|
2019-08-14 10:07:09 +02:00
|
|
|
// TODO: handle all possible mission_safe_point.frame cases
|
|
|
|
|
switch (closest_safe_point.frame) {
|
|
|
|
|
case 0: // MAV_FRAME_GLOBAL
|
|
|
|
|
_destination.lat = closest_safe_point.lat;
|
|
|
|
|
_destination.lon = closest_safe_point.lon;
|
|
|
|
|
_destination.alt = closest_safe_point.alt;
|
2019-10-10 17:35:26 +02:00
|
|
|
_destination.yaw = home_landing_position.yaw;
|
2019-08-14 10:07:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3: // MAV_FRAME_GLOBAL_RELATIVE_ALT
|
|
|
|
|
_destination.lat = closest_safe_point.lat;
|
|
|
|
|
_destination.lon = closest_safe_point.lon;
|
2019-10-10 17:35:26 +02:00
|
|
|
_destination.alt = closest_safe_point.alt + home_landing_position.alt; // alt of safe point is rel to home
|
|
|
|
|
_destination.yaw = home_landing_position.yaw;
|
2019-08-14 10:07:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2019-10-10 17:35:26 +02:00
|
|
|
mavlink_log_critical(_navigator->get_mavlink_log_pub(), "RTL: unsupported MAV_FRAME");
|
2019-08-14 10:07:09 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::on_activation()
|
2019-10-10 17:35:26 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// output the correct message, depending on where the RTL destination is
|
|
|
|
|
switch (_destination.type) {
|
|
|
|
|
case RTL_DESTINATION_HOME:
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: landing at home position.");
|
2019-10-10 17:35:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RTL_DESTINATION_MISSION_LANDING:
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: landing at mission landing.");
|
2019-10-10 17:35:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RTL_DESTINATION_SAFE_POINT:
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: landing at safe landing point.");
|
2019-10-10 17:35:26 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const vehicle_global_position_s &global_position = *_navigator->get_global_position();
|
|
|
|
|
|
2020-06-19 14:15:42 +03:00
|
|
|
if (_navigator->get_vstatus()->vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING) {
|
|
|
|
|
_rtl_alt = calculate_return_alt_from_cone_half_angle((float)_param_rtl_cone_half_angle_deg.get());
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
_rtl_alt = math::max(global_position.alt, _destination.alt + _param_rtl_return_alt.get());
|
|
|
|
|
}
|
2019-07-01 17:17:48 +02:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
if (_navigator->get_land_detected()->landed) {
|
2018-07-25 14:52:12 -06:00
|
|
|
// For safety reasons don't go into RTL if landed.
|
2017-02-25 18:50:16 +01:00
|
|
|
_rtl_state = RTL_STATE_LANDED;
|
2016-11-06 15:10:28 -05:00
|
|
|
|
2019-10-10 17:35:26 +02:00
|
|
|
} else if ((_destination.type == RTL_DESTINATION_MISSION_LANDING) && _navigator->on_mission_landing()) {
|
2018-07-25 14:43:20 -06:00
|
|
|
// RTL straight to RETURN state, but mission will takeover for landing.
|
2018-01-04 23:42:01 -05:00
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
} else if ((global_position.alt < _destination.alt + _param_rtl_return_alt.get()) || _rtl_alt_min) {
|
2014-06-28 00:54:27 +02:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// If lower than return altitude, climb up first.
|
|
|
|
|
// If rtl_alt_min is true then forcing altitude change even if above.
|
2017-02-25 18:50:16 +01:00
|
|
|
_rtl_state = RTL_STATE_CLIMB;
|
2016-11-06 15:10:28 -05:00
|
|
|
|
2017-07-08 14:48:27 -04:00
|
|
|
} else {
|
2018-07-25 14:43:20 -06:00
|
|
|
// Otherwise go straight to return
|
2017-07-08 14:48:27 -04:00
|
|
|
_rtl_state = RTL_STATE_RETURN;
|
2014-06-28 00:54:27 +02:00
|
|
|
}
|
2014-06-26 12:18:19 +02:00
|
|
|
|
2014-06-29 15:35:34 +02:00
|
|
|
set_rtl_item();
|
2014-06-28 00:54:27 +02:00
|
|
|
}
|
2014-06-03 16:01:28 +02:00
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::on_active()
|
2014-06-28 00:54:27 +02:00
|
|
|
{
|
|
|
|
|
if (_rtl_state != RTL_STATE_LANDED && is_mission_item_reached()) {
|
2014-06-06 20:02:38 +02:00
|
|
|
advance_rtl();
|
2014-06-29 15:35:34 +02:00
|
|
|
set_rtl_item();
|
2019-09-03 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_rtl_state == RTL_STATE_LAND && _param_rtl_pld_md.get() > 0) {
|
|
|
|
|
_navigator->get_precland()->on_active();
|
|
|
|
|
|
|
|
|
|
} else if (_navigator->get_precland()->is_activated()) {
|
|
|
|
|
_navigator->get_precland()->on_inactivation();
|
2014-06-06 20:02:38 +02:00
|
|
|
}
|
2014-06-03 16:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::set_rtl_item()
|
2014-04-21 17:36:59 +02:00
|
|
|
{
|
2018-07-25 14:43:20 -06:00
|
|
|
// RTL_TYPE: mission landing.
|
2019-08-14 10:07:09 +02:00
|
|
|
// Landing using planned mission landing, fly to DO_LAND_START instead of returning _destination.
|
2019-10-10 17:35:26 +02:00
|
|
|
// After reaching DO_LAND_START, do nothing, let navigator takeover with mission landing.
|
|
|
|
|
if (_destination.type == RTL_DESTINATION_MISSION_LANDING) {
|
2018-01-04 23:42:01 -05:00
|
|
|
if (_rtl_state > RTL_STATE_CLIMB) {
|
|
|
|
|
if (_navigator->start_mission_landing()) {
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: using mission landing");
|
2018-01-04 23:42:01 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} else {
|
2018-07-25 14:43:20 -06:00
|
|
|
// Otherwise use regular RTL.
|
2018-01-04 23:42:01 -05:00
|
|
|
mavlink_log_critical(_navigator->get_mavlink_log_pub(), "RTL: unable to use mission landing");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 23:39:17 +02:00
|
|
|
_navigator->set_can_loiter_at_sp(false);
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2017-11-12 14:52:05 -05:00
|
|
|
const vehicle_global_position_s &gpos = *_navigator->get_global_position();
|
|
|
|
|
|
|
|
|
|
position_setpoint_triplet_s *pos_sp_triplet = _navigator->get_position_setpoint_triplet();
|
|
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
// Check if we are pretty close to the destination already.
|
|
|
|
|
const float destination_dist = get_distance_to_next_waypoint(_destination.lat, _destination.lon, gpos.lat, gpos.lon);
|
2017-02-07 11:11:05 +01:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Compute the loiter altitude.
|
2019-08-14 10:07:09 +02:00
|
|
|
const float loiter_altitude = math::min(_destination.alt + _param_rtl_descend_alt.get(), gpos.alt);
|
2018-05-27 18:07:06 +02:00
|
|
|
|
|
|
|
|
switch (_rtl_state) {
|
|
|
|
|
case RTL_STATE_CLIMB: {
|
2017-02-07 11:11:05 +01:00
|
|
|
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_WAYPOINT;
|
|
|
|
|
_mission_item.lat = gpos.lat;
|
|
|
|
|
_mission_item.lon = gpos.lon;
|
2019-07-01 17:17:48 +02:00
|
|
|
_mission_item.altitude = _rtl_alt;
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.altitude_is_relative = false;
|
2020-08-07 14:06:38 -04:00
|
|
|
_mission_item.yaw = _navigator->get_local_position()->heading;
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.acceptance_radius = _navigator->get_acceptance_radius();
|
|
|
|
|
_mission_item.time_inside = 0.0f;
|
|
|
|
|
_mission_item.autocontinue = true;
|
|
|
|
|
_mission_item.origin = ORIGIN_ONBOARD;
|
|
|
|
|
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: climb to %d m (%d m above destination)",
|
|
|
|
|
(int)ceilf(_rtl_alt), (int)ceilf(_rtl_alt - _destination.alt));
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2016-04-23 15:47:00 +02:00
|
|
|
|
2016-11-06 15:10:28 -05:00
|
|
|
case RTL_STATE_RETURN: {
|
2017-11-12 14:52:05 -05:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Don't change altitude.
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_WAYPOINT;
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.lat = _destination.lat;
|
|
|
|
|
_mission_item.lon = _destination.lon;
|
2019-07-01 17:17:48 +02:00
|
|
|
_mission_item.altitude = _rtl_alt;
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.altitude_is_relative = false;
|
2016-04-23 15:47:00 +02:00
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
// Use destination yaw if close to _destination.
|
|
|
|
|
// Check if we are pretty close to the destination already.
|
|
|
|
|
if (destination_dist < _param_rtl_min_dist.get()) {
|
|
|
|
|
_mission_item.yaw = _destination.yaw;
|
2016-04-23 15:47:00 +02:00
|
|
|
|
|
|
|
|
} else {
|
2019-08-14 10:07:09 +02:00
|
|
|
// Use current heading to _destination.
|
|
|
|
|
_mission_item.yaw = get_bearing_to_next_waypoint(gpos.lat, gpos.lon, _destination.lat, _destination.lon);
|
2016-04-23 15:47:00 +02:00
|
|
|
}
|
2016-11-06 15:10:28 -05:00
|
|
|
|
|
|
|
|
_mission_item.acceptance_radius = _navigator->get_acceptance_radius();
|
|
|
|
|
_mission_item.time_inside = 0.0f;
|
|
|
|
|
_mission_item.autocontinue = true;
|
|
|
|
|
_mission_item.origin = ORIGIN_ONBOARD;
|
|
|
|
|
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: return at %d m (%d m above destination)",
|
|
|
|
|
(int)ceilf(_mission_item.altitude), (int)ceilf(_mission_item.altitude - _destination.alt));
|
2017-11-12 14:52:05 -05:00
|
|
|
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
2016-04-23 15:47:00 +02:00
|
|
|
}
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2016-03-23 12:34:00 +01:00
|
|
|
case RTL_STATE_TRANSITION_TO_MC: {
|
2018-05-27 18:07:06 +02:00
|
|
|
set_vtol_transition_item(&_mission_item, vtol_vehicle_status_s::VEHICLE_VTOL_STATE_MC);
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2016-03-23 12:34:00 +01:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case RTL_STATE_DESCEND: {
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_WAYPOINT;
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.lat = _destination.lat;
|
|
|
|
|
_mission_item.lon = _destination.lon;
|
2018-05-27 18:07:06 +02:00
|
|
|
_mission_item.altitude = loiter_altitude;
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.altitude_is_relative = false;
|
2016-04-23 15:47:00 +02:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Except for vtol which might be still off here and should point towards this location.
|
2017-11-12 14:52:05 -05:00
|
|
|
const float d_current = get_distance_to_next_waypoint(gpos.lat, gpos.lon, _mission_item.lat, _mission_item.lon);
|
2016-04-23 15:47:00 +02:00
|
|
|
|
2017-11-12 14:52:05 -05:00
|
|
|
if (_navigator->get_vstatus()->is_vtol && (d_current > _navigator->get_acceptance_radius())) {
|
|
|
|
|
_mission_item.yaw = get_bearing_to_next_waypoint(gpos.lat, gpos.lon, _mission_item.lat, _mission_item.lon);
|
2016-03-23 12:34:00 +01:00
|
|
|
|
2017-11-12 14:52:05 -05:00
|
|
|
} else {
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.yaw = _destination.yaw;
|
2016-11-06 15:10:28 -05:00
|
|
|
}
|
2016-03-23 12:34:00 +01:00
|
|
|
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.acceptance_radius = _navigator->get_acceptance_radius();
|
|
|
|
|
_mission_item.time_inside = 0.0f;
|
2017-04-11 07:37:10 +02:00
|
|
|
_mission_item.autocontinue = true;
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.origin = ORIGIN_ONBOARD;
|
2014-06-06 20:02:38 +02:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Disable previous setpoint to prevent drift.
|
2016-11-06 15:10:28 -05:00
|
|
|
pos_sp_triplet->previous.valid = false;
|
2016-04-08 23:50:34 +02:00
|
|
|
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: descend to %d m (%d m above destination)",
|
|
|
|
|
(int)ceilf(_mission_item.altitude), (int)ceilf(_mission_item.altitude - _destination.alt));
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case RTL_STATE_LOITER: {
|
2019-03-25 14:24:25 +01:00
|
|
|
const bool autoland = (_param_rtl_land_delay.get() > FLT_EPSILON);
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Don't change altitude.
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.lat = _destination.lat;
|
|
|
|
|
_mission_item.lon = _destination.lon;
|
2018-05-27 18:07:06 +02:00
|
|
|
_mission_item.altitude = loiter_altitude;
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.altitude_is_relative = false;
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.yaw = _destination.yaw;
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.loiter_radius = _navigator->get_loiter_radius();
|
|
|
|
|
_mission_item.acceptance_radius = _navigator->get_acceptance_radius();
|
2019-03-25 14:24:25 +01:00
|
|
|
_mission_item.time_inside = math::max(_param_rtl_land_delay.get(), 0.0f);
|
2016-11-06 15:10:28 -05:00
|
|
|
_mission_item.autocontinue = autoland;
|
|
|
|
|
_mission_item.origin = ORIGIN_ONBOARD;
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2016-11-06 15:10:28 -05:00
|
|
|
_navigator->set_can_loiter_at_sp(true);
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2017-04-20 11:24:55 -04:00
|
|
|
if (autoland && (get_time_inside(_mission_item) > FLT_EPSILON)) {
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_LOITER_TIME_LIMIT;
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: loiter %.1fs",
|
|
|
|
|
(double)get_time_inside(_mission_item));
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2016-11-06 15:10:28 -05:00
|
|
|
} else {
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_LOITER_UNLIMITED;
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: completed, loitering");
|
2016-11-06 15:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2014-06-27 00:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case RTL_STATE_LAND: {
|
2019-08-14 10:07:09 +02:00
|
|
|
// Land at destination.
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.nav_cmd = NAV_CMD_LAND;
|
2019-08-14 10:07:09 +02:00
|
|
|
_mission_item.lat = _destination.lat;
|
|
|
|
|
_mission_item.lon = _destination.lon;
|
|
|
|
|
_mission_item.yaw = _destination.yaw;
|
|
|
|
|
_mission_item.altitude = _destination.alt;
|
2017-11-12 14:52:05 -05:00
|
|
|
_mission_item.altitude_is_relative = false;
|
|
|
|
|
_mission_item.acceptance_radius = _navigator->get_acceptance_radius();
|
|
|
|
|
_mission_item.time_inside = 0.0f;
|
|
|
|
|
_mission_item.autocontinue = true;
|
|
|
|
|
_mission_item.origin = ORIGIN_ONBOARD;
|
2019-09-03 15:29:23 +02:00
|
|
|
_mission_item.land_precision = _param_rtl_pld_md.get();
|
|
|
|
|
|
|
|
|
|
if (_mission_item.land_precision == 1) {
|
|
|
|
|
_navigator->get_precland()->set_mode(PrecLandMode::Opportunistic);
|
|
|
|
|
_navigator->get_precland()->on_activation();
|
|
|
|
|
|
|
|
|
|
} else if (_mission_item.land_precision == 2) {
|
|
|
|
|
_navigator->get_precland()->set_mode(PrecLandMode::Required);
|
|
|
|
|
_navigator->get_precland()->on_activation();
|
|
|
|
|
}
|
2014-06-06 20:02:38 +02:00
|
|
|
|
2020-05-11 11:27:33 -04:00
|
|
|
mavlink_log_info(_navigator->get_mavlink_log_pub(), "RTL: land at destination");
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2014-04-21 17:36:59 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case RTL_STATE_LANDED: {
|
2016-11-06 15:10:28 -05:00
|
|
|
set_idle_item(&_mission_item);
|
2017-10-30 22:41:06 +01:00
|
|
|
set_return_alt_min(false);
|
2016-11-06 15:10:28 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2014-04-21 17:36:59 +02:00
|
|
|
default:
|
2014-06-06 20:02:38 +02:00
|
|
|
break;
|
2014-04-21 17:36:59 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-29 15:35:34 +02:00
|
|
|
reset_mission_item_reached();
|
|
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Execute command if set. This is required for commands like VTOL transition.
|
2017-09-26 12:25:02 -04:00
|
|
|
if (!item_contains_position(_mission_item)) {
|
|
|
|
|
issue_command(_mission_item);
|
2017-06-14 21:43:02 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Convert mission item to current position setpoint and make it valid.
|
2017-06-26 10:22:29 +02:00
|
|
|
mission_apply_limitation(_mission_item);
|
2014-06-29 15:35:34 +02:00
|
|
|
|
2017-11-12 14:52:05 -05:00
|
|
|
if (mission_item_to_position_setpoint(_mission_item, &pos_sp_triplet->current)) {
|
|
|
|
|
_navigator->set_position_setpoint_triplet_updated();
|
|
|
|
|
}
|
2014-04-21 17:36:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 09:10:44 -04:00
|
|
|
void RTL::advance_rtl()
|
2014-04-21 17:36:59 +02:00
|
|
|
{
|
|
|
|
|
switch (_rtl_state) {
|
|
|
|
|
case RTL_STATE_CLIMB:
|
|
|
|
|
_rtl_state = RTL_STATE_RETURN;
|
|
|
|
|
break;
|
2014-06-03 16:01:28 +02:00
|
|
|
|
2014-04-21 17:36:59 +02:00
|
|
|
case RTL_STATE_RETURN:
|
2019-04-25 13:44:36 +02:00
|
|
|
|
|
|
|
|
// Descend to desired altitude if delay is set, directly land otherwise
|
|
|
|
|
if (_param_rtl_land_delay.get() < -DELAY_SIGMA || _param_rtl_land_delay.get() > DELAY_SIGMA) {
|
|
|
|
|
_rtl_state = RTL_STATE_DESCEND;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
_rtl_state = RTL_STATE_LAND;
|
|
|
|
|
}
|
2016-03-23 12:34:00 +01:00
|
|
|
|
2019-06-11 12:54:22 +02:00
|
|
|
if (_navigator->get_vstatus()->is_vtol
|
|
|
|
|
&& _navigator->get_vstatus()->vehicle_type == vehicle_status_s::VEHICLE_TYPE_FIXED_WING) {
|
2016-03-23 12:34:00 +01:00
|
|
|
_rtl_state = RTL_STATE_TRANSITION_TO_MC;
|
|
|
|
|
}
|
2016-11-06 15:10:28 -05:00
|
|
|
|
2016-03-23 12:34:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RTL_STATE_TRANSITION_TO_MC:
|
2016-04-08 23:50:34 +02:00
|
|
|
_rtl_state = RTL_STATE_RETURN;
|
2014-04-21 17:36:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RTL_STATE_DESCEND:
|
2016-11-06 15:10:28 -05:00
|
|
|
|
2018-07-25 14:43:20 -06:00
|
|
|
// Only go to land if autoland is enabled.
|
2019-03-25 14:24:25 +01:00
|
|
|
if (_param_rtl_land_delay.get() < -DELAY_SIGMA || _param_rtl_land_delay.get() > DELAY_SIGMA) {
|
2014-06-27 00:27:08 +02:00
|
|
|
_rtl_state = RTL_STATE_LOITER;
|
|
|
|
|
|
2014-04-21 17:36:59 +02:00
|
|
|
} else {
|
|
|
|
|
_rtl_state = RTL_STATE_LAND;
|
|
|
|
|
}
|
2016-11-06 15:10:28 -05:00
|
|
|
|
2014-04-21 17:36:59 +02:00
|
|
|
break;
|
|
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case RTL_STATE_LOITER:
|
|
|
|
|
_rtl_state = RTL_STATE_LAND;
|
2014-04-21 17:36:59 +02:00
|
|
|
break;
|
|
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case RTL_STATE_LAND:
|
|
|
|
|
_rtl_state = RTL_STATE_LANDED;
|
2014-04-21 17:36:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-04-26 12:24:52 +02:00
|
|
|
}
|
2019-07-01 17:17:48 +02:00
|
|
|
|
|
|
|
|
float RTL::calculate_return_alt_from_cone_half_angle(float cone_half_angle_deg)
|
|
|
|
|
{
|
|
|
|
|
const vehicle_global_position_s &gpos = *_navigator->get_global_position();
|
|
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
// horizontal distance to destination
|
|
|
|
|
const float destination_dist = get_distance_to_next_waypoint(_destination.lat, _destination.lon, gpos.lat, gpos.lon);
|
2019-07-03 13:53:17 +02:00
|
|
|
|
2019-07-01 17:17:48 +02:00
|
|
|
float rtl_altitude;
|
|
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
if (destination_dist <= _param_rtl_min_dist.get()) {
|
|
|
|
|
rtl_altitude = _destination.alt + _param_rtl_descend_alt.get();
|
2019-07-03 13:53:17 +02:00
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
} else if (gpos.alt > _destination.alt + _param_rtl_return_alt.get() || cone_half_angle_deg >= 90.0f) {
|
2019-07-01 17:17:48 +02:00
|
|
|
rtl_altitude = gpos.alt;
|
|
|
|
|
|
|
|
|
|
} else if (cone_half_angle_deg <= 0) {
|
2019-08-14 10:07:09 +02:00
|
|
|
rtl_altitude = _destination.alt + _param_rtl_return_alt.get();
|
2019-07-01 17:17:48 +02:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// constrain cone half angle to meaningful values. All other cases are already handled above.
|
|
|
|
|
const float cone_half_angle_rad = math::radians(math::constrain(cone_half_angle_deg, 1.0f, 89.0f));
|
|
|
|
|
|
2019-08-14 10:07:09 +02:00
|
|
|
// minimum height above destination required
|
|
|
|
|
float height_above_destination_min = destination_dist / tanf(cone_half_angle_rad);
|
2019-07-01 17:17:48 +02:00
|
|
|
|
|
|
|
|
// minimum altitude we need in order to be within the user defined cone
|
2019-08-14 10:07:09 +02:00
|
|
|
const float altitude_min = math::constrain(height_above_destination_min + _destination.alt, _destination.alt,
|
|
|
|
|
_destination.alt + _param_rtl_return_alt.get());
|
2019-07-01 17:17:48 +02:00
|
|
|
|
|
|
|
|
if (gpos.alt < altitude_min) {
|
|
|
|
|
rtl_altitude = altitude_min;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
rtl_altitude = gpos.alt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// always demand altitude which is higher or equal the RTL descend altitude
|
2019-08-14 10:07:09 +02:00
|
|
|
rtl_altitude = math::max(rtl_altitude, _destination.alt + _param_rtl_descend_alt.get());
|
2019-07-01 17:17:48 +02:00
|
|
|
|
|
|
|
|
return rtl_altitude;
|
|
|
|
|
}
|