Skip to content

Commit b0af5f6

Browse files
committed
add payloads to Action
1 parent e7b2926 commit b0af5f6

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ class TreeExecutionServer
6161
rclcpp::Node::SharedPtr node();
6262

6363
/// @brief Name of the tree being executed
64-
const std::string& currentTreeName() const;
64+
const std::string& treeName() const;
65+
66+
/// @brief The payload received in the last goal
67+
const std::string& goalPayload() const;
6568

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

6972
/// @brief Pointer to the global blackboard
7073
BT::Blackboard::Ptr globalBlackboard();
@@ -110,9 +113,14 @@ class TreeExecutionServer
110113
*
111114
* @param status The status of the tree after the last tick
112115
* @param was_cancelled True if the action was cancelled by the Action Client
116+
*
117+
* @return if not std::nullopt, the string will be sent as [return_message] to the Action Client.
113118
*/
114-
virtual void onTreeExecutionCompleted(BT::NodeStatus status, bool was_cancelled)
115-
{}
119+
virtual std::optional<std::string> onTreeExecutionCompleted(BT::NodeStatus status,
120+
bool was_cancelled)
121+
{
122+
return std::nullopt;
123+
}
116124

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

behaviortree_ros2/src/tree_execution_server.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ struct TreeExecutionServer::Pimpl
4141
BT::BehaviorTreeFactory factory;
4242
std::shared_ptr<BT::Groot2Publisher> groot_publisher;
4343

44-
std::string current_tree_name;
44+
std::string tree_name;
45+
std::string payload;
4546
std::shared_ptr<BT::Tree> tree;
4647
BT::Blackboard::Ptr global_blackboard;
4748
bool factory_initialized_ = false;
@@ -173,7 +174,8 @@ void TreeExecutionServer::execute(
173174

174175
p_->tree = std::make_shared<BT::Tree>();
175176
*(p_->tree) = p_->factory.createTree(goal->target_tree, root_blackboard);
176-
p_->current_tree_name = goal->target_tree;
177+
p_->tree_name = goal->target_tree;
178+
p_->payload = goal->payload;
177179

178180
// call user defined function after the tree has been created
179181
onTreeCreated(*p_->tree);
@@ -191,10 +193,14 @@ void TreeExecutionServer::execute(
191193
auto stop_action = [this, &action_result](BT::NodeStatus status,
192194
const std::string& message) {
193195
action_result->node_status = ConvertNodeStatus(status);
194-
action_result->error_message = message;
195-
RCLCPP_WARN(kLogger, action_result->error_message.c_str());
196+
action_result->return_message = message;
197+
RCLCPP_WARN(kLogger, action_result->return_message.c_str());
196198
p_->tree->haltTree();
197-
onTreeExecutionCompleted(status, true);
199+
// override the message value if the user defined function returns it
200+
if(auto msg = onTreeExecutionCompleted(status, true))
201+
{
202+
action_result->return_message = msg.value();
203+
}
198204
};
199205

200206
while(rclcpp::ok() && status == BT::NodeStatus::RUNNING)
@@ -219,7 +225,7 @@ void TreeExecutionServer::execute(
219225
if(const auto res = onLoopFeedback(); res.has_value())
220226
{
221227
auto feedback = std::make_shared<ExecuteTree::Feedback>();
222-
feedback->msg = res.value();
228+
feedback->message = res.value();
223229
goal_handle->publish_feedback(feedback);
224230
}
225231

@@ -233,40 +239,38 @@ void TreeExecutionServer::execute(
233239
}
234240
catch(const std::exception& ex)
235241
{
236-
action_result->error_message = std::string("Behavior Tree exception:") + ex.what();
237-
RCLCPP_ERROR(kLogger, action_result->error_message.c_str());
242+
action_result->return_message = std::string("Behavior Tree exception:") + ex.what();
243+
RCLCPP_ERROR(kLogger, action_result->return_message.c_str());
238244
goal_handle->abort(action_result);
239245
return;
240246
}
241247

242-
// call user defined execution complete function
243-
onTreeExecutionCompleted(status, false);
244-
245248
// set the node_status result to the action
246249
action_result->node_status = ConvertNodeStatus(status);
247250

248-
// return success or aborted for the action result
249-
if(status == BT::NodeStatus::SUCCESS)
250-
{
251-
RCLCPP_INFO(kLogger, "BT finished with status: %s", BT::toStr(status).c_str());
252-
goal_handle->succeed(action_result);
253-
}
254-
else
251+
// Call user defined onTreeExecutionCompleted function.
252+
// Override the message value if the user defined function returns it
253+
if(auto msg = onTreeExecutionCompleted(status, false))
255254
{
256-
action_result->error_message = std::string("Behavior Tree failed during execution "
257-
"with status: ") +
258-
BT::toStr(status);
259-
RCLCPP_ERROR(kLogger, action_result->error_message.c_str());
260-
goal_handle->abort(action_result);
255+
action_result->return_message = msg.value();
261256
}
257+
258+
// return success or aborted for the action result
259+
RCLCPP_INFO(kLogger, "BT finished with status: %s", BT::toStr(status).c_str());
260+
goal_handle->succeed(action_result);
261+
}
262+
263+
const std::string& TreeExecutionServer::treeName() const
264+
{
265+
return p_->tree_name;
262266
}
263267

264-
const std::string& TreeExecutionServer::currentTreeName() const
268+
const std::string& TreeExecutionServer::goalPayload() const
265269
{
266-
return p_->current_tree_name;
270+
return p_->payload;
267271
}
268272

269-
BT::Tree* TreeExecutionServer::currentTree()
273+
BT::Tree* TreeExecutionServer::tree()
270274
{
271275
return p_->tree ? p_->tree.get() : nullptr;
272276
}
Lines changed: 14 additions & 5 deletions
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

Lines changed: 3 additions & 1 deletion
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)