Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
compute_and_track_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_and_track_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  nav_msgs::msg::Path empty_path;
30  nav2_msgs::msg::Route empty_route;
31  feedback_.last_node_id = 0;
32  feedback_.next_node_id = 0;
33  feedback_.current_edge_id = 0;
34  feedback_.route = empty_route;
35  feedback_.path = empty_path;
36  feedback_.rerouted = false;
37 }
38 
40 {
41  bool use_poses = false, use_start = false;
42  getInput("use_poses", use_poses);
43  if (use_poses) {
44  goal_.use_poses = true;
45  getInput("goal", goal_.goal);
46 
47  goal_.use_start = false;
48  getInput("use_start", use_start);
49  if (use_start) {
50  getInput("start", goal_.start);
51  goal_.use_start = true;
52  }
53  } else {
54  getInput("start_id", goal_.start_id);
55  getInput("goal_id", goal_.goal_id);
56  goal_.use_start = false;
57  goal_.use_poses = false;
58  }
59 }
60 
62 {
64  setOutput("execution_duration", result_.result->execution_duration);
65  setOutput("error_code_id", ActionResult::NONE);
66  setOutput("error_msg", "");
67  return BT::NodeStatus::SUCCESS;
68 }
69 
71 {
73  setOutput("execution_duration", builtin_interfaces::msg::Duration());
74  setOutput("error_code_id", result_.result->error_code);
75  setOutput("error_msg", result_.result->error_msg);
76  return BT::NodeStatus::FAILURE;
77 }
78 
80 {
82  // Set empty error code, action was cancelled
83  setOutput("execution_duration", builtin_interfaces::msg::Duration());
84  setOutput("error_code_id", ActionResult::NONE);
85  setOutput("error_msg", "");
86  return BT::NodeStatus::SUCCESS;
87 }
88 
90 {
91  setOutput("error_code_id", ActionResult::TIMEOUT);
92  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
93 }
94 
96  std::shared_ptr<const Action::Feedback> feedback)
97 {
98  // Check for request updates to the goal
99  bool use_poses = false, use_start = false;
100  getInput("use_start", use_start);
101  getInput("use_poses", use_poses);
102 
103  if (goal_.use_poses != use_poses) {
104  goal_updated_ = true;
105  }
106 
107  if (use_poses) {
108  geometry_msgs::msg::PoseStamped goal;
109  getInput("goal", goal);
110  if (goal_.goal != goal) {
111  goal_updated_ = true;
112  }
113 
114  if (goal_.use_start != use_start) {
115  goal_updated_ = true;
116  }
117  if (use_start) {
118  geometry_msgs::msg::PoseStamped start;
119  getInput("start", start);
120  if (goal_.start != start) {
121  goal_updated_ = true;
122  }
123  }
124  } else {
125  // Check if the start and goal IDs have changed
126  unsigned int start_id = 0;
127  unsigned int goal_id = 0;
128  getInput("start_id", start_id);
129  getInput("goal_id", goal_id);
130  if (goal_.start_id != start_id) {
131  goal_updated_ = true;
132  }
133  if (goal_.goal_id != goal_id) {
134  goal_updated_ = true;
135  }
136  }
137 
138  // If we're updating the request, we need to fully update the goal
139  // Easier to call on_tick() again than to duplicate the code
140  if (goal_updated_) {
141  on_tick();
142  }
143 
144  if (feedback) {
145  feedback_ = *feedback;
146  setOutput("last_node_id", feedback_.last_node_id);
147  setOutput("next_node_id", feedback_.next_node_id);
148  setOutput("current_edge_id", feedback_.current_edge_id);
149  setOutput("route", feedback_.route);
150  setOutput("path", feedback_.path);
151  setOutput("rerouted", feedback_.rerouted);
152  }
153 }
154 
156 {
157  nav_msgs::msg::Path empty_path;
158  nav2_msgs::msg::Route empty_route;
159  feedback_.last_node_id = 0;
160  feedback_.next_node_id = 0;
161  feedback_.current_edge_id = 0;
162  feedback_.route = empty_route;
163  feedback_.path = empty_path;
164  feedback_.rerouted = false;
165  setOutput("last_node_id", feedback_.last_node_id);
166  setOutput("next_node_id", feedback_.next_node_id);
167  setOutput("current_edge_id", feedback_.current_edge_id);
168  setOutput("route", feedback_.route);
169  setOutput("path", feedback_.path);
170  setOutput("rerouted", feedback_.rerouted);
171 }
172 
173 } // namespace nav2_behavior_tree
174 
175 #include "behaviortree_cpp/bt_factory.h"
176 BT_REGISTER_NODES(factory)
177 {
178  BT::NodeBuilder builder =
179  [](const std::string & name, const BT::NodeConfiguration & config)
180  {
181  return std::make_unique<nav2_behavior_tree::ComputeAndTrackRouteAction>(
182  name, "compute_and_track_route", config);
183  };
184 
185  factory.registerBuilder<nav2_behavior_tree::ComputeAndTrackRouteAction>(
186  "ComputeAndTrackRoute", builder);
187 }
Abstract class representing an action based BT node.
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::ComputeAndTrackRoute.
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.
BT::NodeStatus on_aborted() override
Function to perform some user-defined operation upon abortion of the action.
ComputeAndTrackRouteAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ComputeAndTrackRouteAction.
BT::NodeStatus on_success() override
Function to perform some user-defined operation upon successful completion of the action.
void on_wait_for_result(std::shared_ptr< const Action::Feedback > feedback) override
Function to perform some user-defined operation after a timeout waiting for a result that hasn't been...
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 resetFeedbackAndOutputPorts()
Function to set all feedbacks and output ports to be null values.