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

Use BT.CPP Tree::sleep #4761

Merged
merged 8 commits into from
Nov 27, 2024
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
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ _commands:
- restore_cache:
name: Restore Cache << parameters.key >>
keys:
- "<< parameters.key >>-v28\
- "<< parameters.key >>-v29\
-{{ arch }}\
-{{ .Branch }}\
-{{ .Environment.CIRCLE_PR_NUMBER }}\
-{{ checksum \"<< parameters.workspace >>/lockfile.txt\" }}"
- "<< parameters.key >>-v28\
- "<< parameters.key >>-v29\
-{{ arch }}\
-main\
-<no value>\
Expand All @@ -58,7 +58,7 @@ _commands:
steps:
- save_cache:
name: Save Cache << parameters.key >>
key: "<< parameters.key >>-v28\
key: "<< parameters.key >>-v29\
-{{ arch }}\
-{{ .Branch }}\
-{{ .Environment.CIRCLE_PR_NUMBER }}\
Expand Down
82 changes: 82 additions & 0 deletions nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2024 Angsa Robotics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NAV2_BEHAVIOR_TREE__UTILS__LOOP_RATE_HPP_
#define NAV2_BEHAVIOR_TREE__UTILS__LOOP_RATE_HPP_

#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/behavior_tree.h"

namespace nav2_behavior_tree
{

class LoopRate
{
public:
LoopRate(const rclcpp::Duration & period, BT::Tree * tree)
: clock_(std::make_shared<rclcpp::Clock>(RCL_STEADY_TIME)), period_(period),
last_interval_(clock_->now()), tree_(tree)
{}

// Similar to rclcpp::WallRate::sleep() but using tree_->sleep()
bool sleep()
{
// Time coming into sleep
auto now = clock_->now();
// Time of next interval
auto next_interval = last_interval_ + period_;
// Detect backwards time flow
if (now < last_interval_) {
// Best thing to do is to set the next_interval to now + period
next_interval = now + period_;
}
// Update the interval
last_interval_ += period_;
// If the time_to_sleep is negative or zero, don't sleep
if (next_interval <= now) {
// If an entire cycle was missed then reset next interval.
// This might happen if the loop took more than a cycle.
// Or if time jumps forward.
if (now > next_interval + period_) {
last_interval_ = now + period_;

Check warning on line 55 in nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp

View check run for this annotation

Codecov / codecov/patch

nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp#L54-L55

Added lines #L54 - L55 were not covered by tests
}
// Either way do not sleep and return false
return false;

Check warning on line 58 in nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp

View check run for this annotation

Codecov / codecov/patch

nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp#L58

Added line #L58 was not covered by tests
}
// Calculate the time to sleep
auto time_to_sleep = next_interval - now;
std::chrono::nanoseconds time_to_sleep_ns(time_to_sleep.nanoseconds());
// Sleep (can get interrupted by emitWakeUpSignal())
tree_->sleep(time_to_sleep_ns);
return true;
}

std::chrono::nanoseconds period() const
{
return std::chrono::nanoseconds(period_.nanoseconds());

Check warning on line 70 in nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp

View check run for this annotation

Codecov / codecov/patch

nav2_behavior_tree/include/nav2_behavior_tree/utils/loop_rate.hpp#L70

Added line #L70 was not covered by tests
}

private:
rclcpp::Clock::SharedPtr clock_;
rclcpp::Duration period_;
rclcpp::Time last_interval_;
BT::Tree * tree_;
};

} // namespace nav2_behavior_tree

#endif // NAV2_BEHAVIOR_TREE__UTILS__LOOP_RATE_HPP_
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 @@ -21,6 +21,7 @@

#include "rclcpp/rclcpp.hpp"
#include "behaviortree_cpp/utils/shared_library.h"
#include "nav2_behavior_tree/utils/loop_rate.hpp"

namespace nav2_behavior_tree
{
Expand Down Expand Up @@ -49,7 +50,7 @@ BehaviorTreeEngine::run(
std::function<bool()> cancelRequested,
std::chrono::milliseconds loopTimeout)
{
rclcpp::WallRate loopRate(loopTimeout);
nav2_behavior_tree::LoopRate loopRate(loopTimeout, tree);
BT::NodeStatus result = BT::NodeStatus::RUNNING;

// Loop until something happens with ROS or the node completes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "behaviortree_cpp/bt_factory.h"
#include "nav2_behavior_tree/bt_action_node.hpp"
#include "nav2_behavior_tree/utils/loop_rate.hpp"

#include "test_msgs/action/fibonacci.hpp"

Expand Down Expand Up @@ -260,7 +261,7 @@ TEST_F(BTActionNodeTestFixture, test_server_timeout_success)
BT::NodeStatus result = BT::NodeStatus::RUNNING;

// BT loop execution rate
rclcpp::WallRate loopRate(10ms);
nav2_behavior_tree::LoopRate loopRate(10ms, tree_.get());

// main BT execution loop
while (rclcpp::ok() && result == BT::NodeStatus::RUNNING) {
Expand Down
Loading