Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
rotate_to_goal.cpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "dwb_critics/rotate_to_goal.hpp"
36 #include <string>
37 #include <vector>
38 #include "dwb_core/exceptions.hpp"
39 #include "pluginlib/class_list_macros.hpp"
40 #include "dwb_core/trajectory_utils.hpp"
41 #include "nav2_util/geometry_utils.hpp"
42 #include "angles/angles.h"
43 
45 
46 namespace dwb_critics
47 {
48 
49 inline double hypot_sq(double dx, double dy)
50 {
51  return dx * dx + dy * dy;
52 }
53 
54 void RotateToGoalCritic::onInit()
55 {
56  auto node = node_.lock();
57  if (!node) {
58  throw std::runtime_error{"Failed to lock node"};
59  }
60 
61  xy_goal_tolerance_ = node->declare_or_get_parameter(
62  dwb_plugin_name_ + ".xy_goal_tolerance", 0.25);
63  xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_;
64  path_length_tolerance_ = node->declare_or_get_parameter(
65  dwb_plugin_name_ + "path_length_tolerance", 1.0);
66  double stopped_xy_velocity = node->declare_or_get_parameter(
67  dwb_plugin_name_ + ".trans_stopped_velocity", 0.25);
68  stopped_xy_velocity_sq_ = stopped_xy_velocity * stopped_xy_velocity;
69  slowing_factor_ = node->declare_or_get_parameter(
70  dwb_plugin_name_ + "." + name_ + ".slowing_factor", 5.0);
71  lookahead_time_ = node->declare_or_get_parameter(
72  dwb_plugin_name_ + "." + name_ + ".lookahead_time", -1.0);
73  reset();
74 }
75 
77 {
78  in_window_ = false;
79  rotating_ = false;
80 }
81 
83  const geometry_msgs::msg::Pose & pose, const nav_2d_msgs::msg::Twist2D & vel,
84  const geometry_msgs::msg::Pose & goal,
85  const nav_msgs::msg::Path & transformed_global_plan)
86 {
87  double dxy_sq = hypot_sq(pose.position.x - goal.position.x, pose.position.y - goal.position.y);
88  double path_length = nav2_util::geometry_utils::calculate_path_length(transformed_global_plan);
89  in_window_ = in_window_ ||
90  (dxy_sq <= xy_goal_tolerance_sq_ && path_length <= path_length_tolerance_);
91  current_xy_speed_sq_ = hypot_sq(vel.x, vel.y);
92  rotating_ = rotating_ || (in_window_ && current_xy_speed_sq_ <= stopped_xy_velocity_sq_);
93  goal_yaw_ = tf2::getYaw(goal.orientation);
94  return true;
95 }
96 
97 double RotateToGoalCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj)
98 {
99  // If we're not sufficiently close to the goal, we don't care what the twist is
100  if (!in_window_) {
101  return 0.0;
102  } else if (!rotating_) {
103  double speed_sq = hypot_sq(traj.velocity.x, traj.velocity.y);
104  if (speed_sq >= current_xy_speed_sq_) {
105  throw dwb_core::IllegalTrajectoryException(name_, "Not slowing down near goal.");
106  }
107  return speed_sq * slowing_factor_ + scoreRotation(traj);
108  }
109 
110  // If we're sufficiently close to the goal, any transforming velocity is invalid
111  if (fabs(traj.velocity.x) > 0 || fabs(traj.velocity.y) > 0) {
112  throw dwb_core::
113  IllegalTrajectoryException(name_, "Nonrotation command near goal.");
114  }
115 
116  return scoreRotation(traj);
117 }
118 
119 double RotateToGoalCritic::scoreRotation(const dwb_msgs::msg::Trajectory2D & traj)
120 {
121  if (traj.poses.empty()) {
122  throw dwb_core::IllegalTrajectoryException(name_, "Empty trajectory.");
123  }
124 
125  double end_yaw;
126  if (lookahead_time_ >= 0.0) {
127  geometry_msgs::msg::Pose eval_pose = dwb_core::projectPose(traj, lookahead_time_);
128  end_yaw = tf2::getYaw(eval_pose.orientation);
129  } else {
130  end_yaw = tf2::getYaw(traj.poses.back().orientation);
131  }
132  return fabs(angles::shortest_angular_distance(end_yaw, goal_yaw_));
133 }
134 
135 } // namespace dwb_critics
Thrown when one of the critics encountered a fatal error.
Definition: exceptions.hpp:50
Evaluates a Trajectory2D to produce a score.
Forces the commanded trajectories to only be rotations if within a certain distance window.
bool prepare(const geometry_msgs::msg::Pose &pose, const nav_2d_msgs::msg::Twist2D &vel, const geometry_msgs::msg::Pose &goal, const nav_msgs::msg::Path &global_plan) override
Prior to evaluating any trajectories, look at contextual information constant across all trajectories...
void reset() override
Reset the state of the critic.
virtual double scoreRotation(const dwb_msgs::msg::Trajectory2D &traj)
Assuming that this is an actual rotation when near the goal, score the trajectory.
double scoreTrajectory(const dwb_msgs::msg::Trajectory2D &traj) override
Return a raw score for the given trajectory.