action_based_controller_handle.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Unbounded Robotics Inc.
00005  *  Copyright (c) 2012, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Willow Garage nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 /* Author: Michael Ferguson, Ioan Sucan, E. Gil Jones */
00037 
00038 #ifndef MOVEIT_PLUGINS_ACTION_BASED_CONTROLLER_HANDLE
00039 #define MOVEIT_PLUGINS_ACTION_BASED_CONTROLLER_HANDLE
00040 
00041 #include <moveit/controller_manager/controller_manager.h>
00042 #include <actionlib/client/simple_action_client.h>
00043 #include <moveit/macros/class_forward.h>
00044 
00045 namespace moveit_simple_controller_manager
00046 {
00047 /*
00048  * This exist solely to inject addJoint/getJoints into base non-templated class.
00049  */
00050 class ActionBasedControllerHandleBase : public moveit_controller_manager::MoveItControllerHandle
00051 {
00052 public:
00053   ActionBasedControllerHandleBase(const std::string& name) : moveit_controller_manager::MoveItControllerHandle(name)
00054   {
00055   }
00056 
00057   virtual void addJoint(const std::string& name) = 0;
00058   virtual void getJoints(std::vector<std::string>& joints) = 0;
00059 };
00060 
00061 MOVEIT_CLASS_FORWARD(ActionBasedControllerHandleBase);
00062 
00063 /*
00064  * This is a simple base class, which handles all of the action creation/etc
00065  */
00066 template <typename T>
00067 class ActionBasedControllerHandle : public ActionBasedControllerHandleBase
00068 {
00069 public:
00070   ActionBasedControllerHandle(const std::string& name, const std::string& ns)
00071     : ActionBasedControllerHandleBase(name), namespace_(ns), done_(true)
00072   {
00073     controller_action_client_.reset(new actionlib::SimpleActionClient<T>(getActionName(), true));
00074     unsigned int attempts = 0;
00075     while (ros::ok() && !controller_action_client_->waitForServer(ros::Duration(5.0)) && ++attempts < 3)
00076       ROS_INFO_STREAM_NAMED("moveit_simple_controller_manager", "Waiting for " << getActionName() << " to come up");
00077 
00078     if (!controller_action_client_->isServerConnected())
00079     {
00080       ROS_ERROR_STREAM_NAMED("moveit_simple_controller_manager", "Action client not connected: " << getActionName());
00081       controller_action_client_.reset();
00082     }
00083 
00084     last_exec_ = moveit_controller_manager::ExecutionStatus::SUCCEEDED;
00085   }
00086 
00087   bool isConnected() const
00088   {
00089     return static_cast<bool>(controller_action_client_);
00090   }
00091 
00092   virtual bool cancelExecution()
00093   {
00094     if (!controller_action_client_)
00095       return false;
00096     if (!done_)
00097     {
00098       ROS_INFO_STREAM_NAMED("moveit_simple_controller_manager", "Cancelling execution for " << name_);
00099       controller_action_client_->cancelGoal();
00100       last_exec_ = moveit_controller_manager::ExecutionStatus::PREEMPTED;
00101       done_ = true;
00102     }
00103     return true;
00104   }
00105 
00106   virtual bool waitForExecution(const ros::Duration& timeout = ros::Duration(0))
00107   {
00108     if (controller_action_client_ && !done_)
00109       return controller_action_client_->waitForResult(timeout);
00110     return true;
00111   }
00112 
00113   virtual moveit_controller_manager::ExecutionStatus getLastExecutionStatus()
00114   {
00115     return last_exec_;
00116   }
00117 
00118   virtual void addJoint(const std::string& name)
00119   {
00120     joints_.push_back(name);
00121   }
00122 
00123   virtual void getJoints(std::vector<std::string>& joints)
00124   {
00125     joints = joints_;
00126   }
00127 
00128 protected:
00129   std::string getActionName(void) const
00130   {
00131     if (namespace_.empty())
00132       return name_;
00133     else
00134       return name_ + "/" + namespace_;
00135   }
00136 
00137   void finishControllerExecution(const actionlib::SimpleClientGoalState& state)
00138   {
00139     ROS_DEBUG_STREAM_NAMED("moveit_simple_controller_manager", "Controller " << name_ << " is done with state "
00140                                                                              << state.toString() << ": "
00141                                                                              << state.getText());
00142     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00143       last_exec_ = moveit_controller_manager::ExecutionStatus::SUCCEEDED;
00144     else if (state == actionlib::SimpleClientGoalState::ABORTED)
00145       last_exec_ = moveit_controller_manager::ExecutionStatus::ABORTED;
00146     else if (state == actionlib::SimpleClientGoalState::PREEMPTED)
00147       last_exec_ = moveit_controller_manager::ExecutionStatus::PREEMPTED;
00148     else
00149       last_exec_ = moveit_controller_manager::ExecutionStatus::FAILED;
00150     done_ = true;
00151   }
00152 
00153   /* execution status */
00154   moveit_controller_manager::ExecutionStatus last_exec_;
00155   bool done_;
00156 
00157   /* the controller namespace, for instance, topics will map to name/ns/goal, name/ns/result, etc */
00158   std::string namespace_;
00159 
00160   /* the joints controlled by this controller */
00161   std::vector<std::string> joints_;
00162 
00163   /* action client */
00164   boost::shared_ptr<actionlib::SimpleActionClient<T> > controller_action_client_;
00165 };
00166 
00167 }  // end namespace moveit_simple_controller_manager
00168 
00169 #endif  // MOVEIT_PLUGINS_ACTION_BASED_CONTROLLER_HANDLE


moveit_simple_controller_manager
Author(s): Michael Ferguson
autogenerated on Mon Jul 24 2017 02:22:36