ros::NodeHandle成员介绍
完整英文介绍见网址http://docs.ros.org/jade/api/roscpp/html/classros_1_1NodeHandle.html
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);
2 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 a TransportHints structure which defines various transport-related options -
参数解释:
-
fp 消息到达时的回调函数指针
-
obj 回调对象
-
3 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_name The name of the service to connect to persistent Whether 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_values Key/value pairs you'd like to send along in the connection handshake
- 参数解释:service_name链接到的服务的名称
- persistent是否持续连接
- Exceptions:
InvalidNameException If 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:
service Service name to advertise on srv_func Member function pointer to call when a message has arrived obj Object 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 { ... }