@@ -176,90 +176,76 @@ void TreeExecutionServer::execute(
176176 executeRegistration ();
177177 }
178178
179- // Loop until something happens with ROS or the node completes
180- try
181- {
182- // This blackboard will be owned by "MainTree". It parent is p_->global_blackboard
183- auto root_blackboard = BT::Blackboard::create (p_->global_blackboard );
184-
185- p_->tree = p_->factory .createTree (goal->target_tree , root_blackboard);
186- p_->tree_name = goal->target_tree ;
187- p_->payload = goal->payload ;
188-
189- // call user defined function after the tree has been created
190- onTreeCreated (p_->tree );
191- p_->groot_publisher .reset ();
192- // p_->groot_publisher =
193- // std::make_shared<BT::Groot2Publisher>(p_->tree, p_->params.groot2_port);
194- BT::DebuggableTree debugTree{std::shared_ptr<BT::Tree>(&(p_->tree )), true , false };
195- BT::PublisherZMQ publisher (debugTree, p_->params .groot2_port );
196-
197- // Loop until the tree is done or a cancel is requested
198- const auto period =
199- std::chrono::milliseconds (static_cast <int >(1000.0 / p_->params .tick_frequency ));
200- auto loop_deadline = std::chrono::steady_clock::now () + period;
201-
202- // operations to be done if the tree execution is aborted, either by
203- // cancel requested or by onLoopAfterTick()
204- auto stop_action = [this , &action_result](BT::NodeStatus status,
179+ // This blackboard will be owned by "MainTree". It parent is p_->global_blackboard
180+ auto root_blackboard = BT::Blackboard::create (p_->global_blackboard );
181+
182+ p_->tree = p_->factory .createTree (goal->target_tree , root_blackboard);
183+ p_->tree_name = goal->target_tree ;
184+ p_->payload = goal->payload ;
185+
186+ // call user defined function after the tree has been created
187+ onTreeCreated (p_->tree );
188+ p_->groot_publisher .reset ();
189+ BT::DebuggableTree debugTree{std::shared_ptr<BT::Tree>(&p_->tree , [](BT::Tree*) {}), true , false };
190+ BT::PublisherZMQ publisher (debugTree, p_->params .groot2_port );
191+
192+ // Loop until the tree is done or a cancel is requested
193+ const auto period = std::chrono::milliseconds (static_cast <int >(1000.0 / p_->params .tick_frequency ));
194+ auto loop_deadline = std::chrono::steady_clock::now () + period;
195+
196+ // operations to be done if the tree execution is aborted, either by
197+ // cancel requested or by onLoopAfterTick()
198+ auto stop_action = [this , &action_result](BT::NodeStatus status,
205199 const std::string& message) {
206- p_->tree .haltTree ();
207- action_result->node_status = ConvertNodeStatus (status);
208- // override the message value if the user defined function returns it
209- if (auto msg = onTreeExecutionCompleted (status, true ))
210- {
211- action_result->return_message = msg.value ();
212- }
213- else
214- {
215- action_result->return_message = message;
216- }
217- RCLCPP_WARN (kLogger , action_result->return_message .c_str ());
218- };
219-
220- while (rclcpp::ok () && status == BT::NodeStatus::RUNNING)
200+ p_->tree .haltTree ();
201+ action_result->node_status = ConvertNodeStatus (status);
202+ // override the message value if the user defined function returns it
203+ if (auto msg = onTreeExecutionCompleted (status, true ))
221204 {
222- if (goal_handle->is_canceling ())
223- {
224- stop_action (status, " Action Server canceling and halting Behavior Tree" );
225- goal_handle->canceled (action_result);
226- return ;
227- }
228-
229- // tick the tree once and publish the action feedback
230- status = p_->tree .tickExactlyOnce ();
231-
232- if (const auto res = onLoopAfterTick (status); res.has_value ())
233- {
234- stop_action (res.value (), " Action Server aborted by onLoopAfterTick()" );
235- goal_handle->abort (action_result);
236- return ;
237- }
238-
239- if (const auto res = onLoopFeedback (); res.has_value ())
240- {
241- auto feedback = std::make_shared<ExecuteTree::Feedback>();
242- feedback->message = res.value ();
243- goal_handle->publish_feedback (feedback);
244- }
245-
246- const auto now = std::chrono::steady_clock::now ();
247- if (now < loop_deadline)
248- {
249- p_->tree .sleep (std::chrono::duration_cast<std::chrono::system_clock::duration>(
250- loop_deadline - now));
251- }
252- loop_deadline += period;
205+ action_result->return_message = msg.value ();
253206 }
254- }
255- catch (const std::exception& ex)
207+ else
208+ {
209+ action_result->return_message = message;
210+ }
211+ RCLCPP_WARN (kLogger , action_result->return_message .c_str ());
212+ };
213+
214+ while (rclcpp::ok () && status == BT::NodeStatus::RUNNING)
256215 {
257- action_result->return_message = std::string (" Behavior Tree exception:" ) + ex.what ();
258- RCLCPP_ERROR (kLogger , action_result->return_message .c_str ());
259- goal_handle->abort (action_result);
260- return ;
261- }
216+ if (goal_handle->is_canceling ())
217+ {
218+ stop_action (status, " Action Server canceling and halting Behavior Tree" );
219+ goal_handle->canceled (action_result);
220+ return ;
221+ }
262222
223+ // tick the tree once and publish the action feedback
224+ status = p_->tree .tickExactlyOnce ();
225+
226+ if (const auto res = onLoopAfterTick (status); res.has_value ())
227+ {
228+ stop_action (res.value (), " Action Server aborted by onLoopAfterTick()" );
229+ goal_handle->abort (action_result);
230+ return ;
231+ }
232+
233+ if (const auto res = onLoopFeedback (); res.has_value ())
234+ {
235+ auto feedback = std::make_shared<ExecuteTree::Feedback>();
236+ feedback->message = res.value ();
237+ goal_handle->publish_feedback (feedback);
238+ }
239+
240+ const auto now = std::chrono::steady_clock::now ();
241+ if (now < loop_deadline)
242+ {
243+ p_->tree .sleep (std::chrono::duration_cast<std::chrono::system_clock::duration>(
244+ loop_deadline - now));
245+ }
246+ loop_deadline += period;
247+ }
248+
263249 // Call user defined onTreeExecutionCompleted function.
264250 // Override the message value if the user defined function returns it
265251 if (auto msg = onTreeExecutionCompleted (status, false ))
0 commit comments