23 #include "nav2_constrained_smoother/constrained_smoother.hpp"
24 #include "nav2_core/exceptions.hpp"
25 #include "nav2_util/node_utils.hpp"
26 #include "nav2_util/geometry_utils.hpp"
27 #include "nav2_costmap_2d/costmap_filters/filter_values.hpp"
29 #include "pluginlib/class_loader.hpp"
30 #include "pluginlib/class_list_macros.hpp"
32 #include "tf2/utils.h"
34 using nav2_util::declare_parameter_if_not_declared;
35 using nav2_util::geometry_utils::euclidean_distance;
38 namespace nav2_constrained_smoother
41 void ConstrainedSmoother::configure(
42 const rclcpp_lifecycle::LifecycleNode::WeakPtr & parent,
43 std::string name, std::shared_ptr<tf2_ros::Buffer> tf,
44 std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_sub,
45 std::shared_ptr<nav2_costmap_2d::FootprintSubscriber>)
47 auto node = parent.lock();
49 throw std::runtime_error(
"Unable to lock node!");
52 costmap_sub_ = costmap_sub;
55 logger_ = node->get_logger();
57 smoother_ = std::make_unique<nav2_constrained_smoother::Smoother>();
58 optimizer_params_.get(node.get(), name);
59 smoother_params_.get(node.get(), name);
60 smoother_->initialize(optimizer_params_);
63 void ConstrainedSmoother::cleanup()
67 "Cleaning up smoother: %s of type"
68 " nav2_constrained_smoother::ConstrainedSmoother",
69 plugin_name_.c_str());
72 void ConstrainedSmoother::activate()
76 "Activating smoother: %s of type "
77 "nav2_constrained_smoother::ConstrainedSmoother",
78 plugin_name_.c_str());
81 void ConstrainedSmoother::deactivate()
85 "Deactivating smoother: %s of type "
86 "nav2_constrained_smoother::ConstrainedSmoother",
87 plugin_name_.c_str());
90 bool ConstrainedSmoother::smooth(nav_msgs::msg::Path & path,
const rclcpp::Duration & max_time)
92 if (path.poses.size() < 2) {
97 std::vector<Eigen::Vector3d> path_world;
98 path_world.reserve(path.poses.size());
101 Eigen::Vector2d start_dir;
102 Eigen::Vector2d end_dir;
103 for (
size_t i = 0; i < path.poses.size(); i++) {
104 auto & pose = path.poses[i].pose;
105 double angle = tf2::getYaw(pose.orientation);
106 Eigen::Vector2d orientation(cos(angle), sin(angle));
107 if (i == path.poses.size() - 1) {
111 path_world.emplace_back(pose.position.x, pose.position.y, path_world.back()[2]);
112 end_dir = orientation;
114 auto & pos_next = path.poses[i + 1].pose.position;
115 Eigen::Vector2d mvmt(pos_next.x - pose.position.x, pos_next.y - pose.position.y);
118 bool reversing = smoother_params_.reversing_enabled && orientation.dot(mvmt) < 0;
121 path_world.emplace_back(pose.position.x, pose.position.y, reversing ? -1 : 1);
123 start_dir = orientation;
124 }
else if (i == 1 && !smoother_params_.keep_start_orientation) {
127 path_world[0][2] = path_world.back()[2];
132 smoother_params_.max_time = max_time.seconds();
135 auto costmap = costmap_sub_->getCostmap();
136 if (!smoother_->smooth(path_world, start_dir, end_dir, costmap.get(), smoother_params_)) {
139 "%s: failed to smooth plan, Ceres could not find a usable solution to optimize.",
140 plugin_name_.c_str());
142 "Failed to smooth plan, Ceres could not find a usable solution.");
146 geometry_msgs::msg::PoseStamped pose;
147 pose.header = path.poses.front().header;
149 path.poses.reserve(path_world.size());
150 for (
auto & pw : path_world) {
151 pose.pose.position.x = pw[0];
152 pose.pose.position.y = pw[1];
153 pose.pose.orientation.z = sin(pw[2] / 2);
154 pose.pose.orientation.w = cos(pw[2] / 2);
156 path.poses.push_back(pose);
165 PLUGINLIB_EXPORT_CLASS(
Regulated pure pursuit controller plugin.
smoother interface that acts as a virtual base class for all smoother plugins