recovery_behaviors.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the institute nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Author: Christoph Rösmann
37  *********************************************************************/
38 
40 #include <ros/ros.h>
41 #include <limits>
42 #include <functional>
43 #include <numeric>
44 #include <g2o/stuff/misc.h>
45 
46 namespace teb_local_planner
47 {
48 
49 // ============== FailureDetector Implementation ===================
50 
51 void FailureDetector::update(const geometry_msgs::Twist& twist, double v_max, double v_backwards_max, double omega_max, double v_eps, double omega_eps)
52 {
53  if (buffer_.capacity() == 0)
54  return;
55 
56  VelMeasurement measurement;
57  measurement.v = twist.linear.x; // just consider linear velocity in x-direction in the robot frame for now
58  measurement.omega = twist.angular.z;
59 
60  if (measurement.v > 0 && v_max>0)
61  measurement.v /= v_max;
62  else if (measurement.v < 0 && v_backwards_max > 0)
63  measurement.v /= v_backwards_max;
64 
65  if (omega_max > 0)
66  measurement.omega /= omega_max;
67 
68  buffer_.push_back(measurement);
69 
70  // immediately compute new state
71  detect(v_eps, omega_eps);
72 }
73 
75 {
76  buffer_.clear();
77  oscillating_ = false;
78 }
79 
81 {
82  return oscillating_;
83 }
84 
85 bool FailureDetector::detect(double v_eps, double omega_eps)
86 {
87  oscillating_ = false;
88 
89  if (buffer_.size() < buffer_.capacity()/2) // we start detecting only as soon as we have the buffer filled at least half
90  return false;
91 
92  double n = (double)buffer_.size();
93 
94  // compute mean for v and omega
95  double v_mean=0;
96  double omega_mean=0;
97  int omega_zero_crossings = 0;
98  for (int i=0; i < n; ++i)
99  {
100  v_mean += buffer_[i].v;
101  omega_mean += buffer_[i].omega;
102  if ( i>0 && g2o::sign(buffer_[i].omega) != g2o::sign(buffer_[i-1].omega) )
103  ++omega_zero_crossings;
104  }
105  v_mean /= n;
106  omega_mean /= n;
107 
108  if (std::abs(v_mean) < v_eps && std::abs(omega_mean) < omega_eps && omega_zero_crossings>1 )
109  {
110  oscillating_ = true;
111  }
112 // ROS_INFO_STREAM("v: " << std::abs(v_mean) << ", omega: " << std::abs(omega_mean) << ", zero crossings: " << omega_zero_crossings);
113  return oscillating_;
114 }
115 
116 
117 
118 } // namespace teb_local_planner
bool detect(double v_eps, double omega_eps)
Detect if the robot got stucked based on the current buffer content.
void update(const geometry_msgs::Twist &twist, double v_max, double v_backwards_max, double omega_max, double v_eps, double omega_eps)
Add a new twist measurement to the internal buffer and compute a new decision.
boost::circular_buffer< VelMeasurement > buffer_
Circular buffer to store the last measurements.
void clear()
Clear the current internal state.
bool oscillating_
Current state: true if robot is oscillating.
bool isOscillating() const
Check if the robot got stucked.
def sign(number)


teb_local_planner
Author(s): Christoph Rösmann
autogenerated on Wed Jun 3 2020 04:03:08