ros::NodeHandle成員介紹

完整英文介紹見網址http://docs.ros.org/jade/api/roscpp/html/classros_1_1NodeHandle.html   

ros::NodeHandle

NodeHandle::advertise

Publisher ros::NodeHandle::advertise ( const std::string &  topic,
    uint32_t  queue_size,
    bool  latch = false 
  )   [inline]

Advertise a topic, simple version.

This call connects to the master to publicize that the node will be publishing messages on the given topic. This method returns a Publisher that allows you to publish a message on this topic.

This version of advertise is a templated convenience function, and can be used like so

使用示例:

ros::Publisher pub = handle.advertise<std_msgs::Empty>("my_topic", 1);

Parameters:
topic Topic to advertise on
queue_size Maximum number of outgoing messages to be queued for delivery to subscribers
latch (optional) If true, the last message published on this topic will be saved and sent to new subscribers when they connect
Returns:
On success, a Publisher that, when it goes out of scope, will automatically release a reference on this advertisement. On failure, an empty Publisher.
Exceptions:

InvalidNameException If the topic name begins with a tilde, or is an otherwise invalid graph resource name, or is an otherwise invalid graph resource name

介紹:以指定名稱,隊列大小發佈一個topic,latch參數表示是否發送保存,可不初始化。

回調函數寫法:

 void connectCallback(const ros::SingleSubscriberPublisher& pub)
     {
     // Do something
     }

 ros::Publisher pub =handle.advertise<std_msgs::Empty>("my_topic", 1, (ros::SubscriberStatusCallback)connectCallback);
NodeHandle::subscribe

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

Subscribe to a topic, version for class member function with bare pointer.

This method connects to the master to register interest in a given topic. The node will automatically be connected with publishers on this topic. On each message receipt, fp is invoked and passed a shared pointer to the message received. This message should not be changed in place, as it is shared with any other subscriptions to this topic.

This version of subscribe is a convenience function for using member functions, and can be used like so:

void Foo::callback(const std_msgs::Empty::ConstPtr& message)
{
}

Foo foo_object;
ros::Subscriber sub = handle.subscribe("my_topic", 1, &Foo::callback, &foo_object);
參數  
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

參數解釋:
fp 消息到達時的回調函數指針
obj 回調對象
NodeHandle::serviceClient
ServiceClient ros::NodeHandle::serviceClient(const std::string & service_name,
  bool persistent = false,
  const M_string & header_values = M_string() 
 ) [inline]

Create a client for a service, version templated on two message types.

When the last handle reference of a persistent connection is cleared, the connection will automatically close.

Parameters:
service_nameThe name of the service to connect to
persistentWhether this connection should persist. Persistent services keep the connection to the remote host active so that subsequent calls will happen faster. In general persistent services are discouraged, as they are not as robust to node failure as non-persistent services.
header_valuesKey/value pairs you'd like to send along in the connection handshake
參數解釋:service_name鏈接到的服務的名稱
persistent是否持續連接
Exceptions:
InvalidNameExceptionIf the service name begins with a tilde, or is an otherwise invalid graph resource name
4 NodeHandle::advertiseService
 ros::NodeHandle::advertiseService(const std::string & service,
  bool(T::*)(MReq &, MRes &) srv_func,
  T * obj 
 ) [inline]

Advertise a service, version for class member function with bare pointer.

This call connects to the master to publicize that the node will be offering an RPC service with the given name.

This is a convenience function for using member functions, and can be used like so:

bool Foo::callback(std_srvs::Empty& request, std_srvs::Empty& response)
{
  return true;
}

Foo foo_object;
ros::ServiceServer service = handle.advertiseService("my_service", &Foo::callback, &foo_object);
Parameters:
serviceService name to advertise on
srv_funcMember function pointer to call when a message has arrived
objObject to call srv_func on
Returns:
On success, a ServiceServer that, when all copies of it go out of scope, will unadvertise this service. On failure, an empty ServiceServer which can be checked with:
bool Foo::callback(std_srvs::Empty& request, std_srvs::Empty& response)
{
  return true;
}
ros::NodeHandle nodeHandle;
Foo foo_object;
ros::ServiceServer service = nodeHandle.advertiseService("my_service", &Foo::callback, &foo_object);
if (service)  // Enter if advertised service is valid
{
...
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章