Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MICROSRTT_NODE_HANDLE_HPP
00021 #define MICROSRTT_NODE_HANDLE_HPP
00022
00023 #include "ros/node_handle.h"
00024 #include "ros/message_traits.h"
00025 #include "micros_rtt/publisher.h"
00026 #include "micros_rtt/subscriber.h"
00027 #include "micros_rtt/topic_manager.h"
00028 #include "micros_rtt/oro/connection_factory.hpp"
00029
00030 namespace micros_rtt {
00031
00032 class NodeHandle
00033 {
00034 public:
00035 NodeHandle(const std::string& ns = std::string(), const ros::M_string& remappings = ros::M_string())
00036 {
00037
00038 ros_nh_ = ros::NodeHandle(ns, remappings);
00039
00040
00041 }
00042 ~NodeHandle()
00043 {
00044
00045 }
00046
00047
00048 void setCallbackQueue(ros::CallbackQueueInterface* queue);
00049 ros::CallbackQueueInterface* getCallbackQueue() const;
00050 const std::string& getNamespace() const ;
00051 const std::string& getUnresolvedNamespace();
00052 std::string resolveName(const std::string& name, bool remap = true) const;
00053
00054
00055
00056
00079 template <class M>
00080 Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)
00081 {
00082
00083 const std::string real_topic = resolveName(topic);
00084
00085 const std::string data_type = ros::message_traits::datatype<M>();
00086
00087
00088 ros::Publisher ros_pub = ros_nh_.advertise<M>(topic, queue_size, latch);
00089
00090 if (ros_pub)
00091 {
00092
00093 ROS_INFO("micros has published topic %s on ros with %s.", topic.c_str(), ros_pub.getTopic().c_str());
00094
00095
00096
00097 ConnectionBasePtr pub_connection(new Publication<M>(topic));
00098 TopicManager::instance()->addPubConnection(pub_connection);
00099
00100
00101 ConnFactory::createStream<M>(pub_connection, true);
00102
00103
00104 ConnectionBasePtr local_sub = TopicManager::instance()->findSubConnection(topic);
00105 if (local_sub)
00106 {
00107
00108 ConnFactory::createConnection<M>(pub_connection, local_sub);
00109 }
00110
00111 Publisher pub(ros_pub, pub_connection);
00112 return pub;
00113 }
00114 else
00115 {
00116 ROS_WARN("micros publishes topic %s on ros failed", topic.c_str());
00117 return Publisher();
00118 }
00119 }
00120
00153 template <class M>
00154 Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(*fp)(M), const ros::TransportHints& transport_hints = ros::TransportHints())
00155 {
00156
00157
00158 ros::Subscriber ros_sub = ros_nh_.subscribe<M>(topic, queue_size, NULL, transport_hints);
00159
00160 if (ros_sub)
00161 {
00162
00163 ROS_INFO("micros has subscribed topic %s on ros with %s.", topic.c_str(), ros_sub.getTopic().c_str());
00164
00165
00166
00167 ConnectionBasePtr sub_connection(new Subscription<M>(topic, fp));
00168 TopicManager::instance()->addSubConnection(sub_connection);
00169
00170
00171 ConnFactory::createStream<M>(sub_connection, false);
00172
00173
00174 ConnectionBasePtr local_pub = TopicManager::instance()->findPubConnection(topic);
00175 if (local_pub)
00176 {
00177
00178 ConnFactory::createConnection<M>(local_pub, sub_connection);
00179 }
00180
00181 Subscriber sub(ros_sub, sub_connection);
00182 return sub;
00183 }
00184 else
00185 {
00186 ROS_WARN("micros subscribes topic %s on ros failed", topic.c_str());
00187 return Subscriber();
00188 }
00189
00190 }
00191
00192 private:
00193 ros::NodeHandle ros_nh_;
00194
00195 };
00196
00197 }
00198
00199 #endif