Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
compute_path_through_poses_action.cpp
1 // Copyright (c) 2021 Samsung Research America
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 #include <vector>
18 
19 #include "nav2_behavior_tree/plugins/action/compute_path_through_poses_action.hpp"
20 
21 namespace nav2_behavior_tree
22 {
23 
25  const std::string & xml_tag_name,
26  const std::string & action_name,
27  const BT::NodeConfiguration & conf)
28 : BtActionNode<nav2_msgs::action::ComputePathThroughPoses>(xml_tag_name, action_name, conf)
29 {
30 }
31 
33 {
34  getInput("goals", goal_.goals);
35  getInput("planner_id", goal_.planner_id);
36  if (getInput("start", goal_.start)) {
37  goal_.use_start = true;
38  }
39 }
40 
42 {
43  setOutput("path", result_.result->path);
44  setOutput("last_reached_index", result_.result->last_reached_index);
45  // Set empty error code, action was successful
46  setOutput("error_code_id", ActionResult::NONE);
47  setOutput("error_msg", "");
48  return BT::NodeStatus::SUCCESS;
49 }
50 
52 {
53  nav_msgs::msg::Path empty_path;
54  setOutput("path", empty_path);
55  setOutput("last_reached_index", ActionResult::ALL_GOALS);
56  setOutput("error_code_id", result_.result->error_code);
57  setOutput("error_msg", result_.result->error_msg);
58  return BT::NodeStatus::FAILURE;
59 }
60 
62 {
63  nav_msgs::msg::Path empty_path;
64  setOutput("path", empty_path);
65  setOutput("last_reached_index", ActionResult::ALL_GOALS);
66  // Set empty error code, action was cancelled
67  setOutput("error_code_id", ActionResult::NONE);
68  setOutput("error_msg", "");
69  return BT::NodeStatus::SUCCESS;
70 }
71 
73 {
74  setOutput("last_reached_index", ActionResult::ALL_GOALS);
75  setOutput("error_code_id", ActionResult::TIMEOUT);
76  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
77 }
78 
80 {
81  nav_msgs::msg::Path empty_path;
82  setOutput("path", empty_path);
83  setOutput("last_reached_index", ActionResult::ALL_GOALS);
84  // DO NOT reset "error_code_id" output port, we want to read it later
85  // DO NOT reset "error_msg" output port, we want to read it later
87 }
88 
89 } // namespace nav2_behavior_tree
90 
91 #include "behaviortree_cpp/bt_factory.h"
92 BT_REGISTER_NODES(factory)
93 {
94  BT::NodeBuilder builder =
95  [](const std::string & name, const BT::NodeConfiguration & config)
96  {
97  return std::make_unique<nav2_behavior_tree::ComputePathThroughPosesAction>(
98  name, "compute_path_through_poses", config);
99  };
100 
101  factory.registerBuilder<nav2_behavior_tree::ComputePathThroughPosesAction>(
102  "ComputePathThroughPoses", builder);
103 }
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::ComputePathThroughPoses.
void on_tick() override
Function to perform some user-defined operation on tick.
void halt() override
Override required by the a BT action. Cancel the action and set the path output.
void on_timeout() override
Function to perform work in a BT Node when the action server times out Such as setting the error code...
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.
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.
ComputePathThroughPosesAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ComputePathThroughPosesAction.