Nav2 Navigation Stack - rolling  main
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 "nav2_ros_common/node_utils.hpp"
41 #include "dwb_core/exceptions.hpp"
42 #include "pluginlib/class_list_macros.hpp"
43 #include "angles/angles.h"
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_ = node->declare_or_get_parameter(dwb_plugin_name_ + "." + name_ +
102  ".oscillation_reset_dist", 0.05);
103  oscillation_reset_dist_sq_ = oscillation_reset_dist_ * oscillation_reset_dist_;
104  oscillation_reset_angle_ = node->declare_or_get_parameter(
105  dwb_plugin_name_ + "." + name_ + ".oscillation_reset_angle", 0.2);
106  oscillation_reset_time_ = rclcpp::Duration::from_seconds(
107  node->declare_or_get_parameter(
108  dwb_plugin_name_ + "." + name_ + ".oscillation_reset_time", -1.0));
109 
110  nav2::declare_parameter_if_not_declared(
111  node,
112  dwb_plugin_name_ + "." + name_ + ".x_only_threshold", rclcpp::ParameterValue(0.05));
113 
121  node->get_parameter(dwb_plugin_name_ + "." + name_ + ".x_only_threshold", x_only_threshold_);
122  // TODO(crdelsey): How to handle searchParam?
123  // std::string resolved_name;
124  // if (node->hasParam("x_only_threshold"))
125  // {
126  // node->param("x_only_threshold", x_only_threshold_);
127  // }
128  // else if (node->searchParam("min_speed_xy", resolved_name))
129  // {
130  // node->param(resolved_name, x_only_threshold_);
131  // }
132  // else if (node->searchParam("min_trans_vel", resolved_name))
133  // {
134  // ROS_WARN_NAMED("OscillationCritic",
135  // "Parameter min_trans_vel is deprecated. "
136  // "Please use the name min_speed_xy or x_only_threshold instead.");
137  // node->param(resolved_name, x_only_threshold_);
138  // }
139  // else
140  // {
141  // x_only_threshold_ = 0.05;
142  // }
143 
144  reset();
145 }
146 
148  const geometry_msgs::msg::Pose & pose,
149  const nav_2d_msgs::msg::Twist2D &,
150  const geometry_msgs::msg::Pose &,
151  const nav_msgs::msg::Path &)
152 {
153  pose_ = pose;
154  return true;
155 }
156 
157 void OscillationCritic::debrief(const nav_2d_msgs::msg::Twist2D & cmd_vel)
158 {
159  if (setOscillationFlags(cmd_vel)) {
160  prev_stationary_pose_ = pose_;
161  prev_reset_time_ = clock_->now();
162  }
163 
164  // if we've got restrictions... check if we can reset any oscillation flags
165  if (x_trend_.hasSignFlipped() || y_trend_.hasSignFlipped() || theta_trend_.hasSignFlipped()) {
166  // Reset flags if enough time or distance has passed
167  if (resetAvailable()) {
168  reset();
169  }
170  }
171 }
172 bool OscillationCritic::resetAvailable()
173 {
174  if (oscillation_reset_dist_ >= 0.0) {
175  double x_diff = pose_.position.x - prev_stationary_pose_.position.x;
176  double y_diff = pose_.position.y - prev_stationary_pose_.position.y;
177  double sq_dist = x_diff * x_diff + y_diff * y_diff;
178  if (sq_dist > oscillation_reset_dist_sq_) {
179  return true;
180  }
181  }
182  if (oscillation_reset_angle_ >= 0.0) {
183  tf2::Quaternion pose_q, prev_stationary_pose_q;
184  tf2::fromMsg(pose_.orientation, pose_q);
185  tf2::fromMsg(prev_stationary_pose_.orientation, prev_stationary_pose_q);
186 
187  double th_diff = angles::shortest_angular_distance(
188  tf2::getYaw(pose_q), tf2::getYaw(prev_stationary_pose_q));
189 
190  if (fabs(th_diff) > oscillation_reset_angle_) {
191  return true;
192  }
193  }
194  if (oscillation_reset_time_ >= rclcpp::Duration::from_seconds(0.0)) {
195  auto t_diff = (clock_->now() - prev_reset_time_);
196  if (t_diff > oscillation_reset_time_) {
197  return true;
198  }
199  }
200  return false;
201 }
202 
204 {
205  x_trend_.reset();
206  y_trend_.reset();
207  theta_trend_.reset();
208 }
209 
210 bool OscillationCritic::setOscillationFlags(const nav_2d_msgs::msg::Twist2D & cmd_vel)
211 {
212  bool flag_set = false;
213  // set oscillation flags for moving forward and backward
214  flag_set |= x_trend_.update(cmd_vel.x);
215 
216  // we'll only set flags for strafing and rotating when we're not moving forward at all
217  if (x_only_threshold_ < 0.0 || fabs(cmd_vel.x) <= x_only_threshold_) {
218  flag_set |= y_trend_.update(cmd_vel.y);
219  flag_set |= theta_trend_.update(cmd_vel.theta);
220  }
221  return flag_set;
222 }
223 
224 double OscillationCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj)
225 {
226  if (x_trend_.isOscillating(traj.velocity.x) ||
227  y_trend_.isOscillating(traj.velocity.y) ||
228  theta_trend_.isOscillating(traj.velocity.theta))
229  {
230  throw dwb_core::
231  IllegalTrajectoryException(name_, "Trajectory is oscillating.");
232  }
233  return 0.0;
234 }
235 
236 } // 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::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...
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.