Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
follow_path_action.cpp
1 // Copyright (c) 2018 Intel Corporation
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/follow_path_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  getInput("path", goal_.path);
34  getInput("controller_id", goal_.controller_id);
35  getInput("goal_checker_id", goal_.goal_checker_id);
36  getInput("progress_checker_id", goal_.progress_checker_id);
37 }
38 
40 {
41  setOutput("error_code_id", ActionResult::NONE);
42  setOutput("error_msg", "");
43  return BT::NodeStatus::SUCCESS;
44 }
45 
47 {
48  setOutput("error_code_id", result_.result->error_code);
49  setOutput("error_msg", result_.result->error_msg);
50  return BT::NodeStatus::FAILURE;
51 }
52 
54 {
55  // Set empty error code, action was cancelled
56  setOutput("error_code_id", ActionResult::NONE);
57  setOutput("error_msg", "");
58  return BT::NodeStatus::SUCCESS;
59 }
60 
62 {
63  setOutput("error_code_id", ActionResult::CONTROLLER_TIMED_OUT);
64  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
65 }
66 
68  std::shared_ptr<const Action::Feedback>/*feedback*/)
69 {
70  // Grab the new path
71  nav_msgs::msg::Path new_path;
72  getInput("path", new_path);
73 
74  // Check if it is not same with the current one
75  if (goal_.path != new_path && new_path != nav_msgs::msg::Path()) {
76  // the action server on the next loop iteration
77  goal_.path = new_path;
78  goal_updated_ = true;
79  }
80 
81  std::string new_controller_id;
82  getInput("controller_id", new_controller_id);
83 
84  if (goal_.controller_id != new_controller_id) {
85  goal_.controller_id = new_controller_id;
86  goal_updated_ = true;
87  }
88 
89  std::string new_goal_checker_id;
90  getInput("goal_checker_id", new_goal_checker_id);
91 
92  if (goal_.goal_checker_id != new_goal_checker_id) {
93  goal_.goal_checker_id = new_goal_checker_id;
94  goal_updated_ = true;
95  }
96 
97  std::string new_progress_checker_id;
98  getInput("progress_checker_id", new_progress_checker_id);
99 
100  if (goal_.progress_checker_id != new_progress_checker_id) {
101  goal_.progress_checker_id = new_progress_checker_id;
102  goal_updated_ = true;
103  }
104 }
105 
106 } // namespace nav2_behavior_tree
107 
108 #include "behaviortree_cpp/bt_factory.h"
109 BT_REGISTER_NODES(factory)
110 {
111  BT::NodeBuilder builder =
112  [](const std::string & name, const BT::NodeConfiguration & config)
113  {
114  return std::make_unique<nav2_behavior_tree::FollowPathAction>(
115  name, "follow_path", config);
116  };
117 
118  factory.registerBuilder<nav2_behavior_tree::FollowPathAction>(
119  "FollowPath", builder);
120 }
Abstract class representing an action based BT node.
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::FollowPath.
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...
BT::NodeStatus on_success() override
Function to perform some user-defined operation upon successful completion of the action.
FollowPathAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::FollowPathAction.
BT::NodeStatus on_aborted() override
Function to perform some user-defined operation upon abortion 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...
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.