-
Notifications
You must be signed in to change notification settings - Fork 1.7k
adding an abstract BTNode for action cancellation #2787
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
Merged
SteveMacenski
merged 8 commits into
ros-navigation:main
from
padhupradheep:feature/bt_cancel_action
Feb 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49e5450
adding an abstract BT for action cancellation
padhupradheep 4b5cebe
review update and adding test
padhupradheep f6e719d
adding controller cancel BT Node
padhupradheep 63feaf7
Fixing the copyrights
padhupradheep d9f6a5b
review updates
padhupradheep 25d2f2a
satisfying lint
padhupradheep 7e36a3e
adding the bt to the defaults
padhupradheep 8516706
update
padhupradheep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
review update and adding test
- Loading branch information
commit 4b5cebe88ff5d14263886d9aad2821956e0d02f7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
nav2_behavior_tree/test/plugins/action/test_bt_cancel_action_node.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| // Copyright (c) 2022 Neobotix GmbH | ||
| // | ||
| // 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. | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <memory> | ||
| #include <set> | ||
| #include <vector> | ||
| #include <string> | ||
| #include <chrono> | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "rclcpp_action/rclcpp_action.hpp" | ||
|
|
||
| #include "behaviortree_cpp_v3/bt_factory.h" | ||
| #include "nav2_behavior_tree/bt_cancel_action_node.hpp" | ||
|
|
||
| #include "test_msgs/action/fibonacci.hpp" | ||
|
|
||
| using namespace std::chrono_literals; // NOLINT | ||
| using namespace std::placeholders; // NOLINT | ||
|
|
||
| class FibonacciActionServer : public rclcpp::Node | ||
| { | ||
| public: | ||
| FibonacciActionServer() | ||
| : rclcpp::Node("fibonacci_node", rclcpp::NodeOptions()) | ||
| { | ||
| this->action_server_ = rclcpp_action::create_server<test_msgs::action::Fibonacci>( | ||
| this->get_node_base_interface(), | ||
| this->get_node_clock_interface(), | ||
| this->get_node_logging_interface(), | ||
| this->get_node_waitables_interface(), | ||
| "fibonacci", | ||
| std::bind(&FibonacciActionServer::handle_goal, this, _1, _2), | ||
| std::bind(&FibonacciActionServer::handle_cancel, this, _1), | ||
| std::bind(&FibonacciActionServer::handle_accepted, this, _1)); | ||
| } | ||
|
|
||
| protected: | ||
| rclcpp_action::GoalResponse handle_goal( | ||
| const rclcpp_action::GoalUUID &, | ||
| std::shared_ptr<const test_msgs::action::Fibonacci::Goal>) | ||
| { | ||
| return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE; | ||
| } | ||
|
|
||
| rclcpp_action::CancelResponse handle_cancel( | ||
| const std::shared_ptr<rclcpp_action::ServerGoalHandle<test_msgs::action::Fibonacci>>) | ||
| { | ||
| return rclcpp_action::CancelResponse::ACCEPT; | ||
| } | ||
|
|
||
| void handle_accepted( | ||
| const std::shared_ptr<rclcpp_action::ServerGoalHandle<test_msgs::action::Fibonacci>>) | ||
| { | ||
| } | ||
|
|
||
| protected: | ||
| rclcpp_action::Server<test_msgs::action::Fibonacci>::SharedPtr action_server_; | ||
| std::chrono::milliseconds sleep_duration_; | ||
| }; | ||
|
|
||
| class FibonacciAction : public nav2_behavior_tree::BtCancelActionNode<test_msgs::action::Fibonacci> | ||
| { | ||
| public: | ||
| FibonacciAction( | ||
| const std::string & xml_tag_name, | ||
| const BT::NodeConfiguration & conf) | ||
| : nav2_behavior_tree::BtCancelActionNode<test_msgs::action::Fibonacci>(xml_tag_name, "fibonacci", | ||
| conf) | ||
| {} | ||
| }; | ||
|
|
||
| class BTCancelActionNodeTestFixture : public ::testing::Test | ||
| { | ||
| public: | ||
| static void SetUpTestCase() | ||
| { | ||
| node_ = std::make_shared<rclcpp::Node>("bt_cancel_action_node_test_fixture"); | ||
| factory_ = std::make_shared<BT::BehaviorTreeFactory>(); | ||
|
|
||
| config_ = new BT::NodeConfiguration(); | ||
|
|
||
| // Create the blackboard that will be shared by all of the nodes in the tree | ||
| config_->blackboard = BT::Blackboard::create(); | ||
| // Put items on the blackboard | ||
| config_->blackboard->set<rclcpp::Node::SharedPtr>( | ||
| "node", | ||
| node_); | ||
| config_->blackboard->set<std::chrono::milliseconds>( | ||
| "server_timeout", | ||
| std::chrono::milliseconds(20)); | ||
| config_->blackboard->set<std::chrono::milliseconds>( | ||
| "bt_loop_duration", | ||
| std::chrono::milliseconds(10)); | ||
|
|
||
| client_ = rclcpp_action::create_client<test_msgs::action::Fibonacci>( | ||
| node_, "fibonacci"); | ||
|
|
||
| BT::NodeBuilder builder = | ||
| [](const std::string & name, const BT::NodeConfiguration & config) | ||
| { | ||
| return std::make_unique<FibonacciAction>(name, config); | ||
| }; | ||
|
|
||
| factory_->registerBuilder<FibonacciAction>("Fibonacci", builder); | ||
| } | ||
|
|
||
| static void TearDownTestCase() | ||
| { | ||
| delete config_; | ||
| config_ = nullptr; | ||
| node_.reset(); | ||
| action_server_.reset(); | ||
| factory_.reset(); | ||
| } | ||
|
|
||
| void TearDown() override | ||
| { | ||
| tree_.reset(); | ||
| } | ||
|
|
||
| static std::shared_ptr<FibonacciActionServer> action_server_; | ||
| static std::shared_ptr<rclcpp_action::Client<test_msgs::action::Fibonacci>> client_; | ||
|
|
||
| protected: | ||
| static rclcpp::Node::SharedPtr node_; | ||
| static BT::NodeConfiguration * config_; | ||
| static std::shared_ptr<BT::BehaviorTreeFactory> factory_; | ||
| static std::shared_ptr<BT::Tree> tree_; | ||
| }; | ||
|
|
||
| rclcpp::Node::SharedPtr BTCancelActionNodeTestFixture::node_ = nullptr; | ||
| std::shared_ptr<FibonacciActionServer> | ||
| BTCancelActionNodeTestFixture::action_server_ = nullptr; | ||
| std::shared_ptr<rclcpp_action::Client<test_msgs::action::Fibonacci>> | ||
| BTCancelActionNodeTestFixture::client_ = nullptr; | ||
|
|
||
| BT::NodeConfiguration * BTCancelActionNodeTestFixture::config_ = nullptr; | ||
| std::shared_ptr<BT::BehaviorTreeFactory> BTCancelActionNodeTestFixture::factory_ = nullptr; | ||
| std::shared_ptr<BT::Tree> BTCancelActionNodeTestFixture::tree_ = nullptr; | ||
|
|
||
| TEST_F(BTCancelActionNodeTestFixture, test_ports) | ||
| { | ||
| // Creating a Fibonacci and treating it as a dummy node | ||
| std::string xml_txt = | ||
| R"( | ||
| <root main_tree_to_execute = "MainTree" > | ||
| <BehaviorTree ID="MainTree"> | ||
| <Fibonacci/> | ||
| </BehaviorTree> | ||
| </root>)"; | ||
|
|
||
| tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
| auto send_goal_options = rclcpp_action::Client<test_msgs::action::Fibonacci>::SendGoalOptions(); | ||
| auto goal_msg = test_msgs::action::Fibonacci::Goal(); | ||
|
|
||
| client_->wait_for_action_server(); | ||
| // Sending a dummy goal | ||
| client_->async_send_goal(goal_msg, send_goal_options); | ||
|
|
||
| // Ticking the tree | ||
| tree_->rootNode()->executeTick(); | ||
|
|
||
| EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::SUCCESS); | ||
| } | ||
|
|
||
| int main(int argc, char ** argv) | ||
| { | ||
| ::testing::InitGoogleTest(&argc, argv); | ||
|
|
||
| // initialize ROS | ||
| rclcpp::init(argc, argv); | ||
|
|
||
| // initialize action server and spin on new thread | ||
| BTCancelActionNodeTestFixture::action_server_ = std::make_shared<FibonacciActionServer>(); | ||
| std::thread server_thread([]() { | ||
| rclcpp::spin(BTCancelActionNodeTestFixture::action_server_); | ||
| }); | ||
|
|
||
| int all_successful = RUN_ALL_TESTS(); | ||
|
|
||
| // shutdown ROS | ||
| rclcpp::shutdown(); | ||
| server_thread.join(); | ||
|
|
||
| return all_successful; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.