Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
truncate_path_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_v3/decorator_node.h"
24 
25 #include "nav2_behavior_tree/plugins/action/truncate_path_action.hpp"
26 
27 namespace nav2_behavior_tree
28 {
29 
31  const std::string & name,
32  const BT::NodeConfiguration & conf)
33 : BT::ActionNodeBase(name, conf),
34  distance_(1.0)
35 {
36  getInput("distance", distance_);
37 }
38 
39 inline BT::NodeStatus TruncatePath::tick()
40 {
41  setStatus(BT::NodeStatus::RUNNING);
42 
43  nav_msgs::msg::Path input_path;
44 
45  getInput("input_path", input_path);
46 
47  if (input_path.poses.empty()) {
48  setOutput("output_path", input_path);
49  return BT::NodeStatus::SUCCESS;
50  }
51 
52  geometry_msgs::msg::PoseStamped final_pose = input_path.poses.back();
53 
54  double distance_to_goal = nav2_util::geometry_utils::euclidean_distance(
55  input_path.poses.back(), final_pose);
56 
57  while (distance_to_goal < distance_ && input_path.poses.size() > 2) {
58  input_path.poses.pop_back();
59  distance_to_goal = nav2_util::geometry_utils::euclidean_distance(
60  input_path.poses.back(), final_pose);
61  }
62 
63  double dx = final_pose.pose.position.x - input_path.poses.back().pose.position.x;
64  double dy = final_pose.pose.position.y - input_path.poses.back().pose.position.y;
65 
66  double final_angle = atan2(dy, dx);
67 
68  if (std::isnan(final_angle) || std::isinf(final_angle)) {
69  RCLCPP_WARN(
70  config().blackboard->get<rclcpp::Node::SharedPtr>("node")->get_logger(),
71  "Final angle is not valid while truncating path. Setting to 0.0");
72  final_angle = 0.0;
73  }
74 
75  input_path.poses.back().pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
76  final_angle);
77 
78  setOutput("output_path", input_path);
79 
80  return BT::NodeStatus::SUCCESS;
81 }
82 
83 } // namespace nav2_behavior_tree
84 
85 #include "behaviortree_cpp_v3/bt_factory.h"
86 BT_REGISTER_NODES(factory)
87 {
88  factory.registerNodeType<nav2_behavior_tree::TruncatePath>("TruncatePath");
89 }
A BT::ActionNodeBase to shorten path by some distance.
TruncatePath(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A nav2_behavior_tree::TruncatePath constructor.