Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
wait_action.cpp
1 // Copyright (c) 2018 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 <string>
16 #include <memory>
17 #include <cmath>
18 
19 #include "nav2_behavior_tree/plugins/action/wait_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::Wait>(xml_tag_name, action_name, conf)
29 {
30 }
31 
33 {
34  double duration;
35  getInput("wait_duration", duration);
36  if (duration <= 0) {
37  RCLCPP_WARN(
38  node_->get_logger(), "Wait duration is negative or zero "
39  "(%f). Setting to positive.", duration);
40  duration *= -1;
41  }
42 
43  goal_.time = rclcpp::Duration::from_seconds(duration);
44 }
45 
47 {
48  if (!BT::isStatusActive(status())) {
49  initialize();
50  }
51 
53 }
54 
55 BT::NodeStatus WaitAction::on_success()
56 {
57  setOutput("error_code_id", ActionResult::NONE);
58  setOutput("error_msg", "");
59  return BT::NodeStatus::SUCCESS;
60 }
61 
62 BT::NodeStatus WaitAction::on_aborted()
63 {
64  setOutput("error_code_id", result_.result->error_code);
65  setOutput("error_msg", result_.result->error_msg);
66  return BT::NodeStatus::FAILURE;
67 }
68 
69 BT::NodeStatus WaitAction::on_cancelled()
70 {
71  setOutput("error_code_id", ActionResult::NONE);
72  setOutput("error_msg", "");
73  return BT::NodeStatus::SUCCESS;
74 }
75 
77 {
78  setOutput("error_code_id", ActionResult::TIMEOUT);
79  setOutput("error_msg", "Behavior Tree action client timed out waiting.");
80 }
81 
82 } // namespace nav2_behavior_tree
83 
84 #include "behaviortree_cpp/bt_factory.h"
85 BT_REGISTER_NODES(factory)
86 {
87  BT::NodeBuilder builder =
88  [](const std::string & name, const BT::NodeConfiguration & config)
89  {
90  return std::make_unique<nav2_behavior_tree::WaitAction>(name, "wait", config);
91  };
92 
93  factory.registerBuilder<nav2_behavior_tree::WaitAction>("Wait", builder);
94 }
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.
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::Wait.
Definition: wait_action.hpp:33
WaitAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::WaitAction.
Definition: wait_action.cpp:24
BT::NodeStatus on_aborted() override
Function to perform some user-defined operation upon abortion of the action.
Definition: wait_action.cpp:62
void on_tick() override
Function to perform some user-defined operation on tick.
Definition: wait_action.cpp:46
BT::NodeStatus on_success() override
Function to perform some user-defined operation upon successful completion of the action.
Definition: wait_action.cpp:55
void initialize()
Function to read parameters and initialize class variables.
Definition: wait_action.cpp:32
void on_timeout() override
Function to perform work in a BT Node when the action server times out Such as setting the error code...
Definition: wait_action.cpp:76
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.
Definition: wait_action.cpp:69