Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
path_align.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/path_align.hpp"
36 #include <vector>
37 #include <string>
38 #include "dwb_critics/alignment_util.hpp"
39 #include "pluginlib/class_list_macros.hpp"
40 #include "nav_2d_utils/parameters.hpp"
41 
42 namespace dwb_critics
43 {
44 
45 void PathAlignCritic::onInit()
46 {
47  PathDistCritic::onInit();
48  stop_on_failure_ = false;
49 
50  auto node = node_.lock();
51  if (!node) {
52  throw std::runtime_error{"Failed to lock node"};
53  }
54 
55  forward_point_distance_ = nav_2d_utils::searchAndGetParam(
56  node,
57  dwb_plugin_name_ + "." + name_ + ".forward_point_distance", 0.325);
58 }
59 
61  const geometry_msgs::msg::Pose2D & pose, const nav_2d_msgs::msg::Twist2D & vel,
62  const geometry_msgs::msg::Pose2D & goal,
63  const nav_2d_msgs::msg::Path2D & global_plan)
64 {
65  double dx = pose.x - goal.x;
66  double dy = pose.y - goal.y;
67  double sq_dist = dx * dx + dy * dy;
68  if (sq_dist > forward_point_distance_ * forward_point_distance_) {
69  zero_scale_ = false;
70  } else {
71  // once we are close to goal, trying to keep the nose close to anything destabilizes behavior.
72  zero_scale_ = true;
73  return true;
74  }
75 
76  return PathDistCritic::prepare(pose, vel, goal, global_plan);
77 }
78 
79 double PathAlignCritic::getScale() const
80 {
81  if (zero_scale_) {
82  return 0.0;
83  } else {
84  return costmap_->getResolution() * 0.5 * scale_;
85  }
86 }
87 
88 double PathAlignCritic::scorePose(const geometry_msgs::msg::Pose2D & pose)
89 {
90  return PathDistCritic::scorePose(getForwardPose(pose, forward_point_distance_));
91 }
92 
93 } // namespace dwb_critics
94 
Evaluates a Trajectory2D to produce a score.
virtual double scorePose(const geometry_msgs::msg::Pose2D &pose)
Retrieve the score for a single pose.
Definition: map_grid.cpp:158
Scores trajectories based on how far from the global path the front of the robot ends up.
Definition: path_align.hpp:57
bool prepare(const geometry_msgs::msg::Pose2D &pose, const nav_2d_msgs::msg::Twist2D &vel, const geometry_msgs::msg::Pose2D &goal, const nav_2d_msgs::msg::Path2D &global_plan) override
Prior to evaluating any trajectories, look at contextual information constant across all trajectories...
Definition: path_align.cpp:60
double scorePose(const geometry_msgs::msg::Pose2D &pose) override
Retrieve the score for a single pose.
Definition: path_align.cpp:88
bool prepare(const geometry_msgs::msg::Pose2D &pose, const nav_2d_msgs::msg::Twist2D &vel, const geometry_msgs::msg::Pose2D &goal, const nav_2d_msgs::msg::Path2D &global_plan) override
Prior to evaluating any trajectories, look at contextual information constant across all trajectories...
Definition: path_dist.cpp:43
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:531