Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
oscillation.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/oscillation.hpp"
36 #include <chrono>
37 #include <cmath>
38 #include <string>
39 #include <vector>
40 #include "nav_2d_utils/parameters.hpp"
41 #include "nav2_util/node_utils.hpp"
42 #include "dwb_core/exceptions.hpp"
43 #include "pluginlib/class_list_macros.hpp"
44 
46 
47 namespace dwb_critics
48 {
49 
50 
51 OscillationCritic::CommandTrend::CommandTrend()
52 {
53  reset();
54 }
55 
56 void OscillationCritic::CommandTrend::reset()
57 {
58  sign_ = Sign::ZERO;
59  positive_only_ = false;
60  negative_only_ = false;
61 }
62 
63 bool OscillationCritic::CommandTrend::update(double velocity)
64 {
65  bool flag_set = false;
66  if (velocity < 0.0) {
67  if (sign_ == Sign::POSITIVE) {
68  negative_only_ = true;
69  flag_set = true;
70  }
71  sign_ = Sign::NEGATIVE;
72  } else if (velocity > 0.0) {
73  if (sign_ == Sign::NEGATIVE) {
74  positive_only_ = true;
75  flag_set = true;
76  }
77  sign_ = Sign::POSITIVE;
78  }
79  return flag_set;
80 }
81 
82 bool OscillationCritic::CommandTrend::isOscillating(double velocity)
83 {
84  return (positive_only_ && velocity < 0.0) || (negative_only_ && velocity > 0.0);
85 }
86 
87 bool OscillationCritic::CommandTrend::hasSignFlipped()
88 {
89  return positive_only_ || negative_only_;
90 }
91 
93 {
94  auto node = node_.lock();
95  if (!node) {
96  throw std::runtime_error{"Failed to lock node"};
97  }
98 
99  clock_ = node->get_clock();
100 
101  oscillation_reset_dist_ = nav_2d_utils::searchAndGetParam(
102  node,
103  dwb_plugin_name_ + "." + name_ + ".oscillation_reset_dist", 0.05);
104  oscillation_reset_dist_sq_ = oscillation_reset_dist_ * oscillation_reset_dist_;
105  oscillation_reset_angle_ = nav_2d_utils::searchAndGetParam(
106  node,
107  dwb_plugin_name_ + "." + name_ + ".oscillation_reset_angle", 0.2);
108  oscillation_reset_time_ = rclcpp::Duration::from_seconds(
109  nav_2d_utils::searchAndGetParam(
110  node,
111  dwb_plugin_name_ + "." + name_ + ".oscillation_reset_time", -1.0));
112 
113  nav2_util::declare_parameter_if_not_declared(
114  node,
115  dwb_plugin_name_ + "." + name_ + ".x_only_threshold", rclcpp::ParameterValue(0.05));
116 
124  node->get_parameter(dwb_plugin_name_ + "." + name_ + ".x_only_threshold", x_only_threshold_);
125  // TODO(crdelsey): How to handle searchParam?
126  // std::string resolved_name;
127  // if (node->hasParam("x_only_threshold"))
128  // {
129  // node->param("x_only_threshold", x_only_threshold_);
130  // }
131  // else if (node->searchParam("min_speed_xy", resolved_name))
132  // {
133  // node->param(resolved_name, x_only_threshold_);
134  // }
135  // else if (node->searchParam("min_trans_vel", resolved_name))
136  // {
137  // ROS_WARN_NAMED("OscillationCritic",
138  // "Parameter min_trans_vel is deprecated. "
139  // "Please use the name min_speed_xy or x_only_threshold instead.");
140  // node->param(resolved_name, x_only_threshold_);
141  // }
142  // else
143  // {
144  // x_only_threshold_ = 0.05;
145  // }
146 
147  reset();
148 }
149 
151  const geometry_msgs::msg::Pose2D & pose,
152  const nav_2d_msgs::msg::Twist2D &,
153  const geometry_msgs::msg::Pose2D &,
154  const nav_2d_msgs::msg::Path2D &)
155 {
156  pose_ = pose;
157  return true;
158 }
159 
160 void OscillationCritic::debrief(const nav_2d_msgs::msg::Twist2D & cmd_vel)
161 {
162  if (setOscillationFlags(cmd_vel)) {
163  prev_stationary_pose_ = pose_;
164  prev_reset_time_ = clock_->now();
165  }
166 
167  // if we've got restrictions... check if we can reset any oscillation flags
168  if (x_trend_.hasSignFlipped() || y_trend_.hasSignFlipped() || theta_trend_.hasSignFlipped()) {
169  // Reset flags if enough time or distance has passed
170  if (resetAvailable()) {
171  reset();
172  }
173  }
174 }
175 
176 bool OscillationCritic::resetAvailable()
177 {
178  if (oscillation_reset_dist_ >= 0.0) {
179  double x_diff = pose_.x - prev_stationary_pose_.x;
180  double y_diff = pose_.y - prev_stationary_pose_.y;
181  double sq_dist = x_diff * x_diff + y_diff * y_diff;
182  if (sq_dist > oscillation_reset_dist_sq_) {
183  return true;
184  }
185  }
186  if (oscillation_reset_angle_ >= 0.0) {
187  double th_diff = pose_.theta - prev_stationary_pose_.theta;
188  if (fabs(th_diff) > oscillation_reset_angle_) {
189  return true;
190  }
191  }
192  if (oscillation_reset_time_ >= rclcpp::Duration::from_seconds(0.0)) {
193  auto t_diff = (clock_->now() - prev_reset_time_);
194  if (t_diff > oscillation_reset_time_) {
195  return true;
196  }
197  }
198  return false;
199 }
200 
202 {
203  x_trend_.reset();
204  y_trend_.reset();
205  theta_trend_.reset();
206 }
207 
208 bool OscillationCritic::setOscillationFlags(const nav_2d_msgs::msg::Twist2D & cmd_vel)
209 {
210  bool flag_set = false;
211  // set oscillation flags for moving forward and backward
212  flag_set |= x_trend_.update(cmd_vel.x);
213 
214  // we'll only set flags for strafing and rotating when we're not moving forward at all
215  if (x_only_threshold_ < 0.0 || fabs(cmd_vel.x) <= x_only_threshold_) {
216  flag_set |= y_trend_.update(cmd_vel.y);
217  flag_set |= theta_trend_.update(cmd_vel.theta);
218  }
219  return flag_set;
220 }
221 
222 double OscillationCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj)
223 {
224  if (x_trend_.isOscillating(traj.velocity.x) ||
225  y_trend_.isOscillating(traj.velocity.y) ||
226  theta_trend_.isOscillating(traj.velocity.theta))
227  {
228  throw dwb_core::
229  IllegalTrajectoryException(name_, "Trajectory is oscillating.");
230  }
231  return 0.0;
232 }
233 
234 } // namespace dwb_critics
Evaluates a Trajectory2D to produce a score.
Checks to see whether the sign of the commanded velocity flips frequently.
Definition: oscillation.hpp:83
void debrief(const nav_2d_msgs::msg::Twist2D &cmd_vel) override
debrief informs the critic what the chosen cmd_vel was (if it cares)
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...
double scoreTrajectory(const dwb_msgs::msg::Trajectory2D &traj) override
Return a raw score for the given trajectory.
void reset() override
Reset the state of the critic.