18 #include "angles/angles.h"
19 #include "nav2_core/controller_exceptions.hpp"
20 #include "nav2_util/geometry_utils.hpp"
21 #include "nav2_util/controller_utils.hpp"
22 #include "nav2_graceful_controller/graceful_controller.hpp"
23 #include "nav2_costmap_2d/costmap_filters/filter_values.hpp"
25 namespace nav2_graceful_controller
29 const nav2::LifecycleNode::WeakPtr & parent,
30 std::string name,
const std::shared_ptr<tf2_ros::Buffer> tf,
31 const std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros)
33 nav2::LifecycleNode::SharedPtr node = parent.lock();
38 costmap_ros_ = costmap_ros;
41 logger_ = node->get_logger();
45 param_handler_ = std::make_unique<ParameterHandler>(
46 node, plugin_name_, logger_,
47 costmap_ros_->getCostmap()->getSizeInMetersX());
48 params_ = param_handler_->getParams();
51 path_handler_ = std::make_unique<PathHandler>(
52 params_->transform_tolerance, tf_buffer_, costmap_ros_);
55 control_law_ = std::make_unique<SmoothControlLaw>(
56 params_->k_phi, params_->k_delta, params_->beta, params_->lambda, params_->slowdown_radius,
57 params_->v_linear_min, params_->v_linear_max, params_->v_angular_max);
60 if(params_->use_collision_detection) {
61 collision_checker_ = std::make_unique<nav2_costmap_2d::
62 FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>>(costmap_ros_->getCostmap());
66 transformed_plan_pub_ = node->create_publisher<nav_msgs::msg::Path>(
"transformed_global_plan");
67 local_plan_pub_ = node->create_publisher<nav_msgs::msg::Path>(
"local_plan");
68 motion_target_pub_ = node->create_publisher<geometry_msgs::msg::PoseStamped>(
"motion_target");
69 slowdown_pub_ = node->create_publisher<visualization_msgs::msg::Marker>(
"slowdown");
71 RCLCPP_INFO(logger_,
"Configured Graceful Motion Controller: %s", plugin_name_.c_str());
78 "Cleaning up controller: %s of type graceful_controller::GracefulController",
79 plugin_name_.c_str());
80 transformed_plan_pub_.reset();
81 local_plan_pub_.reset();
82 motion_target_pub_.reset();
83 slowdown_pub_.reset();
84 collision_checker_.reset();
85 path_handler_.reset();
86 param_handler_.reset();
94 "Activating controller: %s of type nav2_graceful_controller::GracefulController",
95 plugin_name_.c_str());
96 transformed_plan_pub_->on_activate();
97 local_plan_pub_->on_activate();
98 motion_target_pub_->on_activate();
99 slowdown_pub_->on_activate();
106 "Deactivating controller: %s of type nav2_graceful_controller::GracefulController",
107 plugin_name_.c_str());
108 transformed_plan_pub_->on_deactivate();
109 local_plan_pub_->on_deactivate();
110 motion_target_pub_->on_deactivate();
111 slowdown_pub_->on_deactivate();
115 const geometry_msgs::msg::PoseStamped & pose,
116 const geometry_msgs::msg::Twist & ,
119 std::lock_guard<std::mutex> param_lock(param_handler_->getMutex());
121 geometry_msgs::msg::TwistStamped cmd_vel;
122 cmd_vel.header = pose.header;
125 geometry_msgs::msg::Pose pose_tolerance;
126 geometry_msgs::msg::Twist velocity_tolerance;
127 if (!goal_checker->
getTolerances(pose_tolerance, velocity_tolerance)) {
128 RCLCPP_WARN(logger_,
"Unable to retrieve goal checker's tolerances!");
130 goal_dist_tolerance_ = pose_tolerance.position.x;
134 control_law_->setCurvatureConstants(
135 params_->k_phi, params_->k_delta, params_->beta, params_->lambda);
136 control_law_->setSlowdownRadius(params_->slowdown_radius);
137 control_law_->setSpeedLimit(params_->v_linear_min, params_->v_linear_max, params_->v_angular_max);
140 auto transformed_plan = path_handler_->transformGlobalPlan(
141 pose, params_->max_robot_pose_search_dist);
147 transformed_plan_pub_->publish(transformed_plan);
150 geometry_msgs::msg::TransformStamped costmap_transform;
152 costmap_transform = tf_buffer_->lookupTransform(
153 costmap_ros_->getGlobalFrameID(), costmap_ros_->getBaseFrameID(),
155 }
catch (tf2::TransformException & ex) {
157 logger_,
"Could not transform %s to %s: %s",
158 costmap_ros_->getBaseFrameID().c_str(), costmap_ros_->getGlobalFrameID().c_str(),
164 double dist_to_goal = nav2_util::geometry_utils::calculate_path_length(transformed_plan);
167 if (dist_to_goal < goal_dist_tolerance_ || goal_reached_) {
168 goal_reached_ =
true;
169 double angle_to_goal = tf2::getYaw(transformed_plan.poses.back().pose.orientation);
171 size_t num_steps = fabs(angle_to_goal) / params_->in_place_collision_resolution;
173 num_steps = std::max(
static_cast<size_t>(1), num_steps);
174 bool collision_free =
true;
175 for (
size_t i = 1; i <= num_steps; ++i) {
176 double step =
static_cast<double>(i) /
static_cast<double>(num_steps);
177 double yaw = step * angle_to_goal;
178 geometry_msgs::msg::PoseStamped next_pose;
179 next_pose.header.frame_id = costmap_ros_->getBaseFrameID();
180 next_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(yaw);
181 geometry_msgs::msg::PoseStamped costmap_pose;
182 tf2::doTransform(next_pose, costmap_pose, costmap_transform);
183 if (params_->use_collision_detection &&
inCollision(
184 costmap_pose.pose.position.x, costmap_pose.pose.position.y,
185 tf2::getYaw(costmap_pose.pose.orientation)))
187 collision_free =
false;
192 if (collision_free) {
200 nav_msgs::msg::Path local_plan;
201 geometry_msgs::msg::PoseStamped target_pose;
203 double dist_to_target;
204 std::vector<double> target_distances;
207 bool is_first_iteration =
true;
208 for (
int i = transformed_plan.poses.size() - 1; i >= 0; --i) {
209 if (is_first_iteration) {
212 dist_to_target = params_->max_lookahead;
216 target_pose = nav2_util::getLookAheadPoint(dist_to_target, transformed_plan,
false);
217 is_first_iteration =
false;
223 dist_to_target = target_distances[i];
224 target_pose = transformed_plan.poses[i];
229 target_pose, dist_to_target, dist_to_goal, local_plan, costmap_transform, cmd_vel))
232 motion_target_pub_->publish(target_pose);
234 auto slowdown_marker = nav2_graceful_controller::createSlowdownMarker(
235 target_pose, params_->slowdown_radius);
236 slowdown_pub_->publish(slowdown_marker);
238 local_plan.header = transformed_plan.header;
239 local_plan_pub_->publish(local_plan);
250 path_handler_->setPlan(path);
251 goal_reached_ =
false;
252 do_initial_rotation_ =
true;
256 const double & speed_limit,
const bool & percentage)
258 std::lock_guard<std::mutex> param_lock(param_handler_->getMutex());
260 if (speed_limit == nav2_costmap_2d::NO_SPEED_LIMIT) {
261 params_->v_linear_max = params_->v_linear_max_initial;
262 params_->v_angular_max = params_->v_angular_max_initial;
266 params_->v_linear_max = std::max(
267 params_->v_linear_max_initial * speed_limit / 100.0, params_->v_linear_min);
268 params_->v_angular_max = params_->v_angular_max_initial * speed_limit / 100.0;
271 params_->v_linear_max = std::max(speed_limit, params_->v_linear_min);
273 params_->v_angular_max = params_->v_angular_max_initial *
274 speed_limit / params_->v_linear_max_initial;
280 geometry_msgs::msg::PoseStamped & target_pose,
281 double dist_to_target,
283 nav_msgs::msg::Path & trajectory,
284 geometry_msgs::msg::TransformStamped & costmap_transform,
285 geometry_msgs::msg::TwistStamped & cmd_vel)
288 if (dist_to_target > params_->max_lookahead) {
292 if (dist_to_goal < params_->max_lookahead) {
293 if (params_->prefer_final_rotation) {
296 double yaw = std::atan2(target_pose.pose.position.y, target_pose.pose.position.x);
297 target_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(yaw);
299 }
else if (dist_to_target < params_->min_lookahead) {
305 bool reversing =
false;
306 if (params_->allow_backward && target_pose.pose.position.x < 0.0) {
308 target_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
309 tf2::getYaw(target_pose.pose.orientation) + M_PI);
313 if (
simulateTrajectory(target_pose, costmap_transform, trajectory, cmd_vel, reversing)) {
323 const geometry_msgs::msg::PoseStamped & motion_target,
324 const geometry_msgs::msg::TransformStamped & costmap_transform,
325 nav_msgs::msg::Path & trajectory,
326 geometry_msgs::msg::TwistStamped & cmd_vel,
329 trajectory.poses.clear();
332 geometry_msgs::msg::PoseStamped next_pose;
333 next_pose.header.frame_id = costmap_ros_->getBaseFrameID();
334 next_pose.pose.orientation.w = 1.0;
337 bool sim_initial_rotation = do_initial_rotation_ && params_->initial_rotation;
338 double angle_to_target =
339 std::atan2(motion_target.pose.position.y, motion_target.pose.position.x);
340 if (fabs(angle_to_target) < params_->initial_rotation_tolerance) {
341 sim_initial_rotation =
false;
342 do_initial_rotation_ =
false;
345 double distance = std::numeric_limits<double>::max();
346 double resolution_ = costmap_ros_->getCostmap()->getResolution();
347 double dt = (params_->v_linear_max > 0.0) ? resolution_ / params_->v_linear_max : 0.0;
350 unsigned int max_iter = 3 *
351 std::hypot(motion_target.pose.position.x, motion_target.pose.position.y) / resolution_;
355 if (sim_initial_rotation) {
357 double next_pose_yaw = tf2::getYaw(next_pose.pose.orientation);
361 if (trajectory.poses.empty()) {cmd_vel.twist = cmd;}
364 if (fabs(angle_to_target - next_pose_yaw) < params_->initial_rotation_tolerance) {
365 sim_initial_rotation =
false;
369 next_pose_yaw += cmd_vel.twist.angular.z * dt;
370 next_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(next_pose_yaw);
373 if (trajectory.poses.empty()) {
374 cmd_vel.twist = control_law_->calculateRegularVelocity(
375 motion_target.pose, next_pose.pose, backward);
379 next_pose.pose = control_law_->calculateNextPose(
380 dt, motion_target.pose, next_pose.pose, backward);
384 trajectory.poses.push_back(next_pose);
387 geometry_msgs::msg::PoseStamped global_pose;
388 tf2::doTransform(next_pose, global_pose, costmap_transform);
389 if (params_->use_collision_detection &&
inCollision(
390 global_pose.pose.position.x, global_pose.pose.position.y,
391 tf2::getYaw(global_pose.pose.orientation)))
397 distance = nav2_util::geometry_utils::euclidean_distance(motion_target.pose, next_pose.pose);
398 }
while(distance > resolution_ && trajectory.poses.size() < max_iter);
405 geometry_msgs::msg::Twist vel;
407 vel.angular.z = params_->rotation_scaling_factor * angle_to_target * params_->v_angular_max;
408 vel.angular.z = std::copysign(1.0, vel.angular.z) * std::max(abs(vel.angular.z),
409 params_->v_angular_min_in_place);
416 if (!costmap_ros_->getCostmap()->worldToMap(x, y, mx, my)) {
418 logger_,
"The path is not in the costmap. Cannot check for collisions. "
419 "Proceed at your own risk, slow the robot, or increase your costmap size.");
425 bool is_tracking_unknown =
426 costmap_ros_->getLayeredCostmap()->isTrackingUnknown();
427 bool consider_footprint = !costmap_ros_->getUseRadius();
429 double footprint_cost;
430 if (consider_footprint) {
431 footprint_cost = collision_checker_->footprintCostAtPose(
432 x, y, theta, costmap_ros_->getRobotFootprint());
434 footprint_cost = collision_checker_->pointCost(mx, my);
437 switch (
static_cast<unsigned char>(footprint_cost)) {
438 case (nav2_costmap_2d::LETHAL_OBSTACLE):
440 case (nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE):
441 return consider_footprint ? false :
true;
442 case (nav2_costmap_2d::NO_INFORMATION):
443 return is_tracking_unknown ? false :
true;
450 const std::vector<geometry_msgs::msg::PoseStamped> & poses,
451 std::vector<double> & distances)
453 distances.resize(poses.size());
455 double d = std::hypot(poses[0].pose.position.x, poses[0].pose.position.y);
458 for (
size_t i = 1; i < poses.size(); ++i) {
459 d += nav2_util::geometry_utils::euclidean_distance(poses[i - 1].pose, poses[i].pose);
465 std::vector<geometry_msgs::msg::PoseStamped> & path)
469 if (path.size() < 3) {
return;}
472 double initial_yaw = tf2::getYaw(path[1].pose.orientation);
473 for (
size_t i = 2; i < path.size() - 1; ++i) {
474 double this_yaw = tf2::getYaw(path[i].pose.orientation);
475 if (angles::shortest_angular_distance(this_yaw, initial_yaw) > 1e-6) {
return;}
480 for (
size_t i = 0; i < path.size() - 1; ++i) {
482 double dx = path[i + 1].pose.position.x - path[i].pose.position.x;
483 double dy = path[i + 1].pose.position.y - path[i].pose.position.y;
484 double yaw = std::atan2(dy, dx);
485 path[i].pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(yaw);
492 PLUGINLIB_EXPORT_CLASS(
controller interface that acts as a virtual base class for all controller plugins
Function-object for checking whether a goal has been reached.
virtual bool getTolerances(geometry_msgs::msg::Pose &pose_tolerance, geometry_msgs::msg::Twist &vel_tolerance)=0
Get the maximum possible tolerances used for goal checking in the major types. Any field without a va...
Graceful controller plugin.
void activate() override
Activate controller state machine.
void setPlan(const nav_msgs::msg::Path &path) override
nav2_core setPlan - Sets the global plan.
void computeDistanceAlongPath(const std::vector< geometry_msgs::msg::PoseStamped > &poses, std::vector< double > &distances)
Compute the distance to each pose in a path.
void deactivate() override
Deactivate controller state machine.
bool simulateTrajectory(const geometry_msgs::msg::PoseStamped &motion_target, const geometry_msgs::msg::TransformStamped &costmap_transform, nav_msgs::msg::Path &trajectory, geometry_msgs::msg::TwistStamped &cmd_vel, bool backward)
Simulate trajectory calculating in every step the new velocity command based on a new curvature value...
void cleanup() override
Cleanup controller state machine.
geometry_msgs::msg::Twist rotateToTarget(double angle_to_target)
Rotate the robot to face the motion target with maximum angular velocity.
void setSpeedLimit(const double &speed_limit, const bool &percentage) override
Limits the maximum linear speed of the robot.
void configure(const nav2::LifecycleNode::WeakPtr &parent, std::string name, std::shared_ptr< tf2_ros::Buffer > tf, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros) override
Configure controller state machine.
geometry_msgs::msg::TwistStamped computeVelocityCommands(const geometry_msgs::msg::PoseStamped &pose, const geometry_msgs::msg::Twist &velocity, nav2_core::GoalChecker *goal_checker) override
Compute the best command given the current pose and velocity.
bool inCollision(const double &x, const double &y, const double &theta)
Checks if the robot is in collision.
void validateOrientations(std::vector< geometry_msgs::msg::PoseStamped > &path)
Control law requires proper orientations, not all planners provide them.
bool validateTargetPose(geometry_msgs::msg::PoseStamped &target_pose, double dist_to_target, double dist_to_goal, nav_msgs::msg::Path &trajectory, geometry_msgs::msg::TransformStamped &costmap_transform, geometry_msgs::msg::TwistStamped &cmd_vel)
Validate a given target pose for calculating command velocity.