abstract_recovery_execution.cpp
Go to the documentation of this file.
00001 /*
00002  *  Copyright 2018, Magazino GmbH, Sebastian Pütz, Jorge Santos Simón
00003  *
00004  *  Redistribution and use in source and binary forms, with or without
00005  *  modification, are permitted provided that the following conditions
00006  *  are met:
00007  *
00008  *  1. Redistributions of source code must retain the above copyright
00009  *     notice, this list of conditions and the following disclaimer.
00010  *
00011  *  2. Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *
00016  *  3. Neither the name of the copyright holder nor the names of its
00017  *     contributors may be used to endorse or promote products derived
00018  *     from this software without specific prior written permission.
00019  *
00020  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  *  POSSIBILITY OF SUCH DAMAGE.
00032  *
00033  *  abstract_recovery_execution.cpp
00034  *
00035  *  authors:
00036  *    Sebastian Pütz <spuetz@uni-osnabrueck.de>
00037  *    Jorge Santos Simón <santos@magazino.eu>
00038  *
00039  */
00040 
00041 #include <XmlRpcException.h>
00042 #include <boost/exception/diagnostic_information.hpp>
00043 
00044 #include <mbf_abstract_nav/abstract_recovery_execution.h>
00045 
00046 namespace mbf_abstract_nav
00047 {
00048 
00049 
00050   AbstractRecoveryExecution::AbstractRecoveryExecution(
00051       const std::string name,
00052       mbf_abstract_core::AbstractRecovery::Ptr recovery_ptr,
00053       const TFPtr &tf_listener_ptr,
00054       const MoveBaseFlexConfig &config,
00055       boost::function<void()> setup_fn,
00056       boost::function<void()> cleanup_fn) :
00057     AbstractExecutionBase(name, setup_fn, cleanup_fn),
00058       behavior_(recovery_ptr), tf_listener_ptr_(tf_listener_ptr), state_(INITIALIZED)
00059   {
00060     // dynamically reconfigurable parameters
00061     reconfigure(config);
00062   }
00063 
00064   AbstractRecoveryExecution::~AbstractRecoveryExecution()
00065   {
00066   }
00067 
00068 
00069   void AbstractRecoveryExecution::reconfigure(const MoveBaseFlexConfig &config)
00070   {
00071     boost::lock_guard<boost::mutex> guard(conf_mtx_);
00072 
00073     // Maximum time allowed to recovery behaviors. Intended as a safeward for the case a behavior hangs.
00074     // If it doesn't return within time, the navigator will cancel it and abort the corresponding action.
00075     patience_ = ros::Duration(config.recovery_patience);
00076 
00077     // Nothing else to do here, as recovery_enabled is loaded and used in the navigation server
00078   }
00079 
00080 
00081   void AbstractRecoveryExecution::setState(RecoveryState state)
00082   {
00083     boost::lock_guard<boost::mutex> guard(state_mtx_);
00084     state_ = state;
00085   }
00086 
00087 
00088   typename AbstractRecoveryExecution::RecoveryState AbstractRecoveryExecution::getState()
00089   {
00090     boost::lock_guard<boost::mutex> guard(state_mtx_);
00091     return state_;
00092   }
00093 
00094   bool AbstractRecoveryExecution::cancel()
00095   {
00096     cancel_ = true;
00097     // returns false if cancel is not implemented or rejected by the recovery behavior (will run until completion)
00098     if(!behavior_->cancel())
00099     {
00100       ROS_WARN_STREAM("Cancel recovering failed or is not supported by the plugin. "
00101                           << "Wait until the current recovery behavior finished!");
00102       return false;
00103     }
00104     return true;
00105   }
00106 
00107   bool AbstractRecoveryExecution::isPatienceExceeded()
00108   {
00109     boost::lock_guard<boost::mutex> guard1(conf_mtx_);
00110     boost::lock_guard<boost::mutex> guard2(time_mtx_);
00111     ROS_DEBUG_STREAM("Patience: " << patience_ << ", start time: " << start_time_ << " now: " << ros::Time::now());
00112     return !patience_.isZero() && (ros::Time::now() - start_time_ > patience_);
00113   }
00114 
00115   void AbstractRecoveryExecution::run()
00116   {
00117     cancel_ = false; // reset the canceled state
00118 
00119     time_mtx_.lock();
00120     start_time_ = ros::Time::now();
00121     time_mtx_.unlock();
00122     setState(RECOVERING);
00123     try
00124     {
00125       outcome_ = behavior_->runBehavior(message_);
00126       if (cancel_)
00127       {
00128         setState(CANCELED);
00129       }
00130       else
00131       {
00132         setState(RECOVERY_DONE);
00133       }
00134     }
00135     catch (boost::thread_interrupted &ex)
00136     {
00137       setState(STOPPED);
00138     }
00139     catch (...){
00140       ROS_FATAL_STREAM("Unknown error occurred: " << boost::current_exception_diagnostic_information());
00141       setState(INTERNAL_ERROR);
00142     }
00143     condition_.notify_one();
00144   }
00145 } /* namespace mbf_abstract_nav */


mbf_abstract_nav
Author(s): Sebastian Pütz
autogenerated on Mon Jun 17 2019 20:11:35