Skip to content

Commit e81f588

Browse files
committed
add constructor and cleanups
1 parent b93dfb2 commit e81f588

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
lines changed

behaviortree_ros2/include/behaviortree_ros2/tree_execution_server.hpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ class TreeExecutionServer
3737
using GoalHandleExecuteTree = rclcpp_action::ServerGoalHandle<ExecuteTree>;
3838

3939
/**
40-
* @brief Constructor for TreeExecutionServer.
41-
* @details This initializes a ParameterListener to read configurable options from the user and
42-
* starts an Action Server that takes requests to execute BehaviorTrees.
43-
*
44-
* @param options rclcpp::NodeOptions to pass to node_ when initializing it.
45-
* after the tree is created, while its running and after it finishes.
40+
* @brief Constructor that will create its own instance of rclcpp::Node
4641
*/
47-
explicit TreeExecutionServer(const rclcpp::NodeOptions& options);
42+
explicit TreeExecutionServer(const rclcpp::NodeOptions& options)
43+
: TreeExecutionServer(std::make_unique<rclcpp::Node>("bt_action_server", options))
44+
{}
45+
46+
/**
47+
* @brief Constructor to use when an already existing node should be used.
48+
*/
49+
explicit TreeExecutionServer(const rclcpp::Node::SharedPtr& node);
4850

4951
virtual ~TreeExecutionServer();
5052

@@ -138,6 +140,7 @@ class TreeExecutionServer
138140
private:
139141
struct Pimpl;
140142
std::unique_ptr<Pimpl> p_;
143+
rclcpp::Node::SharedPtr node_;
141144

142145
/**
143146
* @brief handle the goal requested: accept or reject. This implementation always accepts.

behaviortree_ros2/src/tree_execution_server.cpp

+27-44
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ namespace BT
3030

3131
struct TreeExecutionServer::Pimpl
3232
{
33-
Pimpl(const rclcpp::NodeOptions& node_options);
34-
35-
rclcpp::Node::SharedPtr node;
3633
rclcpp_action::Server<ExecuteTree>::SharedPtr action_server;
3734
std::thread action_thread;
3835
// thread safe bool to keep track of cancel requests
@@ -50,53 +47,20 @@ struct TreeExecutionServer::Pimpl
5047
bool factory_initialized_ = false;
5148

5249
rclcpp::WallTimer<rclcpp::VoidCallbackType>::SharedPtr single_shot_timer;
53-
54-
rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID& uuid,
55-
std::shared_ptr<const ExecuteTree::Goal> goal);
56-
57-
rclcpp_action::CancelResponse
58-
handle_cancel(const std::shared_ptr<GoalHandleExecuteTree> goal_handle);
59-
60-
void handle_accepted(const std::shared_ptr<GoalHandleExecuteTree> goal_handle);
61-
62-
void execute(const std::shared_ptr<GoalHandleExecuteTree> goal_handle);
6350
};
6451

65-
TreeExecutionServer::Pimpl::Pimpl(const rclcpp::NodeOptions& node_options)
66-
: node(std::make_unique<rclcpp::Node>("bt_action_server", node_options))
67-
{
68-
param_listener = std::make_shared<bt_server::ParamListener>(node);
69-
params = param_listener->get_params();
70-
global_blackboard = BT::Blackboard::create();
71-
}
72-
73-
TreeExecutionServer::~TreeExecutionServer()
74-
{}
75-
76-
void TreeExecutionServer::executeRegistration()
52+
TreeExecutionServer::TreeExecutionServer(const rclcpp::Node::SharedPtr& node)
53+
: p_(new Pimpl), node_(node)
7754
{
78-
// Before executing check if we have new Behaviors or Subtrees to reload
79-
p_->factory.clearRegisteredBehaviorTrees();
80-
55+
p_->param_listener = std::make_shared<bt_server::ParamListener>(node_);
8156
p_->params = p_->param_listener->get_params();
82-
// user defined method
83-
registerNodesIntoFactory(p_->factory);
84-
// load plugins from multiple directories
85-
RegisterPlugins(p_->params, p_->factory, p_->node);
86-
// load trees (XML) from multiple directories
87-
RegisterBehaviorTrees(p_->params, p_->factory, p_->node);
88-
89-
p_->factory_initialized_ = true;
90-
}
57+
p_->global_blackboard = BT::Blackboard::create();
9158

92-
TreeExecutionServer::TreeExecutionServer(const rclcpp::NodeOptions& options)
93-
: p_(new Pimpl(options))
94-
{
9559
// create the action server
9660
const auto action_name = p_->params.action_name;
9761
RCLCPP_INFO(kLogger, "Starting Action Server: %s", action_name.c_str());
9862
p_->action_server = rclcpp_action::create_server<ExecuteTree>(
99-
p_->node, action_name,
63+
node_, action_name,
10064
[this](const rclcpp_action::GoalUUID& uuid,
10165
std::shared_ptr<const ExecuteTree::Goal> goal) {
10266
return handle_goal(uuid, std::move(goal));
@@ -119,18 +83,37 @@ TreeExecutionServer::TreeExecutionServer(const rclcpp::NodeOptions& options)
11983
};
12084

12185
p_->single_shot_timer =
122-
p_->node->create_wall_timer(std::chrono::milliseconds(1), callback);
86+
node_->create_wall_timer(std::chrono::milliseconds(1), callback);
87+
}
88+
89+
TreeExecutionServer::~TreeExecutionServer()
90+
{}
91+
92+
void TreeExecutionServer::executeRegistration()
93+
{
94+
// Before executing check if we have new Behaviors or Subtrees to reload
95+
p_->factory.clearRegisteredBehaviorTrees();
96+
97+
p_->params = p_->param_listener->get_params();
98+
// user defined method
99+
registerNodesIntoFactory(p_->factory);
100+
// load plugins from multiple directories
101+
RegisterPlugins(p_->params, p_->factory, node_);
102+
// load trees (XML) from multiple directories
103+
RegisterBehaviorTrees(p_->params, p_->factory, node_);
104+
105+
p_->factory_initialized_ = true;
123106
}
124107

125108
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
126109
TreeExecutionServer::get_node_base_interface()
127110
{
128-
return p_->node->get_node_base_interface();
111+
return node_->get_node_base_interface();
129112
}
130113

131114
rclcpp::Node::SharedPtr TreeExecutionServer::node()
132115
{
133-
return p_->node;
116+
return node_;
134117
}
135118

136119
rclcpp_action::GoalResponse

0 commit comments

Comments
 (0)