Skip to content

Commit 10d4cef

Browse files
committed
add payloads to Action
1 parent 7e727ba commit 10d4cef

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

behaviortree_ros2/include/behaviortree_ros2/tree_execution_server.hpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ class TreeExecutionServer
5959
rclcpp::Node::SharedPtr node();
6060

6161
/// @brief Name of the tree being executed
62-
const std::string& currentTreeName() const;
62+
const std::string& treeName() const;
63+
64+
/// @brief The payload received in the last goal
65+
const std::string& goalPayload() const;
6366

6467
/// @brief Tree being executed, nullptr if it doesn't exist, yet.
65-
BT::Tree* currentTree();
68+
BT::Tree* tree();
6669

6770
/// @brief Pointer to the global blackboard
6871
BT::Blackboard::Ptr globalBlackboard();
@@ -108,9 +111,14 @@ class TreeExecutionServer
108111
*
109112
* @param status The status of the tree after the last tick
110113
* @param was_cancelled True if the action was cancelled by the Action Client
114+
*
115+
* @return if not std::nullopt, the string will be sent as [return_message] to the Action Client.
111116
*/
112-
virtual void onTreeExecutionCompleted(BT::NodeStatus status, bool was_cancelled)
113-
{}
117+
virtual std::optional<std::string> onTreeExecutionCompleted(BT::NodeStatus status,
118+
bool was_cancelled)
119+
{
120+
return std::nullopt;
121+
}
114122

115123
/**
116124
* @brief onLoopFeedback is a callback invoked at each loop, after tree.tickOnce().

behaviortree_ros2/src/tree_execution_server.cpp

+30-26
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ struct TreeExecutionServer::Pimpl
4444
BT::BehaviorTreeFactory factory;
4545
std::shared_ptr<BT::Groot2Publisher> groot_publisher;
4646

47-
std::string current_tree_name;
47+
std::string tree_name;
48+
std::string payload;
4849
std::shared_ptr<BT::Tree> tree;
4950
BT::Blackboard::Ptr global_blackboard;
5051
bool factory_initialized_ = false;
@@ -190,7 +191,8 @@ void TreeExecutionServer::execute(
190191

191192
p_->tree = std::make_shared<BT::Tree>();
192193
*(p_->tree) = p_->factory.createTree(goal->target_tree, root_blackboard);
193-
p_->current_tree_name = goal->target_tree;
194+
p_->tree_name = goal->target_tree;
195+
p_->payload = goal->payload;
194196

195197
// call user defined function after the tree has been created
196198
onTreeCreated(*p_->tree);
@@ -208,10 +210,14 @@ void TreeExecutionServer::execute(
208210
auto stop_action = [this, &action_result](BT::NodeStatus status,
209211
const std::string& message) {
210212
action_result->node_status = ConvertNodeStatus(status);
211-
action_result->error_message = message;
212-
RCLCPP_WARN(kLogger, action_result->error_message.c_str());
213+
action_result->return_message = message;
214+
RCLCPP_WARN(kLogger, action_result->return_message.c_str());
213215
p_->tree->haltTree();
214-
onTreeExecutionCompleted(status, true);
216+
// override the message value if the user defined function returns it
217+
if(auto msg = onTreeExecutionCompleted(status, true))
218+
{
219+
action_result->return_message = msg.value();
220+
}
215221
};
216222

217223
while(rclcpp::ok() && status == BT::NodeStatus::RUNNING)
@@ -236,7 +242,7 @@ void TreeExecutionServer::execute(
236242
if(const auto res = onLoopFeedback(); res.has_value())
237243
{
238244
auto feedback = std::make_shared<ExecuteTree::Feedback>();
239-
feedback->msg = res.value();
245+
feedback->message = res.value();
240246
goal_handle->publish_feedback(feedback);
241247
}
242248

@@ -250,40 +256,38 @@ void TreeExecutionServer::execute(
250256
}
251257
catch(const std::exception& ex)
252258
{
253-
action_result->error_message = std::string("Behavior Tree exception:") + ex.what();
254-
RCLCPP_ERROR(kLogger, action_result->error_message.c_str());
259+
action_result->return_message = std::string("Behavior Tree exception:") + ex.what();
260+
RCLCPP_ERROR(kLogger, action_result->return_message.c_str());
255261
goal_handle->abort(action_result);
256262
return;
257263
}
258264

259-
// call user defined execution complete function
260-
onTreeExecutionCompleted(status, false);
261-
262265
// set the node_status result to the action
263266
action_result->node_status = ConvertNodeStatus(status);
264267

265-
// return success or aborted for the action result
266-
if(status == BT::NodeStatus::SUCCESS)
267-
{
268-
RCLCPP_INFO(kLogger, "BT finished with status: %s", BT::toStr(status).c_str());
269-
goal_handle->succeed(action_result);
270-
}
271-
else
268+
// Call user defined onTreeExecutionCompleted function.
269+
// Override the message value if the user defined function returns it
270+
if(auto msg = onTreeExecutionCompleted(status, false))
272271
{
273-
action_result->error_message = std::string("Behavior Tree failed during execution "
274-
"with status: ") +
275-
BT::toStr(status);
276-
RCLCPP_ERROR(kLogger, action_result->error_message.c_str());
277-
goal_handle->abort(action_result);
272+
action_result->return_message = msg.value();
278273
}
274+
275+
// return success or aborted for the action result
276+
RCLCPP_INFO(kLogger, "BT finished with status: %s", BT::toStr(status).c_str());
277+
goal_handle->succeed(action_result);
278+
}
279+
280+
const std::string& TreeExecutionServer::treeName() const
281+
{
282+
return p_->tree_name;
279283
}
280284

281-
const std::string& TreeExecutionServer::currentTreeName() const
285+
const std::string& TreeExecutionServer::goalPayload() const
282286
{
283-
return p_->current_tree_name;
287+
return p_->payload;
284288
}
285289

286-
BT::Tree* TreeExecutionServer::currentTree()
290+
BT::Tree* TreeExecutionServer::tree()
287291
{
288292
return p_->tree ? p_->tree.get() : nullptr;
289293
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
# Request
1+
#### Request ####
2+
3+
# Name of the tree to execute
24
string target_tree
5+
# Optional, implementation-dependent, payload.
6+
string payload
37
---
4-
# Result
5-
string error_message
8+
#### Result ####
9+
10+
# Status of the tree
611
NodeStatus node_status
12+
# result payload or error message
13+
string return_message
714
---
8-
# Feedback. This can be customized by the user
9-
string msg
15+
#### Feedback ####
16+
17+
#This can be customized by the user
18+
string message

btcpp_ros2_samples/src/sample_bt_executor.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ class MyActionServer : public BT::TreeExecutionServer
4444
logger_cout_ = std::make_shared<BT::StdCoutLogger>(tree);
4545
}
4646

47-
void onTreeExecutionCompleted(BT::NodeStatus status, bool was_cancelled) override
47+
std::optional<std::string> onTreeExecutionCompleted(BT::NodeStatus status,
48+
bool was_cancelled) override
4849
{
4950
// NOT really needed, even if logger_cout_ may contain a dangling pointer of the tree
5051
// at this point
5152
logger_cout_.reset();
53+
return std::nullopt;
5254
}
5355

5456
private:

0 commit comments

Comments
 (0)