21 #include "behaviortree_cpp/decorator_node.h"
22 #include "geometry_msgs/msg/pose_stamped.hpp"
23 #include "nav2_util/geometry_utils.hpp"
24 #include "nav2_util/robot_utils.hpp"
25 #include "nav_msgs/msg/path.hpp"
26 #include "rclcpp/rclcpp.hpp"
27 #include "tf2/LinearMath/Quaternion.hpp"
28 #include "nav2_ros_common/tf2_factories.hpp"
30 #include "nav2_behavior_tree/plugins/action/truncate_path_local_action.hpp"
32 namespace nav2_behavior_tree
36 const std::string & name,
37 const BT::NodeConfiguration & conf)
38 : BT::ActionNodeBase(name, conf)
41 config().blackboard->template get<nav2::TransformBuffer::SharedPtr>(
45 inline BT::NodeStatus TruncatePathLocal::tick()
47 setStatus(BT::NodeStatus::RUNNING);
49 double distance_forward, distance_backward;
50 geometry_msgs::msg::PoseStamped pose;
51 double angular_distance_weight;
52 double max_robot_pose_search_dist;
54 getInput(
"distance_forward", distance_forward);
55 getInput(
"distance_backward", distance_backward);
56 getInput(
"angular_distance_weight", angular_distance_weight);
57 getInput(
"max_robot_pose_search_dist", max_robot_pose_search_dist);
59 bool path_pruning = std::isfinite(max_robot_pose_search_dist);
60 if (distance_forward < 0.0) {
61 distance_forward = std::numeric_limits<double>::max();
63 nav_msgs::msg::Path new_path;
64 getInput(
"input_path", new_path);
65 if (!path_pruning || new_path != path_) {
67 closest_pose_detection_begin_ = path_.poses.begin();
70 if (!getRobotPose(path_.header.frame_id, pose)) {
71 return BT::NodeStatus::FAILURE;
74 if (path_.poses.empty()) {
75 setOutput(
"output_path", path_);
76 return BT::NodeStatus::SUCCESS;
79 auto closest_pose_detection_end = path_.poses.end();
81 closest_pose_detection_end = nav2_util::geometry_utils::first_after_integrated_distance(
82 closest_pose_detection_begin_, path_.poses.end(), max_robot_pose_search_dist);
86 auto current_pose = nav2_util::geometry_utils::min_by(
87 closest_pose_detection_begin_, closest_pose_detection_end,
88 [&pose, angular_distance_weight](
const geometry_msgs::msg::PoseStamped & ps) {
89 return poseDistance(pose, ps, angular_distance_weight);
93 closest_pose_detection_begin_ = current_pose;
97 auto forward_pose_it = nav2_util::geometry_utils::first_after_integrated_distance(
98 current_pose, path_.poses.end(), distance_forward);
102 auto backward_pose_it = nav2_util::geometry_utils::first_after_integrated_distance(
103 std::reverse_iterator(current_pose + 1), path_.poses.rend(), distance_backward);
105 nav_msgs::msg::Path output_path;
106 output_path.header = path_.header;
107 output_path.poses = std::vector<geometry_msgs::msg::PoseStamped>(
108 backward_pose_it.base(), forward_pose_it);
109 setOutput(
"output_path", output_path);
111 return BT::NodeStatus::SUCCESS;
114 inline bool TruncatePathLocal::getRobotPose(
115 std::string path_frame_id, geometry_msgs::msg::PoseStamped & pose)
117 if (!getInput(
"pose", pose)) {
118 auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>(
"node");
119 std::string robot_frame = BT::deconflictPortAndParamFrame<std::string>(
120 node,
"robot_base_frame",
this);
121 if (robot_frame.empty()) {
124 "Neither pose nor robot_base_frame specified for %s", name().c_str());
127 double transform_tolerance;
128 getInput(
"transform_tolerance", transform_tolerance);
129 if (!nav2_util::getCurrentPose(
130 pose, *tf_buffer_, path_frame_id, robot_frame, transform_tolerance))
134 "Failed to lookup current robot pose for %s", name().c_str());
142 TruncatePathLocal::poseDistance(
143 const geometry_msgs::msg::PoseStamped & pose1,
144 const geometry_msgs::msg::PoseStamped & pose2,
145 const double angular_distance_weight)
147 double dx = pose1.pose.position.x - pose2.pose.position.x;
148 double dy = pose1.pose.position.y - pose2.pose.position.y;
152 tf2::convert(pose1.pose.orientation, q1);
154 tf2::convert(pose2.pose.orientation, q2);
155 double da = angular_distance_weight * std::abs(q1.angleShortestPath(q2));
156 return std::sqrt(dx * dx + dy * dy + da * da);
161 #include "behaviortree_cpp/bt_factory.h"
162 BT_REGISTER_NODES(factory) {
164 "TruncatePathLocal");
A BT::ActionNodeBase to shorten path to some distance around robot.
TruncatePathLocal(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A nav2_behavior_tree::TruncatePathLocal constructor.