@@ -44,7 +44,8 @@ struct TreeExecutionServer::Pimpl
44
44
BT::BehaviorTreeFactory factory;
45
45
std::shared_ptr<BT::Groot2Publisher> groot_publisher;
46
46
47
- std::string current_tree_name;
47
+ std::string tree_name;
48
+ std::string payload;
48
49
std::shared_ptr<BT::Tree> tree;
49
50
BT::Blackboard::Ptr global_blackboard;
50
51
bool factory_initialized_ = false ;
@@ -190,7 +191,8 @@ void TreeExecutionServer::execute(
190
191
191
192
p_->tree = std::make_shared<BT::Tree>();
192
193
*(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 ;
194
196
195
197
// call user defined function after the tree has been created
196
198
onTreeCreated (*p_->tree );
@@ -208,10 +210,14 @@ void TreeExecutionServer::execute(
208
210
auto stop_action = [this , &action_result](BT::NodeStatus status,
209
211
const std::string& message) {
210
212
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 ());
213
215
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
+ }
215
221
};
216
222
217
223
while (rclcpp::ok () && status == BT::NodeStatus::RUNNING)
@@ -236,7 +242,7 @@ void TreeExecutionServer::execute(
236
242
if (const auto res = onLoopFeedback (); res.has_value ())
237
243
{
238
244
auto feedback = std::make_shared<ExecuteTree::Feedback>();
239
- feedback->msg = res.value ();
245
+ feedback->message = res.value ();
240
246
goal_handle->publish_feedback (feedback);
241
247
}
242
248
@@ -250,40 +256,38 @@ void TreeExecutionServer::execute(
250
256
}
251
257
catch (const std::exception & ex)
252
258
{
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 ());
255
261
goal_handle->abort (action_result);
256
262
return ;
257
263
}
258
264
259
- // call user defined execution complete function
260
- onTreeExecutionCompleted (status, false );
261
-
262
265
// set the node_status result to the action
263
266
action_result->node_status = ConvertNodeStatus (status);
264
267
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 ))
272
271
{
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 ();
278
273
}
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 ;
279
283
}
280
284
281
- const std::string& TreeExecutionServer::currentTreeName () const
285
+ const std::string& TreeExecutionServer::goalPayload () const
282
286
{
283
- return p_->current_tree_name ;
287
+ return p_->payload ;
284
288
}
285
289
286
- BT::Tree* TreeExecutionServer::currentTree ()
290
+ BT::Tree* TreeExecutionServer::tree ()
287
291
{
288
292
return p_->tree ? p_->tree .get () : nullptr ;
289
293
}
0 commit comments