Nav2 Navigation Stack - kilted  kilted
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 }
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("execution_duration", result_.result->execution_duration);
56  setOutput("error_code_id", ActionResult::NONE);
57  setOutput("error_msg", "");
58  return BT::NodeStatus::SUCCESS;
59 }
60 
62 {
63  setOutput("execution_duration", builtin_interfaces::msg::Duration());
64  setOutput("error_code_id", result_.result->error_code);
65  setOutput("error_msg", result_.result->error_msg);
66  return BT::NodeStatus::FAILURE;
67 }
68 
70 {
71  // Set empty error code, action was cancelled
72  setOutput("execution_duration", builtin_interfaces::msg::Duration());
73  setOutput("error_code_id", ActionResult::NONE);
74  setOutput("error_msg", "");
75  return BT::NodeStatus::SUCCESS;
76 }
77 
79 {
80  setOutput("error_code_id", ActionResult::TIMEOUT);
81  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
82 }
83 
85  std::shared_ptr<const Action::Feedback>/*feedback*/)
86 {
87  // Check for request updates to the goal
88  bool use_poses = false, use_start = false;
89  getInput("use_start", use_start);
90  getInput("use_poses", use_poses);
91 
92  if (goal_.use_poses != use_poses) {
93  goal_updated_ = true;
94  }
95 
96  if (use_poses) {
97  geometry_msgs::msg::PoseStamped goal;
98  getInput("goal", goal);
99  if (goal_.goal != goal) {
100  goal_updated_ = true;
101  }
102 
103  if (goal_.use_start != use_start) {
104  goal_updated_ = true;
105  }
106  if (use_start) {
107  geometry_msgs::msg::PoseStamped start;
108  getInput("start", start);
109  if (goal_.start != start) {
110  goal_updated_ = true;
111  }
112  }
113  } else {
114  // Check if the start and goal IDs have changed
115  unsigned int start_id = 0;
116  unsigned int goal_id = 0;
117  getInput("start_id", start_id);
118  getInput("goal_id", goal_id);
119  if (goal_.start_id != start_id) {
120  goal_updated_ = true;
121  }
122  if (goal_.goal_id != goal_id) {
123  goal_updated_ = true;
124  }
125  }
126 
127  // If we're updating the request, we need to fully update the goal
128  // Easier to call on_tick() again than to duplicate the code
129  if (goal_updated_) {
130  on_tick();
131  }
132 }
133 
134 } // namespace nav2_behavior_tree
135 
136 #include "behaviortree_cpp/bt_factory.h"
137 BT_REGISTER_NODES(factory)
138 {
139  BT::NodeBuilder builder =
140  [](const std::string & name, const BT::NodeConfiguration & config)
141  {
142  return std::make_unique<nav2_behavior_tree::ComputeAndTrackRouteAction>(
143  name, "compute_and_track_route", config);
144  };
145 
146  factory.registerBuilder<nav2_behavior_tree::ComputeAndTrackRouteAction>(
147  "ComputeAndTrackRoute", builder);
148 }
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...