abstract_recovery_execution.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, Magazino GmbH, Sebastian Pütz, Jorge Santos Simón
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * abstract_recovery_execution.cpp
34  *
35  * authors:
36  * Sebastian Pütz <spuetz@uni-osnabrueck.de>
37  * Jorge Santos Simón <santos@magazino.eu>
38  *
39  */
40 
41 #include <XmlRpcException.h>
42 #include <boost/exception/diagnostic_information.hpp>
43 
45 
46 namespace mbf_abstract_nav
47 {
48 
49 
51  const std::string name,
53  const TFPtr &tf_listener_ptr,
54  const MoveBaseFlexConfig &config,
55  boost::function<void()> setup_fn,
56  boost::function<void()> cleanup_fn) :
57  AbstractExecutionBase(name, setup_fn, cleanup_fn),
58  behavior_(recovery_ptr), tf_listener_ptr_(tf_listener_ptr), state_(INITIALIZED)
59  {
60  // dynamically reconfigurable parameters
61  reconfigure(config);
62  }
63 
65  {
66  }
67 
68 
69  void AbstractRecoveryExecution::reconfigure(const MoveBaseFlexConfig &config)
70  {
71  boost::lock_guard<boost::mutex> guard(conf_mtx_);
72 
73  // Maximum time allowed to recovery behaviors. Intended as a safeward for the case a behavior hangs.
74  // If it doesn't return within time, the navigator will cancel it and abort the corresponding action.
75  patience_ = ros::Duration(config.recovery_patience);
76 
77  // Nothing else to do here, as recovery_enabled is loaded and used in the navigation server
78  }
79 
80 
82  {
83  boost::lock_guard<boost::mutex> guard(state_mtx_);
84  state_ = state;
85  }
86 
87 
89  {
90  boost::lock_guard<boost::mutex> guard(state_mtx_);
91  return state_;
92  }
93 
95  {
96  cancel_ = true;
97  // returns false if cancel is not implemented or rejected by the recovery behavior (will run until completion)
98  if(!behavior_->cancel())
99  {
100  ROS_WARN_STREAM("Cancel recovering failed or is not supported by the plugin. "
101  << "Wait until the current recovery behavior finished!");
102  return false;
103  }
104  return true;
105  }
106 
108  {
109  boost::lock_guard<boost::mutex> guard1(conf_mtx_);
110  boost::lock_guard<boost::mutex> guard2(time_mtx_);
111  ROS_DEBUG_STREAM("Patience: " << patience_ << ", start time: " << start_time_ << " now: " << ros::Time::now());
112  return !patience_.isZero() && (ros::Time::now() - start_time_ > patience_);
113  }
114 
116  {
117  cancel_ = false; // reset the canceled state
118 
119  time_mtx_.lock();
121  time_mtx_.unlock();
123  try
124  {
125  outcome_ = behavior_->runBehavior(message_);
126  if (cancel_)
127  {
129  }
130  else
131  {
133  }
134  }
135  catch (boost::thread_interrupted &ex)
136  {
137  setState(STOPPED);
138  }
139  catch (...){
140  ROS_FATAL_STREAM("Unknown error occurred: " << boost::current_exception_diagnostic_information());
142  }
144  }
145 } /* namespace mbf_abstract_nav */
boost::condition_variable condition_
condition variable to wake up control thread
void reconfigure(const MoveBaseFlexConfig &config)
Reconfigures the current configuration and reloads all parameters. This method is called from a dynam...
AbstractRecoveryExecution::RecoveryState getState()
Returns the current state, thread-safe communication.
bool cancel_
flag for canceling controlling
std::string message_
the last received plugin execution message
void setState(RecoveryState state)
Sets the current internal state. This method is thread communication safe.
#define ROS_FATAL_STREAM(args)
virtual void run()
Main execution method which will be executed by the recovery execution thread_.
AbstractRecoveryExecution(const std::string name, const mbf_abstract_core::AbstractRecovery::Ptr recovery_ptr, const TFPtr &tf_listener_ptr, const MoveBaseFlexConfig &config, boost::function< void()> setup_fn, boost::function< void()> cleanup_fn)
Constructor.
uint32_t outcome_
the last received plugin execution outcome
ros::Time start_time_
recovery behavior start time
boost::mutex state_mtx_
mutex to handle safe thread communication for the current state
boost::mutex conf_mtx_
dynamic reconfigure and start time mutexes to mutually exclude read/write configuration ...
ros::Duration patience_
recovery behavior allowed time
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
virtual bool cancel()
Cancel the planner execution. This calls the cancel method of the planner plugin. This could be usefu...
mbf_abstract_core::AbstractRecovery::Ptr behavior_
the current loaded recovery behavior
bool isPatienceExceeded()
Checks whether the patience was exceeded.
static Time now()
void notify_one() BOOST_NOEXCEPT


mbf_abstract_nav
Author(s): Sebastian Pütz
autogenerated on Tue Jun 18 2019 19:34:34