@@ -41,7 +41,8 @@ struct TreeExecutionServer::Pimpl
41
41
BT::BehaviorTreeFactory factory;
42
42
std::shared_ptr<BT::Groot2Publisher> groot_publisher;
43
43
44
- std::string current_tree_name;
44
+ std::string tree_name;
45
+ std::string payload;
45
46
std::shared_ptr<BT::Tree> tree;
46
47
BT::Blackboard::Ptr global_blackboard;
47
48
bool factory_initialized_ = false ;
@@ -173,7 +174,8 @@ void TreeExecutionServer::execute(
173
174
174
175
p_->tree = std::make_shared<BT::Tree>();
175
176
*(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 ;
177
179
178
180
// call user defined function after the tree has been created
179
181
onTreeCreated (*p_->tree );
@@ -191,10 +193,14 @@ void TreeExecutionServer::execute(
191
193
auto stop_action = [this , &action_result](BT::NodeStatus status,
192
194
const std::string& message) {
193
195
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 ());
196
198
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
+ }
198
204
};
199
205
200
206
while (rclcpp::ok () && status == BT::NodeStatus::RUNNING)
@@ -219,7 +225,7 @@ void TreeExecutionServer::execute(
219
225
if (const auto res = onLoopFeedback (); res.has_value ())
220
226
{
221
227
auto feedback = std::make_shared<ExecuteTree::Feedback>();
222
- feedback->msg = res.value ();
228
+ feedback->message = res.value ();
223
229
goal_handle->publish_feedback (feedback);
224
230
}
225
231
@@ -233,40 +239,38 @@ void TreeExecutionServer::execute(
233
239
}
234
240
catch (const std::exception& ex)
235
241
{
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 ());
238
244
goal_handle->abort (action_result);
239
245
return ;
240
246
}
241
247
242
- // call user defined execution complete function
243
- onTreeExecutionCompleted (status, false );
244
-
245
248
// set the node_status result to the action
246
249
action_result->node_status = ConvertNodeStatus (status);
247
250
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 ))
255
254
{
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 ();
261
256
}
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 ;
262
266
}
263
267
264
- const std::string& TreeExecutionServer::currentTreeName () const
268
+ const std::string& TreeExecutionServer::goalPayload () const
265
269
{
266
- return p_->current_tree_name ;
270
+ return p_->payload ;
267
271
}
268
272
269
- BT::Tree* TreeExecutionServer::currentTree ()
273
+ BT::Tree* TreeExecutionServer::tree ()
270
274
{
271
275
return p_->tree ? p_->tree .get () : nullptr ;
272
276
}
0 commit comments