Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
stopped_goal_checker.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 <cmath>
36 #include <string>
37 #include <memory>
38 #include <limits>
39 #include <vector>
40 #include "nav2_controller/plugins/stopped_goal_checker.hpp"
41 #include "pluginlib/class_list_macros.hpp"
42 #include "nav2_ros_common/node_utils.hpp"
43 
44 using std::hypot;
45 using std::fabs;
46 
47 using rcl_interfaces::msg::ParameterType;
48 using std::placeholders::_1;
49 
50 namespace nav2_controller
51 {
52 
53 StoppedGoalChecker::StoppedGoalChecker()
54 : SimpleGoalChecker(), rot_stopped_velocity_(0.25), trans_stopped_velocity_(0.25)
55 {
56 }
57 
58 void StoppedGoalChecker::initialize(
59  const nav2::LifecycleNode::WeakPtr & parent,
60  const std::string & plugin_name,
61  const std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros)
62 {
63  plugin_name_ = plugin_name;
64  SimpleGoalChecker::initialize(parent, plugin_name, costmap_ros);
65 
66  auto node = parent.lock();
67 
68  nav2::declare_parameter_if_not_declared(
69  node,
70  plugin_name + ".rot_stopped_velocity", rclcpp::ParameterValue(0.25));
71  nav2::declare_parameter_if_not_declared(
72  node,
73  plugin_name + ".trans_stopped_velocity", rclcpp::ParameterValue(0.25));
74 
75  node->get_parameter(plugin_name + ".rot_stopped_velocity", rot_stopped_velocity_);
76  node->get_parameter(plugin_name + ".trans_stopped_velocity", trans_stopped_velocity_);
77 
78  // Add callback for dynamic parameters
79  dyn_params_handler_ = node->add_on_set_parameters_callback(
80  std::bind(&StoppedGoalChecker::dynamicParametersCallback, this, _1));
81 }
82 
83 bool StoppedGoalChecker::isGoalReached(
84  const geometry_msgs::msg::Pose & query_pose, const geometry_msgs::msg::Pose & goal_pose,
85  const geometry_msgs::msg::Twist & velocity)
86 {
87  bool ret = SimpleGoalChecker::isGoalReached(query_pose, goal_pose, velocity);
88  if (!ret) {
89  return ret;
90  }
91 
92  return fabs(velocity.angular.z) <= rot_stopped_velocity_ &&
93  hypot(velocity.linear.x, velocity.linear.y) <= trans_stopped_velocity_;
94 }
95 
96 bool StoppedGoalChecker::getTolerances(
97  geometry_msgs::msg::Pose & pose_tolerance,
98  geometry_msgs::msg::Twist & vel_tolerance)
99 {
100  double invalid_field = std::numeric_limits<double>::lowest();
101 
102  // populate the poses
103  bool rtn = SimpleGoalChecker::getTolerances(pose_tolerance, vel_tolerance);
104 
105  // override the velocities
106  vel_tolerance.linear.x = trans_stopped_velocity_;
107  vel_tolerance.linear.y = trans_stopped_velocity_;
108  vel_tolerance.linear.z = invalid_field;
109 
110  vel_tolerance.angular.x = invalid_field;
111  vel_tolerance.angular.y = invalid_field;
112  vel_tolerance.angular.z = rot_stopped_velocity_;
113 
114  return true && rtn;
115 }
116 
117 rcl_interfaces::msg::SetParametersResult
118 StoppedGoalChecker::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
119 {
120  rcl_interfaces::msg::SetParametersResult result;
121  for (auto parameter : parameters) {
122  const auto & param_type = parameter.get_type();
123  const auto & param_name = parameter.get_name();
124  if (param_name.find(plugin_name_ + ".") != 0) {
125  continue;
126  }
127 
128  if (param_type == ParameterType::PARAMETER_DOUBLE) {
129  if (param_name == plugin_name_ + ".rot_stopped_velocity") {
130  rot_stopped_velocity_ = parameter.as_double();
131  } else if (param_name == plugin_name_ + ".trans_stopped_velocity") {
132  trans_stopped_velocity_ = parameter.as_double();
133  }
134  }
135  }
136  result.successful = true;
137  return result;
138 }
139 
140 } // namespace nav2_controller
141 
Goal Checker plugin that checks the position difference and velocity.
Function-object for checking whether a goal has been reached.