Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
compute_route_action.cpp
1 // Copyright (c) 2025 Open Navigation LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <memory>
16 #include <string>
17 
18 #include "nav2_behavior_tree/plugins/action/compute_route_action.hpp"
19 
20 namespace nav2_behavior_tree
21 {
22 
24  const std::string & xml_tag_name,
25  const std::string & action_name,
26  const BT::NodeConfiguration & conf)
27 : BtActionNode<Action>(xml_tag_name, action_name, conf)
28 {
29 }
30 
32 {
33  bool use_poses = false, use_start = false;
34  getInput("use_poses", use_poses);
35  if (use_poses) {
36  goal_.use_poses = true;
37  getInput("goal", goal_.goal);
38 
39  goal_.use_start = false;
40  getInput("use_start", use_start);
41  if (use_start) {
42  getInput("start", goal_.start);
43  goal_.use_start = true;
44  }
45  } else {
46  getInput("start_id", goal_.start_id);
47  getInput("goal_id", goal_.goal_id);
48  goal_.use_start = false;
49  goal_.use_poses = false;
50  }
51 }
52 
54 {
55  setOutput("path", result_.result->path);
56  setOutput("route", result_.result->route);
57  setOutput("planning_time", result_.result->planning_time);
58  // Set empty error code, action was successful
59  setOutput("error_code_id", ActionResult::NONE);
60  setOutput("error_msg", "");
61  return BT::NodeStatus::SUCCESS;
62 }
63 
65 {
66  nav_msgs::msg::Path empty_path;
67  setOutput("path", empty_path);
68  nav2_msgs::msg::Route empty_route;
69  setOutput("route", empty_route);
70  setOutput("planning_time", builtin_interfaces::msg::Duration());
71 }
72 
74 {
75  resetPorts();
76  setOutput("error_code_id", result_.result->error_code);
77  setOutput("error_msg", result_.result->error_msg);
78  return BT::NodeStatus::FAILURE;
79 }
80 
82 {
83  resetPorts();
84  // Set empty error code, action was cancelled
85  setOutput("error_code_id", ActionResult::NONE);
86  setOutput("error_msg", "");
87  return BT::NodeStatus::SUCCESS;
88 }
89 
91 {
92  setOutput("error_code_id", ActionResult::TIMEOUT);
93  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
94 }
95 
97 {
98  resetPorts();
99  // DO NOT reset "error_code_id" output port, we want to read it later
100  // DO NOT reset "error_msg" output port, we want to read it later
102 }
103 
104 } // namespace nav2_behavior_tree
105 
106 #include "behaviortree_cpp/bt_factory.h"
107 BT_REGISTER_NODES(factory)
108 {
109  BT::NodeBuilder builder =
110  [](const std::string & name, const BT::NodeConfiguration & config)
111  {
112  return std::make_unique<nav2_behavior_tree::ComputeRouteAction>(
113  name, "compute_route", config);
114  };
115 
116  factory.registerBuilder<nav2_behavior_tree::ComputeRouteAction>(
117  "ComputeRoute", builder);
118 }
Abstract class representing an action based BT node.
void halt() override
The other (optional) override required by a BT action. In this case, we make sure to cancel the ROS2 ...
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::ComputeRoute.
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.
ComputeRouteAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ComputeRoute.
void halt() override
Override required by the a BT action. Cancel the action and set the path output.
void on_tick() override
Function to perform some user-defined operation on tick.
void on_timeout() override
Function to perform work in a BT Node when the action server times out Such as setting the error code...
void resetPorts()
Reset output port values on failure.
BT::NodeStatus on_aborted() override
Function to perform some user-defined operation upon abortion of the action.
BT::NodeStatus on_success() override
Function to perform some user-defined operation upon successful completion of the action.