| 174 | } |
| 175 | |
| 176 | void TreeExecutionServer::execute( |
| 177 | const std::shared_ptr<GoalHandleExecuteTree> goal_handle) |
| 178 | { |
| 179 | const auto goal = goal_handle->get_goal(); |
| 180 | BT::NodeStatus status = BT::NodeStatus::RUNNING; |
| 181 | auto action_result = std::make_shared<ExecuteTree::Result>(); |
| 182 | |
| 183 | // Before executing check if we have new Behaviors or Subtrees to reload |
| 184 | if(p_->param_listener->is_old(p_->params)) |
| 185 | { |
| 186 | executeRegistration(); |
| 187 | } |
| 188 | |
| 189 | // Loop until something happens with ROS or the node completes |
| 190 | try |
| 191 | { |
| 192 | // This blackboard will be owned by "MainTree". It parent is p_->global_blackboard |
| 193 | auto root_blackboard = BT::Blackboard::create(p_->global_blackboard); |
| 194 | |
| 195 | p_->tree = p_->factory.createTree(goal->target_tree, root_blackboard); |
| 196 | p_->tree_name = goal->target_tree; |
| 197 | p_->payload = goal->payload; |
| 198 | |
| 199 | // call user defined function after the tree has been created |
| 200 | onTreeCreated(p_->tree); |
| 201 | p_->groot_publisher.reset(); |
| 202 | p_->groot_publisher = |
| 203 | std::make_shared<BT::Groot2Publisher>(p_->tree, p_->params.groot2_port); |
| 204 | |
| 205 | // Loop until the tree is done or a cancel is requested |
| 206 | const auto period = |
| 207 | std::chrono::milliseconds(static_cast<int>(1000.0 / p_->params.tick_frequency)); |
| 208 | auto loop_deadline = std::chrono::steady_clock::now() + period; |
| 209 | |
| 210 | // operations to be done if the tree execution is aborted, either by |
| 211 | // cancel requested or by onLoopAfterTick() |
| 212 | auto stop_action = [this, &action_result](BT::NodeStatus status, |
| 213 | const std::string& message) { |
| 214 | p_->tree.haltTree(); |
| 215 | action_result->node_status = ConvertNodeStatus(status); |
| 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 | } |
| 221 | else |
| 222 | { |
| 223 | action_result->return_message = message; |
| 224 | } |
| 225 | RCLCPP_WARN(kLogger, action_result->return_message.c_str()); |
| 226 | }; |
| 227 | |
| 228 | while(rclcpp::ok() && status == BT::NodeStatus::RUNNING) |
| 229 | { |
| 230 | if(goal_handle->is_canceling()) |
| 231 | { |
| 232 | stop_action(status, "Action Server canceling and halting Behavior Tree"); |
| 233 | goal_handle->canceled(action_result); |
nothing calls this directly
no test coverage detected