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

相關文章
相關標籤/搜索