rtt_actionlib.h
Go to the documentation of this file.
00001 #ifndef __RTT_ACTIONLIB_H
00002 #define __RTT_ACTIONLIB_H
00003 
00004 #include <rtt/RTT.hpp>
00005 
00006 #include <actionlib/action_definition.h>
00007 #include <actionlib_msgs/GoalID.h>
00008 #include <actionlib_msgs/GoalStatusArray.h>
00009 
00010 #include <rtt_roscomm/rtt_rostopic.h>
00011 
00012 namespace rtt_actionlib {
00013 
00014   /* \brief Actionlib RTT bridge used to create and access actionlib RTT data ports
00015    */
00016   class ActionBridge
00017   {
00018   public:
00019 
00021     ActionBridge() : 
00022       owns_port_pointers_(false), goal_(NULL), cancel_(NULL), status_(NULL), result_(NULL), feedback_(NULL)
00023     { }
00024 
00025     ~ActionBridge()
00026     {
00027       if(owns_port_pointers_) {
00028         if(goal_) delete goal_;
00029         if(cancel_) delete cancel_;
00030         if(status_) delete status_;
00031         if(result_) delete result_;
00032         if(feedback_) delete feedback_;
00033       }
00034     }
00035 
00037     template<class ActionSpec>
00038     bool createServerPorts()
00039     {
00040       // Generates typedefs that make our lives easier
00041       ACTION_DEFINITION(ActionSpec);
00042 
00043       // Make sure it isn't valid or connected
00044       if(this->isValid() || this->anyConnected()) {
00045         return false;
00046       }
00047 
00048       // Construct server ports
00049       goal_     = new RTT::InputPort<ActionGoal>("_action_goal");
00050       cancel_   = new RTT::InputPort<actionlib_msgs::GoalID>("_action_cancel");
00051       result_   = new RTT::OutputPort<ActionResult>("_action_result");
00052       status_   = new RTT::OutputPort<actionlib_msgs::GoalStatusArray>("_action_status");
00053       feedback_ = new RTT::OutputPort<ActionFeedback>("_action_feedback");
00054 
00055       // Set the ownership flag
00056       owns_port_pointers_ = true;
00057 
00058       return this->isValid();
00059     }
00060 
00061     template<class ActionSpec>
00062     bool createClientPorts()
00063     {
00064       // Generates typedefs that make our lives easier
00065       ACTION_DEFINITION(ActionSpec);
00066 
00067       // Make sure it isn't valid or connected
00068       if(this->isValid() || this->anyConnected()) {
00069         return false;
00070       }
00071 
00072       // Construct server ports
00073       goal_     = new RTT::OutputPort<ActionGoal>("_action_goal");
00074       cancel_   = new RTT::OutputPort<actionlib_msgs::GoalID>("_action_cancel");
00075       result_   = new RTT::InputPort<ActionResult>("_action_result");
00076       status_   = new RTT::InputPort<actionlib_msgs::GoalStatusArray>("_action_status");
00077       feedback_ = new RTT::InputPort<ActionFeedback>("_action_feedback");
00078 
00079       // Set the ownership flag
00080       owns_port_pointers_ = true;
00081 
00082       return this->isValid();
00083     }
00084 
00086     template<class ActionSpec>
00087     RTT::InputPort<typename ActionSpec::_action_goal_type>& goalInput() { return *dynamic_cast<RTT::InputPort<typename ActionSpec::_action_goal_type>*>(goal_); }
00089     RTT::InputPort<actionlib_msgs::GoalID>& cancelInput() { return *dynamic_cast<RTT::InputPort<actionlib_msgs::GoalID>*>(cancel_); }
00091     RTT::InputPort<actionlib_msgs::GoalStatusArray>& statusInput() { return *dynamic_cast<RTT::InputPort<actionlib_msgs::GoalStatusArray>*>(status_); }
00093     template<class ActionSpec>
00094     RTT::InputPort<typename ActionSpec::_action_result_type>& resultInput() { return *dynamic_cast<RTT::InputPort<typename ActionSpec::_action_result_type>*>(result_); }
00096     template<class ActionSpec>
00097     RTT::InputPort<typename ActionSpec::_action_feedback_type>& feedbackInput() { return *dynamic_cast<RTT::InputPort<typename ActionSpec::_action_feedback_type>*>(feedback_); }
00098     
00100     template<class ActionSpec>
00101     RTT::OutputPort<typename ActionSpec::_action_goal_type>& goalOutput() { return *dynamic_cast<RTT::OutputPort<typename ActionSpec::_action_goal_type>*>(goal_); }
00103     RTT::OutputPort<actionlib_msgs::GoalID>& cancelOutput() { return *dynamic_cast<RTT::OutputPort<actionlib_msgs::GoalID>*>(cancel_); }
00105     RTT::OutputPort<actionlib_msgs::GoalStatusArray>& statusOutput() { return *dynamic_cast<RTT::OutputPort<actionlib_msgs::GoalStatusArray>*>(status_); }
00107     template<class ActionSpec>
00108     RTT::OutputPort<typename ActionSpec::_action_result_type>& resultOutput() { return *dynamic_cast<RTT::OutputPort<typename ActionSpec::_action_result_type>*>(result_); }
00110     template<class ActionSpec>
00111     RTT::OutputPort<typename ActionSpec::_action_feedback_type>& feedbackOutput() { return *dynamic_cast<RTT::OutputPort<typename ActionSpec::_action_feedback_type>*>(feedback_); }
00112 
00114     bool setPorts(
00115         RTT::base::PortInterface* goal,
00116         RTT::base::PortInterface* cancel,
00117         RTT::base::PortInterface* status,
00118         RTT::base::PortInterface* result,
00119         RTT::base::PortInterface* feedback) 
00120     {
00121       // Store the ports
00122       goal_ = goal;
00123       cancel_ = cancel;
00124       status_ = status;
00125       result_ = result;
00126       feedback_ = feedback;
00127 
00128       // Set the ownership flag
00129       owns_port_pointers_ = false;
00130 
00131       return this->isValid();
00132     }
00133 
00135     bool setPortsFromService(RTT::Service::shared_ptr service)
00136     {
00137       // Make sure service isn't null
00138       if(service.get() == NULL) { return false; }
00139 
00140       // Get ports
00141       goal_ = service->getPort("_action_goal");
00142       cancel_ = service->getPort("_action_cancel");
00143       status_ = service->getPort("_action_status");
00144       result_ = service->getPort("_action_result");
00145       feedback_ = service->getPort("_action_feedback");
00146 
00147       // Set the ownership flag
00148       owns_port_pointers_ = false;
00149 
00150       // Return true if required ports are non-null
00151       return this->isValid();
00152     }
00153     
00155     bool isValid() const 
00156     {
00157       return goal_ && cancel_ && status_ && result_ && feedback_;
00158     }
00159 
00161     bool isServer() const
00162     {
00163       // Make sure the bridge is valid
00164       if(!this->isValid()) { return false; }
00165 
00166       RTT::base::InputPortInterface *goal_in, *cancel_in;
00167       RTT::base::OutputPortInterface *status_out, *result_out, *feedback_out;
00168 
00169       // Get port directions
00170       goal_in = dynamic_cast<RTT::base::InputPortInterface*>(goal_);
00171       cancel_in = dynamic_cast<RTT::base::InputPortInterface*>(cancel_);
00172 
00173       status_out = dynamic_cast<RTT::base::OutputPortInterface*>(status_);
00174       result_out = dynamic_cast<RTT::base::OutputPortInterface*>(result_);
00175       feedback_out = dynamic_cast<RTT::base::OutputPortInterface*>(feedback_);
00176 
00177       return goal_in && cancel_in && status_out && result_out && feedback_out;
00178     }
00179 
00181     bool isClient() const
00182     {
00183       // Make sure the bridge is valid
00184       if(!this->isValid()) { return false; }
00185 
00186       // Declare actual port directions
00187       RTT::base::InputPortInterface *status_in, *result_in, *feedback_in;
00188       RTT::base::OutputPortInterface *goal_out, *cancel_out;
00189 
00190       // Get port directions
00191       goal_out = dynamic_cast<RTT::base::OutputPortInterface*>(goal_);
00192       cancel_out = dynamic_cast<RTT::base::OutputPortInterface*>(cancel_);
00193 
00194       status_in = dynamic_cast<RTT::base::InputPortInterface*>(status_);
00195       result_in = dynamic_cast<RTT::base::InputPortInterface*>(result_);
00196       feedback_in = dynamic_cast<RTT::base::InputPortInterface*>(feedback_);
00197 
00198       return goal_out && cancel_out && status_in && result_in && feedback_in;
00199     }
00200 
00202     bool createStream(const std::string action_ns, RTT::ConnPolicy cp_template = RTT::ConnPolicy::data()) 
00203     {
00204       // Make sure the bridge is valid
00205       if(!this->isValid()) { return false; }
00206 
00207       // Construct connection policies for each port
00208       RTT::ConnPolicy 
00209         cp_goal = cp_template,
00210         cp_cancel = cp_template,
00211         cp_status = cp_template,
00212         cp_result = cp_template,
00213         cp_feedback = cp_template;
00214     
00215       // Set the ros connection policies
00216       cp_goal.transport = ORO_ROS_PROTOCOL_ID;
00217       cp_cancel.transport = ORO_ROS_PROTOCOL_ID;
00218       cp_status.transport = ORO_ROS_PROTOCOL_ID;
00219       cp_result.transport = ORO_ROS_PROTOCOL_ID;
00220       cp_feedback.transport = ORO_ROS_PROTOCOL_ID;
00221 
00222       // Construct the actionlib topic names
00223       cp_goal.name_id = action_ns+"/goal";
00224       cp_cancel.name_id = action_ns+"/cancel";
00225       cp_status.name_id = action_ns+"/status";
00226       cp_result.name_id = action_ns+"/result";
00227       cp_feedback.name_id = action_ns+"/feedback";
00228 
00229       // Connect each stream
00230       bool valid = true;
00231 
00232       valid &= goal_->createStream(cp_goal);
00233       valid &= cancel_->createStream(cp_cancel);
00234       valid &= status_->createStream(cp_status);
00235       valid &= result_->createStream(cp_result);
00236       valid &= feedback_->createStream(cp_feedback);
00237 
00238       return valid;
00239     }
00240 
00242     bool allConnected() const 
00243     {
00244       // Make sure the bridge is valid
00245       if(!this->isValid()) { return false; }
00246 
00247       bool valid = true;
00248 
00249       valid &= goal_->connected();
00250       valid &= cancel_->connected();
00251       valid &= status_->connected();
00252       valid &= result_->connected();
00253       valid &= feedback_->connected();
00254 
00255       return valid;
00256     }
00257 
00258     bool anyConnected() const 
00259     {
00260       // Make sure the bridge is valid
00261       if(!this->isValid()) { return false; }
00262 
00263       bool valid = false;
00264 
00265       valid |= goal_->connected();
00266       valid |= cancel_->connected();
00267       valid |= status_->connected();
00268       valid |= result_->connected();
00269       valid |= feedback_->connected();
00270 
00271       return valid;
00272     }
00273 
00274   private:
00275 
00276     // Ownership of pointers
00277     bool owns_port_pointers_;
00278 
00279     // RTT Ports
00280     RTT::base::PortInterface* goal_;
00281     RTT::base::PortInterface* cancel_;
00282     RTT::base::PortInterface* status_;
00283     RTT::base::PortInterface* result_;
00284     RTT::base::PortInterface* feedback_;
00285 
00286   };
00287 
00288 }
00289 
00290 #endif // ifndef __RTT_ACTIONLIB_H


rtt_actionlib
Author(s): Jonathan Bohren
autogenerated on Wed Sep 16 2015 07:00:05