Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
back_up_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 <string>
16 #include <memory>
17 
18 #include "nav2_behavior_tree/plugins/action/back_up_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<nav2_msgs::action::BackUp>(xml_tag_name, action_name, conf)
28 {
29 }
30 
32 {
33  double dist;
34  getInput("backup_dist", dist);
35  double speed;
36  getInput("backup_speed", speed);
37  double time_allowance;
38  getInput("time_allowance", time_allowance);
39  bool disable_collision_checks;
40  getInput("disable_collision_checks", disable_collision_checks);
41 
42  // Populate the input message
43  goal_.target.x = dist;
44  goal_.target.y = 0.0;
45  goal_.target.z = 0.0;
46  goal_.speed = speed;
47  goal_.time_allowance = rclcpp::Duration::from_seconds(time_allowance);
48  goal_.disable_collision_checks = disable_collision_checks;
49 }
50 
52 {
53  if (!BT::isStatusActive(status())) {
54  initialize();
55  }
56 
58 }
59 
60 BT::NodeStatus BackUpAction::on_success()
61 {
62  setOutput("error_code_id", ActionResult::NONE);
63  setOutput("error_msg", "");
64  return BT::NodeStatus::SUCCESS;
65 }
66 
67 BT::NodeStatus BackUpAction::on_aborted()
68 {
69  setOutput("error_code_id", result_.result->error_code);
70  setOutput("error_msg", result_.result->error_msg);
71  return BT::NodeStatus::FAILURE;
72 }
73 
75 {
76  setOutput("error_code_id", ActionResult::NONE);
77  setOutput("error_msg", "");
78  return BT::NodeStatus::SUCCESS;
79 }
80 
82 {
83  setOutput("error_code_id", ActionResult::TIMEOUT);
84  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
85 }
86 
87 } // namespace nav2_behavior_tree
88 
89 #include "behaviortree_cpp/bt_factory.h"
90 BT_REGISTER_NODES(factory)
91 {
92  BT::NodeBuilder builder =
93  [](const std::string & name, const BT::NodeConfiguration & config)
94  {
95  return std::make_unique<nav2_behavior_tree::BackUpAction>(
96  name, "backup", config);
97  };
98 
99  factory.registerBuilder<nav2_behavior_tree::BackUpAction>("BackUp", builder);
100 }
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::BackUp.
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.
BT::NodeStatus on_aborted() override
Function to perform some user-defined operation upon abortion of the action.
void on_tick() override
Function to perform some user-defined operation on tick.
void initialize()
Function to read parameters and initialize class variables.
BackUpAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::BackUpAction.
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.
Abstract class representing an action based BT node.
void increment_recovery_count()
Function to increment recovery count on blackboard if this node wraps a recovery.