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

Backport to humble, Cherry-pick from 15c9be0aa5d09dd3be40bb46a66a5175464dd1a4 #4317

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "behaviortree_cpp_v3/xml_parsing.h"
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"

#include "rclcpp/rclcpp.hpp"

namespace nav2_behavior_tree
{
Expand All @@ -46,7 +47,8 @@ class BehaviorTreeEngine
* @brief A constructor for nav2_behavior_tree::BehaviorTreeEngine
* @param plugin_libraries vector of BT plugin library names to load
*/
explicit BehaviorTreeEngine(const std::vector<std::string> & plugin_libraries);
explicit BehaviorTreeEngine(
const std::vector<std::string> & plugin_libraries);
virtual ~BehaviorTreeEngine() {}

/**
Expand Down
3 changes: 2 additions & 1 deletion nav2_behavior_tree/src/behavior_tree_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
namespace nav2_behavior_tree
{

BehaviorTreeEngine::BehaviorTreeEngine(const std::vector<std::string> & plugin_libraries)
BehaviorTreeEngine::BehaviorTreeEngine(
const std::vector<std::string> & plugin_libraries)
{
BT::SharedLibrary loader;
for (const auto & p : plugin_libraries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DriveOnHeading : public TimedBehavior<ActionT>
command_speed_ = command->speed;
command_time_allowance_ = command->time_allowance;

end_time_ = this->steady_clock_.now() + command_time_allowance_;
end_time_ = this->clock_->now() + command_time_allowance_;

if (!nav2_util::getCurrentPose(
initial_pose_, *this->tf_, this->global_frame_, this->robot_base_frame_,
Expand All @@ -93,7 +93,7 @@ class DriveOnHeading : public TimedBehavior<ActionT>
*/
Status onCycleUpdate() override
{
rclcpp::Duration time_remaining = end_time_ - this->steady_clock_.now();
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
this->stopRobot();
RCLCPP_WARN(
Expand Down
14 changes: 7 additions & 7 deletions nav2_behaviors/include/nav2_behaviors/timed_behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class TimedBehavior : public nav2_core::Behavior
{
node_ = parent;
auto node = node_.lock();

logger_ = node->get_logger();
clock_ = node->get_clock();

RCLCPP_INFO(logger_, "Configuring %s", name.c_str());

Expand Down Expand Up @@ -175,7 +175,7 @@ class TimedBehavior : public nav2_core::Behavior
rclcpp::Duration elasped_time_{0, 0};

// Clock
rclcpp::Clock steady_clock_{RCL_STEADY_TIME};
rclcpp::Clock::SharedPtr clock_;

// Logger
rclcpp::Logger logger_{rclcpp::get_logger("nav2_behaviors")};
Expand All @@ -201,15 +201,15 @@ class TimedBehavior : public nav2_core::Behavior
return;
}

auto start_time = steady_clock_.now();
auto start_time = clock_->now();

// Initialize the ActionT result
auto result = std::make_shared<typename ActionT::Result>();

rclcpp::WallRate loop_rate(cycle_frequency_);

while (rclcpp::ok()) {
elasped_time_ = steady_clock_.now() - start_time;
elasped_time_ = clock_->now() - start_time;
if (action_server_->is_cancel_requested()) {
RCLCPP_INFO(logger_, "Canceling %s", behavior_name_.c_str());
stopRobot();
Expand All @@ -226,7 +226,7 @@ class TimedBehavior : public nav2_core::Behavior
" however feature is currently not implemented. Aborting and stopping.",
behavior_name_.c_str());
stopRobot();
result->total_elapsed_time = steady_clock_.now() - start_time;
result->total_elapsed_time = clock_->now() - start_time;
action_server_->terminate_current(result);
onActionCompletion();
return;
Expand All @@ -237,14 +237,14 @@ class TimedBehavior : public nav2_core::Behavior
RCLCPP_INFO(
logger_,
"%s completed successfully", behavior_name_.c_str());
result->total_elapsed_time = steady_clock_.now() - start_time;
result->total_elapsed_time = clock_->now() - start_time;
action_server_->succeeded_current(result);
onActionCompletion();
return;

case Status::FAILED:
RCLCPP_WARN(logger_, "%s failed", behavior_name_.c_str());
result->total_elapsed_time = steady_clock_.now() - start_time;
result->total_elapsed_time = clock_->now() - start_time;
action_server_->terminate_current(result);
onActionCompletion();
return;
Expand Down
8 changes: 4 additions & 4 deletions nav2_behaviors/plugins/assisted_teleop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Status AssistedTeleop::onRun(const std::shared_ptr<const AssistedTeleopAction::G
{
preempt_teleop_ = false;
command_time_allowance_ = command->time_allowance;
end_time_ = steady_clock_.now() + command_time_allowance_;
end_time_ = this->clock_->now() + command_time_allowance_;
return Status::SUCCEEDED;
}

Expand All @@ -82,7 +82,7 @@ Status AssistedTeleop::onCycleUpdate()
feedback_->current_teleop_duration = elasped_time_;
action_server_->publish_feedback(feedback_);

rclcpp::Duration time_remaining = end_time_ - steady_clock_.now();
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
stopRobot();
RCLCPP_WARN_STREAM(
Expand Down Expand Up @@ -124,7 +124,7 @@ Status AssistedTeleop::onCycleUpdate()
if (time == simulation_time_step_) {
RCLCPP_DEBUG_STREAM_THROTTLE(
logger_,
steady_clock_,
*clock_,
1000,
behavior_name_.c_str() << " collided on first time step, setting velocity to zero");
scaled_twist.linear.x = 0.0f;
Expand All @@ -134,7 +134,7 @@ Status AssistedTeleop::onCycleUpdate()
} else {
RCLCPP_DEBUG_STREAM_THROTTLE(
logger_,
steady_clock_,
*clock_,
1000,
behavior_name_.c_str() << " collision approaching in " << time << " seconds");
double scale_factor = time / projection_time_;
Expand Down
2 changes: 1 addition & 1 deletion nav2_behaviors/plugins/back_up.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Status BackUp::onRun(const std::shared_ptr<const BackUpAction::Goal> command)
command_speed_ = -std::fabs(command->speed);
command_time_allowance_ = command->time_allowance;

end_time_ = steady_clock_.now() + command_time_allowance_;
end_time_ = this->clock_->now() + command_time_allowance_;

if (!nav2_util::getCurrentPose(
initial_pose_, *tf_, global_frame_, robot_base_frame_,
Expand Down
4 changes: 2 additions & 2 deletions nav2_behaviors/plugins/spin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ Status Spin::onRun(const std::shared_ptr<const SpinAction::Goal> command)
cmd_yaw_);

command_time_allowance_ = command->time_allowance;
end_time_ = steady_clock_.now() + command_time_allowance_;
end_time_ = this->clock_->now() + command_time_allowance_;

return Status::SUCCEEDED;
}

Status Spin::onCycleUpdate()
{
rclcpp::Duration time_remaining = end_time_ - steady_clock_.now();
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
stopRobot();
RCLCPP_WARN(
Expand Down
3 changes: 0 additions & 3 deletions nav2_planner/include/nav2_planner/planner_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,6 @@ class PlannerServer : public nav2_util::LifecycleNode
double max_planner_duration_;
std::string planner_ids_concat_;

// Clock
rclcpp::Clock steady_clock_{RCL_STEADY_TIME};

// TF buffer
std::shared_ptr<tf2_ros::Buffer> tf_;

Expand Down
8 changes: 4 additions & 4 deletions nav2_planner/src/planner_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ PlannerServer::computePlanThroughPoses()
{
std::lock_guard<std::mutex> lock(dynamic_params_lock_);

auto start_time = steady_clock_.now();
auto start_time = this->now();

// Initialize the ComputePathToPose goal and result
auto goal = action_server_poses_->get_current_goal();
Expand Down Expand Up @@ -438,7 +438,7 @@ PlannerServer::computePlanThroughPoses()
result->path = concat_path;
publishPlan(result->path);

auto cycle_duration = steady_clock_.now() - start_time;
auto cycle_duration = this->now() - start_time;
result->planning_time = cycle_duration;

if (max_planner_duration_ && cycle_duration.seconds() > max_planner_duration_) {
Expand All @@ -464,7 +464,7 @@ PlannerServer::computePlan()
{
std::lock_guard<std::mutex> lock(dynamic_params_lock_);

auto start_time = steady_clock_.now();
auto start_time = this->now();

// Initialize the ComputePathToPose goal and result
auto goal = action_server_pose_->get_current_goal();
Expand Down Expand Up @@ -500,7 +500,7 @@ PlannerServer::computePlan()
// Publish the plan for visualization purposes
publishPlan(result->path);

auto cycle_duration = steady_clock_.now() - start_time;
auto cycle_duration = this->now() - start_time;
result->planning_time = cycle_duration;

if (max_planner_duration_ && cycle_duration.seconds() > max_planner_duration_) {
Expand Down
2 changes: 0 additions & 2 deletions nav2_smoother/include/nav2_smoother/nav2_smoother.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ class SmootherServer : public nav2_util::LifecycleNode
std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_sub_;
std::shared_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
std::shared_ptr<nav2_costmap_2d::CostmapTopicCollisionChecker> collision_checker_;

rclcpp::Clock steady_clock_;
};

} // namespace nav2_smoother
Expand Down
4 changes: 2 additions & 2 deletions nav2_smoother/src/nav2_smoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool SmootherServer::findSmootherId(

void SmootherServer::smoothPlan()
{
auto start_time = steady_clock_.now();
auto start_time = this->now();

RCLCPP_INFO(get_logger(), "Received a path to smooth.");

Expand All @@ -274,7 +274,7 @@ void SmootherServer::smoothPlan()
result->path = goal->path;
result->was_completed = smoothers_[current_smoother_]->smooth(
result->path, goal->max_smoothing_duration);
result->smoothing_duration = steady_clock_.now() - start_time;
result->smoothing_duration = this->now() - start_time;

if (!result->was_completed) {
RCLCPP_INFO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ BackupBehaviorTester::BackupBehaviorTester()
: is_active_(false),
initial_pose_received_(false)
{
node_ = rclcpp::Node::make_shared("backup_behavior_test");
rclcpp::NodeOptions options;
options.parameter_overrides({{"use_sim_time", true}});
node_ = rclcpp::Node::make_shared("backup_behavior_test", options);

tf_buffer_ = std::make_shared<tf2_ros::Buffer>(node_->get_clock());
tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ def main(argv=sys.argv[1:]):
testExecutable = os.getenv('TEST_EXECUTABLE')

test1_action = ExecuteProcess(
cmd=[testExecutable],
name='test_backup_behavior_node',
output='screen')
cmd=[testExecutable], name='test_backup_behavior_node', output='screen',
)

lts = LaunchTestService()
lts.add_test_action(ld, test1_action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ DriveOnHeadingBehaviorTester::DriveOnHeadingBehaviorTester()
: is_active_(false),
initial_pose_received_(false)
{
node_ = rclcpp::Node::make_shared("DriveOnHeading_behavior_test");
rclcpp::NodeOptions options;
options.parameter_overrides({{"use_sim_time", true}});
node_ = rclcpp::Node::make_shared("DriveOnHeading_behavior_test", options);

tf_buffer_ = std::make_shared<tf2_ros::Buffer>(node_->get_clock());
tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ def main(argv=sys.argv[1:]):
testExecutable = os.getenv('TEST_EXECUTABLE')

test1_action = ExecuteProcess(
cmd=[testExecutable],
name='test_spin_behavior_node',
output='screen')
cmd=[testExecutable], name='test_spin_behavior_node', output='screen',
)

lts = LaunchTestService()
lts.add_test_action(ld, test1_action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ def main(argv=sys.argv[1:]):
testExecutable = os.getenv('TEST_EXECUTABLE')

test1_action = ExecuteProcess(
cmd=[testExecutable],
name='test_wait_behavior_node',
output='screen')
cmd=[testExecutable], name='test_wait_behavior_node', output='screen',
)

lts = LaunchTestService()
lts.add_test_action(ld, test1_action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ bool WaitBehaviorTester::behaviorTest(

RCLCPP_INFO(node_->get_logger(), "result received");


if ((node_->now() - start_time).seconds() < static_cast<double>(wait_time)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions nav2_velocity_smoother/src/velocity_smoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ VelocitySmoother::on_activate(const rclcpp_lifecycle::State &)
RCLCPP_INFO(get_logger(), "Activating");
smoothed_cmd_pub_->on_activate();
double timer_duration_ms = 1000.0 / smoothing_frequency_;
timer_ = create_wall_timer(
timer_ = this->create_wall_timer(
std::chrono::milliseconds(static_cast<int>(timer_duration_ms)),
std::bind(&VelocitySmoother::smootherTimer, this));

Expand Down Expand Up @@ -342,7 +342,7 @@ VelocitySmoother::dynamicParametersCallback(std::vector<rclcpp::Parameter> param
}

double timer_duration_ms = 1000.0 / smoothing_frequency_;
timer_ = create_wall_timer(
timer_ = this->create_wall_timer(
std::chrono::milliseconds(static_cast<int>(timer_duration_ms)),
std::bind(&VelocitySmoother::smootherTimer, this));
} else if (name == "velocity_timeout") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class WaitAtWaypoint : public nav2_core::WaypointTaskExecutor
int waypoint_pause_duration_;
bool is_enabled_;
rclcpp::Logger logger_{rclcpp::get_logger("nav2_waypoint_follower")};
rclcpp::Clock::SharedPtr clock_;
};

} // namespace nav2_waypoint_follower
Expand Down
3 changes: 2 additions & 1 deletion nav2_waypoint_follower/plugins/wait_at_waypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void WaitAtWaypoint::initialize(
throw std::runtime_error{"Failed to lock node in wait at waypoint plugin!"};
}
logger_ = node->get_logger();
clock_ = node->get_clock();
nav2_util::declare_parameter_if_not_declared(
node,
plugin_name + ".waypoint_pause_duration",
Expand Down Expand Up @@ -77,7 +78,7 @@ bool WaitAtWaypoint::processAtWaypoint(
logger_, "Arrived at %i'th waypoint, sleeping for %i milliseconds",
curr_waypoint_index,
waypoint_pause_duration_);
rclcpp::sleep_for(std::chrono::milliseconds(waypoint_pause_duration_));
clock_->sleep_for(std::chrono::milliseconds(waypoint_pause_duration_));
return true;
}
} // namespace nav2_waypoint_follower
Expand Down