Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout to the wait for transforms step of the costmap activation. #3866

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion nav2_controller/src/controller_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ ControllerServer::on_activate(const rclcpp_lifecycle::State & /*state*/)
{
RCLCPP_INFO(get_logger(), "Activating");

costmap_ros_->activate();
const auto costmap_ros_state = costmap_ros_->activate();
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
if (costmap_ros_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE) {
return nav2_util::CallbackReturn::FAILURE;
}
ControllerMap::iterator it;
for (it = controllers_.begin(); it != controllers_.end(); ++it) {
it->second->activate();
Expand Down
9 changes: 5 additions & 4 deletions nav2_costmap_2d/include/nav2_costmap_2d/costmap_2d_ros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class Costmap2DROS : public nav2_util::LifecycleNode
bool always_send_full_costmap_{false};
std::string footprint_;
float footprint_padding_{0};
std::string global_frame_; ///< The global frame for the costmap
std::string global_frame_; ///< The global frame for the costmap
int map_height_meters_{0};
double map_publish_frequency_{0};
double map_update_frequency_{0};
Expand All @@ -374,11 +374,12 @@ class Costmap2DROS : public nav2_util::LifecycleNode
std::vector<std::string> filter_names_;
std::vector<std::string> filter_types_;
double resolution_{0};
std::string robot_base_frame_; ///< The frame_id of the robot base
std::string robot_base_frame_; ///< The frame_id of the robot base
double robot_radius_;
bool rolling_window_{false}; ///< Whether to use a rolling window version of the costmap
bool rolling_window_{false}; ///< Whether to use a rolling window version of the costmap
bool track_unknown_space_{false};
double transform_tolerance_{0}; ///< The timeout before transform errors
double transform_tolerance_{0}; ///< The timeout before transform errors
double wait_for_transforms_timeout_{0}; ///< The timeout before activation of the node errors

// Derived parameters
bool use_radius_{false};
Expand Down
20 changes: 20 additions & 0 deletions nav2_costmap_2d/src/costmap_2d_ros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void Costmap2DROS::init()
declare_parameter("rolling_window", rclcpp::ParameterValue(false));
declare_parameter("track_unknown_space", rclcpp::ParameterValue(false));
declare_parameter("transform_tolerance", rclcpp::ParameterValue(0.3));
declare_parameter("wait_for_transforms_timeout", rclcpp::ParameterValue(10.0));
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
declare_parameter("trinary_costmap", rclcpp::ParameterValue(true));
declare_parameter("unknown_cost_value", rclcpp::ParameterValue(static_cast<unsigned char>(0xff)));
declare_parameter("update_frequency", rclcpp::ParameterValue(5.0));
Expand Down Expand Up @@ -279,6 +280,9 @@ Costmap2DROS::on_activate(const rclcpp_lifecycle::State & /*state*/)

RCLCPP_INFO(get_logger(), "Checking transform");
rclcpp::Rate r(2);
const auto wait_for_transforms_timeout = rclcpp::Duration::from_seconds(
wait_for_transforms_timeout_);
const auto start_wait_for_transforms = now();
while (rclcpp::ok() &&
!tf_buffer_->canTransform(
global_frame_, robot_base_frame_, tf2::TimePointZero, &tf_error))
Expand All @@ -288,6 +292,21 @@ Costmap2DROS::on_activate(const rclcpp_lifecycle::State & /*state*/)
" to become available, tf error: %s",
robot_base_frame_.c_str(), global_frame_.c_str(), tf_error.c_str());

// Check timeout
if (now() > (start_wait_for_transforms + wait_for_transforms_timeout)) {
RCLCPP_ERROR(
get_logger(), "Failed to activate %s because transform from %s to %s did not become available before timeout",
get_name(), robot_base_frame_.c_str(), global_frame_.c_str());
// Deactivate already activated publishers
footprint_pub_->on_deactivate();
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
costmap_publisher_->on_deactivate();
for (auto & layer_pub : layer_publishers_) {
layer_pub->on_deactivate();
}

return nav2_util::CallbackReturn::FAILURE;
}

// The error string will accumulate and errors will typically be the same, so the last
// will do for the warning above. Reset the string here to avoid accumulation
tf_error.clear();
Expand Down Expand Up @@ -386,6 +405,7 @@ Costmap2DROS::getParameters()
get_parameter("rolling_window", rolling_window_);
get_parameter("track_unknown_space", track_unknown_space_);
get_parameter("transform_tolerance", transform_tolerance_);
get_parameter("wait_for_transforms_timeout", wait_for_transforms_timeout_);
get_parameter("update_frequency", map_update_frequency_);
get_parameter("width", map_width_meters_);
get_parameter("plugins", plugin_names_);
Expand Down