Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
simple_progress_checker.cpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_controller/plugins/simple_progress_checker.hpp"
16 #include <cmath>
17 #include <string>
18 #include <memory>
19 #include <vector>
20 #include "geometry_msgs/msg/pose_stamped.hpp"
21 #include "geometry_msgs/msg/pose.hpp"
22 #include "nav2_ros_common/node_utils.hpp"
23 #include "pluginlib/class_list_macros.hpp"
24 
25 using rcl_interfaces::msg::ParameterType;
26 using std::placeholders::_1;
27 
28 namespace nav2_controller
29 {
31 {
32  auto node = node_.lock();
33  if (post_set_params_handler_ && node) {
34  node->remove_post_set_parameters_callback(post_set_params_handler_.get());
35  }
36  post_set_params_handler_.reset();
37  if (on_set_params_handler_ && node) {
38  node->remove_on_set_parameters_callback(on_set_params_handler_.get());
39  }
40  on_set_params_handler_.reset();
41 }
42 
44  const nav2::LifecycleNode::WeakPtr & parent,
45  const std::string & plugin_name)
46 {
47  plugin_name_ = plugin_name;
48  node_ = parent;
49  auto node = node_.lock();
50 
51  clock_ = node->get_clock();
52  logger_ = node->get_logger();
53 
54  radius_ = node->declare_or_get_parameter(plugin_name + ".required_movement_radius", 0.5);
55  double time_allowance_param = node->declare_or_get_parameter(
56  plugin_name + ".movement_time_allowance", 10.0);
57  time_allowance_ = rclcpp::Duration::from_seconds(time_allowance_param);
58 
59  // Add callback for dynamic parameter updates
60  post_set_params_handler_ = node->add_post_set_parameters_callback(
61  std::bind(
63  this, std::placeholders::_1));
64  on_set_params_handler_ = node->add_on_set_parameters_callback(
65  std::bind(
67  this, std::placeholders::_1));
68 }
69 
70 bool SimpleProgressChecker::check(geometry_msgs::msg::PoseStamped & current_pose)
71 {
72  std::lock_guard<std::mutex> lock_reinit(mutex_);
73  // relies on short circuit evaluation to not call is_robot_moved_enough if
74  // baseline_pose is not set.
75  if ((!baseline_pose_set_) || (isRobotMovedEnough(current_pose.pose))) {
76  resetBaselinePose(current_pose.pose);
77  return true;
78  }
79  return !((clock_->now() - baseline_time_) > time_allowance_);
80 }
81 
83 {
84  baseline_pose_set_ = false;
85 }
86 
87 void SimpleProgressChecker::resetBaselinePose(const geometry_msgs::msg::Pose & pose)
88 {
89  baseline_pose_ = pose;
90  baseline_time_ = clock_->now();
91  baseline_pose_set_ = true;
92 }
93 
94 bool SimpleProgressChecker::isRobotMovedEnough(const geometry_msgs::msg::Pose & pose)
95 {
96  return pose_distance(pose, baseline_pose_) > radius_;
97 }
98 
100  const geometry_msgs::msg::Pose & pose1,
101  const geometry_msgs::msg::Pose & pose2)
102 {
103  double dx = pose1.position.x - pose2.position.x;
104  double dy = pose1.position.y - pose2.position.y;
105 
106  return std::hypot(dx, dy);
107 }
108 
109 rcl_interfaces::msg::SetParametersResult
111  const std::vector<rclcpp::Parameter> & parameters)
112 {
113  rcl_interfaces::msg::SetParametersResult result;
114  result.successful = true;
115  for (const auto & parameter : parameters) {
116  const auto & param_type = parameter.get_type();
117  const auto & param_name = parameter.get_name();
118  if (param_name.find(plugin_name_ + ".") != 0) {
119  continue;
120  }
121  if (param_type == ParameterType::PARAMETER_DOUBLE) {
122  if (parameter.as_double() < 0.0) {
123  RCLCPP_WARN(
124  logger_, "The value of parameter '%s' is incorrectly set to %f, "
125  "it should be >=0. Ignoring parameter update.",
126  param_name.c_str(), parameter.as_double());
127  result.successful = false;
128  }
129  }
130  }
131  return result;
132 }
133 
134 void
136  const std::vector<rclcpp::Parameter> & parameters)
137 {
138  std::lock_guard<std::mutex> lock_reinit(mutex_);
139  for (const auto & parameter : parameters) {
140  const auto & param_type = parameter.get_type();
141  const auto & param_name = parameter.get_name();
142  if (param_name.find(plugin_name_ + ".") != 0) {
143  continue;
144  }
145  if (param_type == ParameterType::PARAMETER_DOUBLE) {
146  if (param_name == plugin_name_ + ".required_movement_radius") {
147  radius_ = parameter.as_double();
148  } else if (param_name == plugin_name_ + ".movement_time_allowance") {
149  time_allowance_ = rclcpp::Duration::from_seconds(parameter.as_double());
150  }
151  }
152  }
153 }
154 
155 } // namespace nav2_controller
156 
This plugin is used to check the position of the robot to make sure that it is actually progressing t...
bool check(geometry_msgs::msg::PoseStamped &current_pose) override
Checks if the robot has moved compare to previous.
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
static double pose_distance(const geometry_msgs::msg::Pose &, const geometry_msgs::msg::Pose &)
Calculates distance between two poses.
void resetBaselinePose(const geometry_msgs::msg::Pose &pose)
Resets baseline pose with the current pose of the robot.
void initialize(const nav2::LifecycleNode::WeakPtr &parent, const std::string &plugin_name) override
Initialize the goal checker.
bool isRobotMovedEnough(const geometry_msgs::msg::Pose &pose)
Calculates robots movement from baseline pose.
void reset() override
Reset the progress checker state.
~SimpleProgressChecker()
Destroy the Simple Progress Checker object.
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
This class defines the plugin interface used to check the position of the robot to make sure that it ...