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"
25 #include "nav2_behavior_tree/plugins/action/truncate_path_action.hpp"
27 namespace nav2_behavior_tree
31 const std::string & name,
32 const BT::NodeConfiguration & conf)
33 : BT::ActionNodeBase(name, conf),
36 getInput(
"distance", distance_);
39 inline BT::NodeStatus TruncatePath::tick()
41 setStatus(BT::NodeStatus::RUNNING);
43 nav_msgs::msg::Path input_path;
45 getInput(
"input_path", input_path);
47 if (input_path.poses.empty()) {
48 setOutput(
"output_path", input_path);
49 return BT::NodeStatus::SUCCESS;
52 geometry_msgs::msg::PoseStamped final_pose = input_path.poses.back();
54 double distance_to_goal = nav2_util::geometry_utils::euclidean_distance(
55 input_path.poses.back(), final_pose);
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);
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;
66 double final_angle = atan2(dy, dx);
68 if (std::isnan(final_angle) || std::isinf(final_angle)) {
70 config().blackboard->get<rclcpp::Node::SharedPtr>(
"node")->get_logger(),
71 "Final angle is not valid while truncating path. Setting to 0.0");
75 input_path.poses.back().pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
78 setOutput(
"output_path", input_path);
80 return BT::NodeStatus::SUCCESS;
85 #include "behaviortree_cpp_v3/bt_factory.h"
86 BT_REGISTER_NODES(factory)
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.