Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
get_current_pose_action.cpp
1 // Copyright (c) 2018 Intel Corporation
2 // Copyright (c) 2020 Francisco Martin Rico
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <string>
17 #include <memory>
18 #include <limits>
19 
20 #include "nav_msgs/msg/path.hpp"
21 #include "geometry_msgs/msg/pose_stamped.hpp"
22 #include "nav2_util/geometry_utils.hpp"
23 #include "behaviortree_cpp/decorator_node.h"
24 #include "nav2_ros_common/tf2_factories.hpp"
25 
26 #include "nav2_behavior_tree/plugins/action/get_current_pose_action.hpp"
27 
28 namespace nav2_behavior_tree
29 {
30 
32  const std::string & name,
33  const BT::NodeConfiguration & conf)
34 : BT::ActionNodeBase(name, conf)
35 {
36  auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
37  clock_ = node->get_clock();
38  tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>("tf_buffer");
39  node->get_parameter("transform_tolerance", transform_tolerance_);
40  transform_staleness_threshold_ = node->declare_or_get_parameter(
41  "transform_staleness_threshold", 0.0);
42  global_frame_ = BT::deconflictPortAndParamFrame<std::string>(
43  node, "global_frame", this);
44  robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
45  node, "robot_base_frame", this);
46 }
47 
48 inline BT::NodeStatus GetCurrentPoseAction::tick()
49 {
50  setStatus(BT::NodeStatus::RUNNING);
51  geometry_msgs::msg::PoseStamped current_pose;
52 
53  if (!nav2_util::getFreshPose(
54  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
55  current_pose))
56  {
57  RCLCPP_WARN(
58  config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node")->get_logger(),
59  "Current robot pose is not available.");
60  return BT::NodeStatus::FAILURE;
61  }
62 
63  setOutput("current_pose", current_pose);
64  return BT::NodeStatus::SUCCESS;
65 }
66 
67 } // namespace nav2_behavior_tree
68 
69 #include "behaviortree_cpp/bt_factory.h"
70 BT_REGISTER_NODES(factory)
71 {
72  factory.registerNodeType<nav2_behavior_tree::GetCurrentPoseAction>("GetCurrentPose");
73 }
A BT::ActionNodeBase to shorten path by some distance.
GetCurrentPoseAction(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A nav2_behavior_tree::GetCurrentPoseAction constructor.