Skip to content

Commit 96d5646

Browse files
authored
Cherry-pick from 15c9be0 (#4317)
Convert all wall timers and wall rates to ROS clock respecting rates and timers (#4000) * Convert all wall timers and wall rates to ROS clock respecting rates and timers * linty mclint face * WPF wait plugin respect time * move duration metrics to use local clocks * bumping version for cache to break it * complete timing refactor * remove old variable
1 parent 11075c0 commit 96d5646

File tree

20 files changed

+44
-44
lines changed

20 files changed

+44
-44
lines changed

nav2_behavior_tree/include/nav2_behavior_tree/behavior_tree_engine.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "behaviortree_cpp_v3/xml_parsing.h"
2626
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"
2727

28+
#include "rclcpp/rclcpp.hpp"
2829

2930
namespace nav2_behavior_tree
3031
{
@@ -46,7 +47,8 @@ class BehaviorTreeEngine
4647
* @brief A constructor for nav2_behavior_tree::BehaviorTreeEngine
4748
* @param plugin_libraries vector of BT plugin library names to load
4849
*/
49-
explicit BehaviorTreeEngine(const std::vector<std::string> & plugin_libraries);
50+
explicit BehaviorTreeEngine(
51+
const std::vector<std::string> & plugin_libraries);
5052
virtual ~BehaviorTreeEngine() {}
5153

5254
/**

nav2_behavior_tree/src/behavior_tree_engine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
namespace nav2_behavior_tree
2626
{
2727

28-
BehaviorTreeEngine::BehaviorTreeEngine(const std::vector<std::string> & plugin_libraries)
28+
BehaviorTreeEngine::BehaviorTreeEngine(
29+
const std::vector<std::string> & plugin_libraries)
2930
{
3031
BT::SharedLibrary loader;
3132
for (const auto & p : plugin_libraries) {

nav2_behaviors/include/nav2_behaviors/plugins/drive_on_heading.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class DriveOnHeading : public TimedBehavior<ActionT>
7474
command_speed_ = command->speed;
7575
command_time_allowance_ = command->time_allowance;
7676

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

7979
if (!nav2_util::getCurrentPose(
8080
initial_pose_, *this->tf_, this->global_frame_, this->robot_base_frame_,
@@ -93,7 +93,7 @@ class DriveOnHeading : public TimedBehavior<ActionT>
9393
*/
9494
Status onCycleUpdate() override
9595
{
96-
rclcpp::Duration time_remaining = end_time_ - this->steady_clock_.now();
96+
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
9797
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
9898
this->stopRobot();
9999
RCLCPP_WARN(

nav2_behaviors/include/nav2_behaviors/timed_behavior.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class TimedBehavior : public nav2_core::Behavior
108108
{
109109
node_ = parent;
110110
auto node = node_.lock();
111-
112111
logger_ = node->get_logger();
112+
clock_ = node->get_clock();
113113

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

@@ -175,7 +175,7 @@ class TimedBehavior : public nav2_core::Behavior
175175
rclcpp::Duration elasped_time_{0, 0};
176176

177177
// Clock
178-
rclcpp::Clock steady_clock_{RCL_STEADY_TIME};
178+
rclcpp::Clock::SharedPtr clock_;
179179

180180
// Logger
181181
rclcpp::Logger logger_{rclcpp::get_logger("nav2_behaviors")};
@@ -201,15 +201,15 @@ class TimedBehavior : public nav2_core::Behavior
201201
return;
202202
}
203203

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

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

209209
rclcpp::WallRate loop_rate(cycle_frequency_);
210210

211211
while (rclcpp::ok()) {
212-
elasped_time_ = steady_clock_.now() - start_time;
212+
elasped_time_ = clock_->now() - start_time;
213213
if (action_server_->is_cancel_requested()) {
214214
RCLCPP_INFO(logger_, "Canceling %s", behavior_name_.c_str());
215215
stopRobot();
@@ -226,7 +226,7 @@ class TimedBehavior : public nav2_core::Behavior
226226
" however feature is currently not implemented. Aborting and stopping.",
227227
behavior_name_.c_str());
228228
stopRobot();
229-
result->total_elapsed_time = steady_clock_.now() - start_time;
229+
result->total_elapsed_time = clock_->now() - start_time;
230230
action_server_->terminate_current(result);
231231
onActionCompletion();
232232
return;
@@ -237,14 +237,14 @@ class TimedBehavior : public nav2_core::Behavior
237237
RCLCPP_INFO(
238238
logger_,
239239
"%s completed successfully", behavior_name_.c_str());
240-
result->total_elapsed_time = steady_clock_.now() - start_time;
240+
result->total_elapsed_time = clock_->now() - start_time;
241241
action_server_->succeeded_current(result);
242242
onActionCompletion();
243243
return;
244244

245245
case Status::FAILED:
246246
RCLCPP_WARN(logger_, "%s failed", behavior_name_.c_str());
247-
result->total_elapsed_time = steady_clock_.now() - start_time;
247+
result->total_elapsed_time = clock_->now() - start_time;
248248
action_server_->terminate_current(result);
249249
onActionCompletion();
250250
return;

nav2_behaviors/plugins/assisted_teleop.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Status AssistedTeleop::onRun(const std::shared_ptr<const AssistedTeleopAction::G
6767
{
6868
preempt_teleop_ = false;
6969
command_time_allowance_ = command->time_allowance;
70-
end_time_ = steady_clock_.now() + command_time_allowance_;
70+
end_time_ = this->clock_->now() + command_time_allowance_;
7171
return Status::SUCCEEDED;
7272
}
7373

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

85-
rclcpp::Duration time_remaining = end_time_ - steady_clock_.now();
85+
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
8686
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
8787
stopRobot();
8888
RCLCPP_WARN_STREAM(
@@ -124,7 +124,7 @@ Status AssistedTeleop::onCycleUpdate()
124124
if (time == simulation_time_step_) {
125125
RCLCPP_DEBUG_STREAM_THROTTLE(
126126
logger_,
127-
steady_clock_,
127+
*clock_,
128128
1000,
129129
behavior_name_.c_str() << " collided on first time step, setting velocity to zero");
130130
scaled_twist.linear.x = 0.0f;
@@ -134,7 +134,7 @@ Status AssistedTeleop::onCycleUpdate()
134134
} else {
135135
RCLCPP_DEBUG_STREAM_THROTTLE(
136136
logger_,
137-
steady_clock_,
137+
*clock_,
138138
1000,
139139
behavior_name_.c_str() << " collision approaching in " << time << " seconds");
140140
double scale_factor = time / projection_time_;

nav2_behaviors/plugins/back_up.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Status BackUp::onRun(const std::shared_ptr<const BackUpAction::Goal> command)
3131
command_speed_ = -std::fabs(command->speed);
3232
command_time_allowance_ = command->time_allowance;
3333

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

3636
if (!nav2_util::getCurrentPose(
3737
initial_pose_, *tf_, global_frame_, robot_base_frame_,

nav2_behaviors/plugins/spin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ Status Spin::onRun(const std::shared_ptr<const SpinAction::Goal> command)
9191
cmd_yaw_);
9292

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

9696
return Status::SUCCEEDED;
9797
}
9898

9999
Status Spin::onCycleUpdate()
100100
{
101-
rclcpp::Duration time_remaining = end_time_ - steady_clock_.now();
101+
rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
102102
if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
103103
stopRobot();
104104
RCLCPP_WARN(

nav2_planner/include/nav2_planner/planner_server.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ class PlannerServer : public nav2_util::LifecycleNode
237237
double max_planner_duration_;
238238
std::string planner_ids_concat_;
239239

240-
// Clock
241-
rclcpp::Clock steady_clock_{RCL_STEADY_TIME};
242-
243240
// TF buffer
244241
std::shared_ptr<tf2_ros::Buffer> tf_;
245242

nav2_planner/src/planner_server.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ PlannerServer::computePlanThroughPoses()
372372
{
373373
std::lock_guard<std::mutex> lock(dynamic_params_lock_);
374374

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

377377
// Initialize the ComputePathToPose goal and result
378378
auto goal = action_server_poses_->get_current_goal();
@@ -438,7 +438,7 @@ PlannerServer::computePlanThroughPoses()
438438
result->path = concat_path;
439439
publishPlan(result->path);
440440

441-
auto cycle_duration = steady_clock_.now() - start_time;
441+
auto cycle_duration = this->now() - start_time;
442442
result->planning_time = cycle_duration;
443443

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

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

469469
// Initialize the ComputePathToPose goal and result
470470
auto goal = action_server_pose_->get_current_goal();
@@ -500,7 +500,7 @@ PlannerServer::computePlan()
500500
// Publish the plan for visualization purposes
501501
publishPlan(result->path);
502502

503-
auto cycle_duration = steady_clock_.now() - start_time;
503+
auto cycle_duration = this->now() - start_time;
504504
result->planning_time = cycle_duration;
505505

506506
if (max_planner_duration_ && cycle_duration.seconds() > max_planner_duration_) {

nav2_smoother/include/nav2_smoother/nav2_smoother.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ class SmootherServer : public nav2_util::LifecycleNode
158158
std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_sub_;
159159
std::shared_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
160160
std::shared_ptr<nav2_costmap_2d::CostmapTopicCollisionChecker> collision_checker_;
161-
162-
rclcpp::Clock steady_clock_;
163161
};
164162

165163
} // namespace nav2_smoother

0 commit comments

Comments
 (0)