23 #include "geometry_msgs/msg/pose_stamped.hpp"
25 #include "nav2_constrained_smoother/constrained_smoother.hpp"
26 #include "nav2_ros_common/node_utils.hpp"
27 #include "nav2_util/geometry_utils.hpp"
28 #include "nav2_core/smoother_exceptions.hpp"
30 #include "pluginlib/class_loader.hpp"
31 #include "pluginlib/class_list_macros.hpp"
33 #include "tf2/utils.hpp"
35 using nav2_util::geometry_utils::euclidean_distance;
38 namespace nav2_constrained_smoother
41 void ConstrainedSmoother::configure(
42 const nav2::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 std::lock_guard<nav2_costmap_2d::Costmap2D::mutex_t> lock(*(costmap->getMutex()));
137 if (!smoother_->smooth(path_world, start_dir, end_dir, costmap.get(), smoother_params_)) {
140 "%s: failed to smooth plan, Ceres could not find a usable solution to optimize.",
141 plugin_name_.c_str());
143 "Failed to smooth plan, Ceres could not find a usable solution");
147 geometry_msgs::msg::PoseStamped pose;
148 pose.header = path.poses.front().header;
150 path.poses.reserve(path_world.size());
151 for (
auto & pw : path_world) {
152 pose.pose.position.x = pw[0];
153 pose.pose.position.y = pw[1];
154 pose.pose.orientation.z = sin(pw[2] / 2);
155 pose.pose.orientation.w = cos(pw[2] / 2);
157 path.poses.push_back(pose);
166 PLUGINLIB_EXPORT_CLASS(
Regulated pure pursuit controller plugin.
smoother interface that acts as a virtual base class for all smoother plugins