ROS入门(七) subsribe的四个参数详解

订阅节点,主要经过subscribe方式。html

 

最多见的使用方法api

void callback(const std_msgs::Empty::ConstPtr& message) { } ros::Subscriber sub = handle.subscribe("my_topic", 1, callback);

其中subscribe有不少重定义。例如:函数

Subscriber ros::NodeHandle::subscribe    (    const std::string & topic, uint32_t queue_size, void(*)(M) fp, const TransportHints &     transport_hints = TransportHints() ) 

Parameters:ui

M [template] M here is the callback parameter type (e.g. const boost::shared_ptr<M const>& or const M&), not the message type, and should almost always be deduced
topic Topic to subscribe to
queue_size Number of incoming messages to queue up for processing (messages in excess of this queue capacity will be discarded).
fp Function pointer to call when a message has arrived
transport_hints TransportHints structure which defines various transport-related options

 其中的参数: this

                   topic 为订阅的节点名,字符串类型。spa

                   queue_size 为待处理信息队列大小。ssr

                   fp 当消息传入时,能够调用的函数指针,即回调函数。指针

而其中M是回调函数的不一样类型,例如const boost::shared_ptr<M const>& or const M&。这样的话,咱们还能够使用boost::bind()调用回调函数。code

 

可是,咱们还会遇到subscribe()的第四个参数,例如:htm

Subscriber ros::NodeHandle::subscribe    (    const std::string & topic,                             uint32_t queue_size,                             void(T::*)(M) fp,                            T * obj,                             const TransportHints &     transport_hints = TransportHints() ) 

 Parameters:

M [template] M here is the message type
topic Topic to subscribe to
queue_size Number of incoming messages to queue up for processing (messages in excess of this queue capacity will be discarded).
fp Member function pointer to call when a message has arrived
obj Object to call fp on
transport_hints TransportHints structure which defines various transport-related options

 

 当咱们要使用一个Class里面的返回函数以后,咱们须要调用第四个参数。例如咱们有一个class:

class Listener { public:   void callback(const std_msgs::String::ConstPtr& msg); }; 

若是想要调用这个class里的返回函数,能够使用第四个参数,以下:

Listener listener; ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, &listener);

第四个参数定义的class,这样咱们调用的就是Listener的callback函数。若是方法在class里面,能够使用this。以下:

class Listener
{
public:
  void callback(const std_msgs::String::ConstPtr& msg){}

ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, this);
  void TestObject(ros::NodeHandle n)
  {
    
  }
}; 

这样能够调用Class里的回调函数啦。 

 

 

参考资料

ROS::NodeHandle :

http://docs.ros.org/jade/api/roscpp/html/classros_1_1NodeHandle.html#a93b0fe95db250c45fdfbc5fd9f4c0159

Using Class Methods as Callbacks:

http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks\

Using Class Methods as Callbacks issu:

https://answers.ros.org/question/207254/setting-class-method-callback-from-within-object-c/

第四个参数:

http://www.cnblogs.com/feixiao5566/p/4791990.html

相关文章
相关标签/搜索