Skip to content

Commit d5d869e

Browse files
committed
fix issue #63 : expose factory and rclcppNode
1 parent 8790909 commit d5d869e

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

behaviortree_ros2/include/behaviortree_ros2/tree_execution_server.hpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,25 @@ class TreeExecutionServer
5151
/**
5252
* @brief Gets the NodeBaseInterface of node_.
5353
* @details This function exists to allow running TreeExecutionServer as a component in a composable node container.
54-
*
55-
* @return A shared_ptr to the NodeBaseInterface of node_.
54+
* The name of this method shall not change to work properly with the component composer.
5655
*/
57-
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr nodeBaseInterface();
56+
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface();
57+
58+
/// @brief Gets the rclcpp::Node pointer
59+
rclcpp::Node::SharedPtr node();
5860

59-
// name of the tree being executed
61+
/// @brief Name of the tree being executed
6062
const std::string& currentTreeName() const;
6163

62-
// tree being executed, nullptr if it doesn't exist yet.
64+
/// @brief Tree being executed, nullptr if it doesn't exist, yet.
6365
BT::Tree* currentTree();
6466

65-
// pointer to the global blackboard
67+
/// @brief Pointer to the global blackboard
6668
BT::Blackboard::Ptr globalBlackboard();
6769

70+
/// @brief Pointer to the global blackboard
71+
BT::BehaviorTreeFactory& factory();
72+
6873
protected:
6974
/**
7075
* @brief Callback invoked after the tree is created.

behaviortree_ros2/src/tree_execution_server.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,16 @@ TreeExecutionServer::TreeExecutionServer(const rclcpp::NodeOptions& options)
9595
}
9696

9797
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
98-
TreeExecutionServer::nodeBaseInterface()
98+
TreeExecutionServer::get_node_base_interface()
9999
{
100100
return p_->node->get_node_base_interface();
101101
}
102102

103+
rclcpp::Node::SharedPtr TreeExecutionServer::node()
104+
{
105+
return p_->node;
106+
}
107+
103108
rclcpp_action::GoalResponse
104109
TreeExecutionServer::handle_goal(const rclcpp_action::GoalUUID& /* uuid */,
105110
std::shared_ptr<const ExecuteTree::Goal> goal)
@@ -262,4 +267,9 @@ BT::Blackboard::Ptr TreeExecutionServer::globalBlackboard()
262267
return p_->global_blackboard;
263268
}
264269

270+
BT::BehaviorTreeFactory& TreeExecutionServer::factory()
271+
{
272+
return p_->factory;
273+
}
274+
265275
} // namespace BT

btcpp_ros2_samples/src/sample_bt_executor.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class MyActionServer : public BT::TreeExecutionServer
2828
MyActionServer(const rclcpp::NodeOptions& options) : TreeExecutionServer(options)
2929
{
3030
// here we assume that the battery voltage is published as a std_msgs::msg::Float32
31-
auto node = std::dynamic_pointer_cast<rclcpp::Node>(nodeBaseInterface());
32-
sub_ = node->create_subscription<std_msgs::msg::Float32>(
31+
sub_ = node()->create_subscription<std_msgs::msg::Float32>(
3332
"battery_level", 10, [this](const std_msgs::msg::Float32::SharedPtr msg) {
3433
// Update the global blackboard
3534
globalBlackboard()->set("battery_level", msg->data);
@@ -68,9 +67,9 @@ int main(int argc, char* argv[])
6867
// Deadlock is caused when Publishers or Subscribers are dynamically removed as the node is spinning.
6968
rclcpp::executors::MultiThreadedExecutor exec(rclcpp::ExecutorOptions(), 0, false,
7069
std::chrono::milliseconds(250));
71-
exec.add_node(action_server->nodeBaseInterface());
70+
exec.add_node(action_server->node());
7271
exec.spin();
73-
exec.remove_node(action_server->nodeBaseInterface());
72+
exec.remove_node(action_server->node());
7473

7574
rclcpp::shutdown();
7675
}

0 commit comments

Comments
 (0)