您的位置:首页 > Web前端 > Node.js

ros::NodeHandle成员介绍

2016-08-17 10:32 716 查看
完整英文介绍见网址http://docs.ros.org/jade/api/roscpp/html/classros_1_1NodeHandle.html   ros::NodeHandleNodeHandle::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 thatallows 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:
topicTopic to advertise on
queue_sizeMaximum 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:
InvalidNameExceptionIf 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);
参数 
topicTopic to subscribe to
queue_sizeNumber of incoming messages to queue up for processing (messages in excess of this queue capacity will be discarded).
fpMember function pointer to call when a message has arrived
objObject to call fp on
transport_hintsTransportHints structurewhich defines various transport-related options
参数解释:
fp 消息到达时的回调函数指针
obj 回调对象
NodeHandle::serviceClient
ServiceClient ros::NodeHandle::serviceClient(conststd::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
persistentWhetherthis 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/valuepairs you'd like to send along in the connection handshake
参数解释:service_name链接到的服务的名称 persistent是否持续连接Exceptions:
InvalidNameExceptionIfthe service name begins with a tilde, or is an otherwise invalid graph resource name
4 NodeHandle::advertiseService
 ros::NodeHandle::advertiseService(conststd::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_funcMemberfunction 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
{
...
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rosNodeHandle