Nav2 Navigation Stack - lyrical  lyrical
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 "dwb_core/exceptions.hpp"
41 #include "pluginlib/class_list_macros.hpp"
42 #include "angles/angles.h"
43 
45 
46 namespace dwb_critics
47 {
48 
49 
50 OscillationCritic::CommandTrend::CommandTrend()
51 {
52  reset();
53 }
54 
55 void OscillationCritic::CommandTrend::reset()
56 {
57  sign_ = Sign::ZERO;
58  positive_only_ = false;
59  negative_only_ = false;
60 }
61 
62 bool OscillationCritic::CommandTrend::update(double velocity)
63 {
64  bool flag_set = false;
65  if (velocity < 0.0) {
66  if (sign_ == Sign::POSITIVE) {
67  negative_only_ = true;
68  flag_set = true;
69  }
70  sign_ = Sign::NEGATIVE;
71  } else if (velocity > 0.0) {
72  if (sign_ == Sign::NEGATIVE) {
73  positive_only_ = true;
74  flag_set = true;
75  }
76  sign_ = Sign::POSITIVE;
77  }
78  return flag_set;
79 }
80 
81 bool OscillationCritic::CommandTrend::isOscillating(double velocity)
82 {
83  return (positive_only_ && velocity < 0.0) || (negative_only_ && velocity > 0.0);
84 }
85 
86 bool OscillationCritic::CommandTrend::hasSignFlipped()
87 {
88  return positive_only_ || negative_only_;
89 }
90 
91 void OscillationCritic::onInit()
92 {
93  auto node = node_.lock();
94  if (!node) {
95  throw std::runtime_error{"Failed to lock node"};
96  }
97 
98  clock_ = node->get_clock();
99 
100  oscillation_reset_dist_ = node->declare_or_get_parameter(
101  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  x_only_threshold_ = node->declare_or_get_parameter(
111  dwb_plugin_name_ + "." + name_ + ".x_only_threshold", 0.05);
112 
113  reset();
114 }
115 
117  const geometry_msgs::msg::Pose & pose,
118  const nav_2d_msgs::msg::Twist2D &,
119  const geometry_msgs::msg::Pose &,
120  const nav_msgs::msg::Path &)
121 {
122  pose_ = pose;
123  return true;
124 }
125 
126 void OscillationCritic::debrief(const nav_2d_msgs::msg::Twist2D & cmd_vel)
127 {
128  if (setOscillationFlags(cmd_vel)) {
129  prev_stationary_pose_ = pose_;
130  prev_reset_time_ = clock_->now();
131  }
132 
133  // if we've got restrictions... check if we can reset any oscillation flags
134  if (x_trend_.hasSignFlipped() || y_trend_.hasSignFlipped() || theta_trend_.hasSignFlipped()) {
135  // Reset flags if enough time or distance has passed
136  if (resetAvailable()) {
137  reset();
138  }
139  }
140 }
141 bool OscillationCritic::resetAvailable()
142 {
143  if (oscillation_reset_dist_ >= 0.0) {
144  double x_diff = pose_.position.x - prev_stationary_pose_.position.x;
145  double y_diff = pose_.position.y - prev_stationary_pose_.position.y;
146  double sq_dist = x_diff * x_diff + y_diff * y_diff;
147  if (sq_dist > oscillation_reset_dist_sq_) {
148  return true;
149  }
150  }
151  if (oscillation_reset_angle_ >= 0.0) {
152  tf2::Quaternion pose_q, prev_stationary_pose_q;
153  tf2::fromMsg(pose_.orientation, pose_q);
154  tf2::fromMsg(prev_stationary_pose_.orientation, prev_stationary_pose_q);
155 
156  double th_diff = angles::shortest_angular_distance(
157  tf2::getYaw(pose_q), tf2::getYaw(prev_stationary_pose_q));
158 
159  if (fabs(th_diff) > oscillation_reset_angle_) {
160  return true;
161  }
162  }
163  if (oscillation_reset_time_ >= rclcpp::Duration::from_seconds(0.0)) {
164  auto t_diff = (clock_->now() - prev_reset_time_);
165  if (t_diff > oscillation_reset_time_) {
166  return true;
167  }
168  }
169  return false;
170 }
171 
173 {
174  x_trend_.reset();
175  y_trend_.reset();
176  theta_trend_.reset();
177 }
178 
179 bool OscillationCritic::setOscillationFlags(const nav_2d_msgs::msg::Twist2D & cmd_vel)
180 {
181  bool flag_set = false;
182  // set oscillation flags for moving forward and backward
183  flag_set |= x_trend_.update(cmd_vel.x);
184 
185  // we'll only set flags for strafing and rotating when we're not moving forward at all
186  if (x_only_threshold_ < 0.0 || fabs(cmd_vel.x) <= x_only_threshold_) {
187  flag_set |= y_trend_.update(cmd_vel.y);
188  flag_set |= theta_trend_.update(cmd_vel.theta);
189  }
190  return flag_set;
191 }
192 
193 double OscillationCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj)
194 {
195  if (x_trend_.isOscillating(traj.velocity.x) ||
196  y_trend_.isOscillating(traj.velocity.y) ||
197  theta_trend_.isOscillating(traj.velocity.theta))
198  {
199  throw dwb_core::
200  IllegalTrajectoryException(name_, "Trajectory is oscillating.");
201  }
202  return 0.0;
203 }
204 
205 } // 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.