rtt_actionlib_service.cpp
Go to the documentation of this file.
1 
2 #include <boost/algorithm/string.hpp>
3 
4 #include <rtt/RTT.hpp>
7 
8 #include <rtt_roscomm/rostopic.h>
9 
11 
12 using namespace RTT;
13 using namespace std;
14 
19 {
20 public:
26  : Service("actionlib", owner)
27  {
28  this->doc("RTT Service for connecting RTT ports to ROS actionlib actions.");
29 
30  this->addOperation( "connect",
31  (bool (ActionlibService::*)(const std::string&))&ActionlibService::connect, this)
32  .doc( "Connects a set of RTT data ports (goal,cancel,status,result,feedback) to a ROS actionlib action server or client.")
33  .arg( "action_ns", "The ROS action namespace (like \"/some/action\").");
34 
35  this->addOperation( "connectSub",
36  (bool (ActionlibService::*)(const std::string&, const std::string&))&ActionlibService::connect, this)
37  .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.")
38  .arg( "service_name", "The RTT service name (like \"some_provided_service.another\") under which the ports are defined.")
39  .arg( "action_ns", "The ROS action namespace (like \"/some/action\").");
40 
41  }
42 
44  RTT::Service::shared_ptr get_owner_service(const std::string rtt_uri)
45  {
46  // Split up the service uri
47  std::vector<std::string> rtt_uri_tokens;
48  boost::split(rtt_uri_tokens, rtt_uri, boost::is_any_of("."));
49 
50  // Make sure the uri has at least one token
51  if(rtt_uri_tokens.size() < 1) {
52  return RTT::Service::shared_ptr();
53  }
54 
55  // Iterate through the tokens except for the last one (the operation name)
56  RTT::Service::shared_ptr provided = this->getOwner()->provides();
57  for(std::vector<std::string>::iterator it = rtt_uri_tokens.begin();
58  it != rtt_uri_tokens.end();
59  ++it)
60  {
61  if(provided->hasService(*it)) {
62  provided = provided->provides(*it);
63  } else {
64  return RTT::Service::shared_ptr();
65  }
66  }
67 
68  // Get the operation
69  return provided;
70  }
71 
74  bool connect(
75  const std::string &ros_action_ns)
76  {
77  return this->connect(this->getOwner()->provides(), ros_action_ns);
78  }
79 
82  bool connect(
83  const std::string &rtt_service_name,
84  const std::string &ros_action_ns)
85  {
86  // Check if the operation is provided by the owner
87  RTT::Service::shared_ptr rtt_action_service = this->get_owner_service(rtt_service_name);
88 
89  // Return false if the operation isn't provided
90  if(rtt_action_service.get() == NULL) {
91  return false;
92  }
93 
94  return this->connect(rtt_action_service, ros_action_ns);
95  }
96 
100  bool connect(
101  RTT::Service::shared_ptr rtt_action_service,
102  const std::string &ros_action_ns)
103  {
104  // Make sure the service is good and owned by this service's owner
105  if(rtt_action_service.get() == NULL || rtt_action_service->getOwner() != this->getOwner()) {
106  return false;
107  }
108 
109  // Construct the action bridge
110  rtt_actionlib::ActionBridge action_bridge;
111 
112  // Get existing ports from the service
113  if(!action_bridge.setPortsFromService(rtt_action_service)) {
114  return false;
115  }
116 
117  // Try to connect the topics
118  if(!action_bridge.createStream(ros_action_ns)) {
119  return false;
120  }
121 
122  return true;
123  }
124 };
125 
126 
128 
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
boost::shared_ptr< Service > shared_ptr
bool connect(const std::string &rtt_service_name, const std::string &ros_action_ns)
Connect.
bool setPortsFromService(RTT::Service::shared_ptr service)
Store the required RTT ports from a given RTT service.
ActionlibService(TaskContext *owner)
bool createStream(const std::string action_ns, RTT::ConnPolicy cp_template=RTT::ConnPolicy::data())
Create a stream from this RTT actionlib port interface to the appropriate ROS topic interface...
RTT::Service::shared_ptr get_owner_service(const std::string rtt_uri)
Get an RTT service from a string identifier.
bool connect(RTT::Service::shared_ptr rtt_action_service, const std::string &ros_action_ns)
Connect an RTT operation or operation caller to a ROS service server or service client.
bool connect(const std::string &ros_action_ns)


rtt_actionlib
Author(s): Jonathan Bohren
autogenerated on Sat Jun 8 2019 18:06:05