Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
drive_on_heading_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/drive_on_heading_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::DriveOnHeading>(xml_tag_name, action_name, conf),
28  initalized_(false)
29 {
30 }
31 
33 {
34  double dist;
35  getInput("dist_to_travel", dist);
36  double speed;
37  getInput("speed", speed);
38  double time_allowance;
39  getInput("time_allowance", time_allowance);
40 
41  // Populate the input message
42  goal_.target.x = dist;
43  goal_.target.y = 0.0;
44  goal_.target.z = 0.0;
45  goal_.speed = speed;
46  goal_.time_allowance = rclcpp::Duration::from_seconds(time_allowance);
47  initalized_ = true;
48 }
49 
51 {
52  if (!initalized_) {
53  initialize();
54  }
55 }
56 
58 {
59  setOutput("error_code_id", ActionResult::NONE);
60  return BT::NodeStatus::SUCCESS;
61 }
62 
64 {
65  setOutput("error_code_id", result_.result->error_code);
66  return BT::NodeStatus::FAILURE;
67 }
68 
70 {
71  setOutput("error_code_id", ActionResult::NONE);
72  return BT::NodeStatus::SUCCESS;
73 }
74 
75 } // namespace nav2_behavior_tree
76 
77 #include "behaviortree_cpp/bt_factory.h"
78 BT_REGISTER_NODES(factory)
79 {
80  BT::NodeBuilder builder =
81  [](const std::string & name, const BT::NodeConfiguration & config)
82  {
83  return std::make_unique<nav2_behavior_tree::DriveOnHeadingAction>(
84  name, "drive_on_heading", config);
85  };
86 
87  factory.registerBuilder<nav2_behavior_tree::DriveOnHeadingAction>("DriveOnHeading", builder);
88 }
Abstract class representing an action based BT node.
A nav2_behavior_tree::BtActionNode class that wraps nav2_msgs::action::DriveOnHeading.
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.
DriveOnHeadingAction(const std::string &xml_tag_name, const std::string &action_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::DriveOnHeadingAction.
void on_tick() override
Function to perform some user-defined operation on tick.
void initialize()
Function to read parameters and initialize class variables.
BT::NodeStatus on_cancelled() override
Function to perform some user-defined operation upon cancellation of the action.