rtt_actionlib_service.cpp
Go to the documentation of this file.
00001 
00002 #include <boost/algorithm/string.hpp>
00003 
00004 #include <rtt/RTT.hpp>
00005 #include <rtt/plugin/ServicePlugin.hpp>
00006 #include <rtt/internal/GlobalService.hpp>
00007 
00008 #include <rtt_roscomm/rtt_rostopic.h>
00009 
00010 #include <rtt_actionlib/rtt_actionlib.h>
00011 
00012 using namespace RTT;
00013 using namespace std;
00014 
00018 class ActionlibService : public RTT::Service 
00019 {
00020 public:
00025   ActionlibService(TaskContext* owner) 
00026     : Service("actionlib", owner)
00027   {
00028     this->doc("RTT Service for connecting RTT ports to ROS actionlib actions.");
00029 
00030     this->addOperation( "connect", 
00031         (bool (ActionlibService::*)(const std::string&))&ActionlibService::connect, this)
00032       .doc( "Connects a set of RTT data ports (goal,cancel,status,result,feedback) to a ROS actionlib action server or client.")
00033       .arg( "action_ns", "The ROS action namespace (like \"/some/action\").");
00034 
00035     this->addOperation( "connectSub", 
00036         (bool (ActionlibService::*)(const std::string&, const std::string&))&ActionlibService::connect, this)
00037       .doc( "Connects a set of RTT data ports (goal,cancel,status,result,feedback) defined on a sub-service to a ROS actionlib action server or client.")
00038       .arg( "service_name", "The RTT service name (like \"some_provided_service.another\") under which the ports are defined.")
00039       .arg( "action_ns", "The ROS action namespace (like \"/some/action\").");
00040 
00041   }
00042 
00044   RTT::Service::shared_ptr get_owner_service(const std::string rtt_uri)
00045   {
00046     // Split up the service uri
00047     std::vector<std::string> rtt_uri_tokens;
00048     boost::split(rtt_uri_tokens, rtt_uri, boost::is_any_of("."));
00049 
00050     // Make sure the uri has at least one token
00051     if(rtt_uri_tokens.size() < 1) {
00052       return RTT::Service::shared_ptr();
00053     }
00054 
00055     // Iterate through the tokens except for the last one (the operation name)
00056     RTT::Service::shared_ptr provided = this->getOwner()->provides();
00057     for(std::vector<std::string>::iterator it = rtt_uri_tokens.begin();
00058         it != rtt_uri_tokens.end();
00059         ++it)
00060     {
00061       if(provided->hasService(*it)) {
00062         provided = provided->provides(*it);
00063       } else {
00064         return RTT::Service::shared_ptr();
00065       }
00066     }
00067 
00068     // Get the operation 
00069     return provided;
00070   }
00071 
00074   bool connect(
00075     const std::string &ros_action_ns)
00076   {
00077     return this->connect(this->getOwner()->provides(), ros_action_ns);
00078   }
00079 
00082   bool connect(
00083     const std::string &rtt_service_name,
00084     const std::string &ros_action_ns)
00085   {
00086     // Check if the operation is provided by the owner
00087     RTT::Service::shared_ptr rtt_action_service = this->get_owner_service(rtt_service_name);
00088 
00089     // Return false if the operation isn't provided
00090     if(rtt_action_service.get() == NULL) {
00091       return false;
00092     }
00093     
00094     return this->connect(rtt_action_service, ros_action_ns);
00095   }
00096 
00100   bool connect(
00101       RTT::Service::shared_ptr rtt_action_service,
00102       const std::string &ros_action_ns)
00103   {
00104     // Make sure the service is good and owned by this service's owner
00105     if(rtt_action_service.get() == NULL || rtt_action_service->getOwner() != this->getOwner()) {
00106       return false;
00107     }
00108 
00109     // Construct the action bridge
00110     rtt_actionlib::ActionBridge action_bridge;
00111 
00112     // Get existing ports from the service
00113     if(!action_bridge.setPortsFromService(rtt_action_service)) {
00114       return false;
00115     }
00116 
00117     // Try to connect the topics
00118     if(!action_bridge.createStream(ros_action_ns)) {
00119       return false; 
00120     }
00121 
00122     return true;
00123   }
00124 };
00125 
00126 
00127 ORO_SERVICE_NAMED_PLUGIN(ActionlibService, "actionlib")
00128 


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