-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Collision monitor toggle bt plugin #5532
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
DavidG-Develop:feature/cm_toggle_bt
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
061a42d
add Bt pluging for toggle collision monitor service
DavidG-Develop 4d44030
Add test for btt plugin
DavidG-Develop d1da24e
Clean up test
DavidG-Develop 31018aa
Fix copyright in header
DavidG-Develop 9873972
uncrustify
DavidG-Develop da27b78
Merge branch 'main' into feature/cm_toggle_bt
DavidG-Develop ee79a5c
fix lint
DavidG-Develop d6b0127
fix circle ci
DavidG-Develop 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
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
63 changes: 63 additions & 0 deletions
63
...avior_tree/include/nav2_behavior_tree/plugins/action/toggle_collision_monitor_service.hpp
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,63 @@ | ||
// Copyright (c) 2025 David Grbac | ||
// | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "nav2_behavior_tree/bt_service_node.hpp" | ||
#include "nav2_msgs/srv/toggle.hpp" | ||
|
||
namespace nav2_behavior_tree | ||
{ | ||
|
||
/** | ||
* @brief A nav2_behavior_tree::BtServiceNode class that wraps nav2_msgs::srv::Toggle | ||
* @note This is an Asynchronous (long-running) node which may return a RUNNING state while executing. | ||
* It will re-initialize when halted. | ||
*/ | ||
class ToggleCollisionMonitorService : public BtServiceNode<nav2_msgs::srv::Toggle> | ||
{ | ||
public: | ||
/** | ||
* @brief A constructor for nav2_behavior_tree::ToggleCollisionMonitorService | ||
* @param service_node_name Service name this node creates a client for | ||
* @param conf BT node configuration | ||
*/ | ||
ToggleCollisionMonitorService( | ||
const std::string & service_node_name, | ||
const BT::NodeConfiguration & conf); | ||
|
||
/** | ||
* @brief The main override required by a BT service | ||
* @return BT::NodeStatus Status of tick execution | ||
*/ | ||
void on_tick() override; | ||
|
||
/** | ||
* @brief Creates list of BT ports | ||
* @return BT::PortsList Containing basic ports along with node-specific ports | ||
*/ | ||
static BT::PortsList providedPorts() | ||
{ | ||
return providedBasicPorts( | ||
{ | ||
BT::InputPort<bool>( | ||
"enable", true, | ||
"Whether to enable or disable collision monitoring"), | ||
}); | ||
} | ||
}; | ||
|
||
} // namespace nav2_behavior_tree |
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
42 changes: 42 additions & 0 deletions
42
nav2_behavior_tree/plugins/action/toggle_collision_monitor_service.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,42 @@ | ||
// Copyright (c) 2025 David Grbac | ||
// | ||
// 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 <string> | ||
#include <memory> | ||
|
||
#include "nav2_behavior_tree/plugins/action/toggle_collision_monitor_service.hpp" | ||
|
||
namespace nav2_behavior_tree | ||
{ | ||
|
||
ToggleCollisionMonitorService::ToggleCollisionMonitorService( | ||
const std::string & service_node_name, | ||
const BT::NodeConfiguration & conf) | ||
: BtServiceNode<nav2_msgs::srv::Toggle>(service_node_name, conf) | ||
{ | ||
} | ||
|
||
void ToggleCollisionMonitorService::on_tick() | ||
{ | ||
getInput("enable", request_->enable); | ||
} | ||
|
||
} // namespace nav2_behavior_tree | ||
|
||
#include "behaviortree_cpp/bt_factory.h" | ||
BT_REGISTER_NODES(factory) | ||
{ | ||
factory.registerNodeType<nav2_behavior_tree::ToggleCollisionMonitorService>( | ||
"ToggleCollisionMonitor"); | ||
} |
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
142 changes: 142 additions & 0 deletions
142
nav2_behavior_tree/test/plugins/action/test_toggle_collision_monitor_service.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,142 @@ | ||
// Copyright (c) 2025 David Grbac | ||
// | ||
// 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 <string> | ||
|
||
#include "behaviortree_cpp/bt_factory.h" | ||
|
||
#include "utils/test_service.hpp" | ||
#include "nav2_behavior_tree/plugins/action/toggle_collision_monitor_service.hpp" | ||
|
||
class ToggleCollisionMonitorService : public TestService<nav2_msgs::srv::Toggle> | ||
{ | ||
public: | ||
ToggleCollisionMonitorService() | ||
: TestService("toggle_collision_monitor") | ||
{} | ||
}; | ||
|
||
class ToggleCollisionMonitorTestFixture : public ::testing::Test | ||
{ | ||
public: | ||
static void SetUpTestCase() | ||
{ | ||
node_ = std::make_shared<nav2::LifecycleNode>("toggle_collision_monitor_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( | ||
"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)); | ||
config_->blackboard->set<std::chrono::milliseconds>( | ||
"wait_for_service_timeout", | ||
std::chrono::milliseconds(1000)); | ||
config_->blackboard->set("initial_pose_received", false); | ||
|
||
factory_->registerNodeType<nav2_behavior_tree::ToggleCollisionMonitorService>( | ||
"ToggleCollisionMonitor"); | ||
} | ||
|
||
static void TearDownTestCase() | ||
{ | ||
delete config_; | ||
config_ = nullptr; | ||
node_.reset(); | ||
server_.reset(); | ||
factory_.reset(); | ||
} | ||
|
||
void TearDown() override | ||
{ | ||
tree_.reset(); | ||
} | ||
|
||
static std::shared_ptr<ToggleCollisionMonitorService> server_; | ||
|
||
protected: | ||
static nav2::LifecycleNode::SharedPtr node_; | ||
static BT::NodeConfiguration * config_; | ||
static std::shared_ptr<BT::BehaviorTreeFactory> factory_; | ||
static std::shared_ptr<BT::Tree> tree_; | ||
}; | ||
|
||
nav2::LifecycleNode::SharedPtr ToggleCollisionMonitorTestFixture::node_ = nullptr; | ||
std::shared_ptr<ToggleCollisionMonitorService> | ||
ToggleCollisionMonitorTestFixture::server_ = nullptr; | ||
BT::NodeConfiguration * ToggleCollisionMonitorTestFixture::config_ = nullptr; | ||
std::shared_ptr<BT::BehaviorTreeFactory> | ||
ToggleCollisionMonitorTestFixture::factory_ = nullptr; | ||
std::shared_ptr<BT::Tree> ToggleCollisionMonitorTestFixture::tree_ = nullptr; | ||
|
||
class ToggleParamTest | ||
: public ToggleCollisionMonitorTestFixture, | ||
public ::testing::WithParamInterface<bool> {}; | ||
|
||
TEST_P(ToggleParamTest, test_tick) | ||
{ | ||
const bool enable = GetParam(); | ||
|
||
std::string xml_txt = | ||
std::string( | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<ToggleCollisionMonitor service_name="toggle_collision_monitor" enable=")") | ||
+ | ||
std::string(enable ? "true" : "false") + | ||
R"(" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
EXPECT_EQ(tree_->rootNode()->executeTick(), BT::NodeStatus::SUCCESS); | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P(EnableDisable, ToggleParamTest, ::testing::Values(true, false)); | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
|
||
// initialize ROS | ||
rclcpp::init(argc, argv); | ||
|
||
// initialize service and spin on new thread | ||
ToggleCollisionMonitorTestFixture::server_ = | ||
std::make_shared<ToggleCollisionMonitorService>(); | ||
std::thread server_thread([]() { | ||
rclcpp::spin(ToggleCollisionMonitorTestFixture::server_); | ||
}); | ||
|
||
int all_successful = RUN_ALL_TESTS(); | ||
|
||
// shutdown ROS | ||
rclcpp::shutdown(); | ||
server_thread.join(); | ||
|
||
return all_successful; | ||
} |
Oops, something went wrong.
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.