roscpp中常用的函数、类以及命名空间
前言
经过了一段时间的ros与gazebo机器人编队仿真的学习,对ros的结构有了一定了解,接下来将对松灵机器人的底盘进行实车开发,主要开发语言使用C++,转载一篇对ros中c++常用的函数,命名空间进行学习,为以后的开发作理论的准备。
思维导图
图中列出了部分ros中常用的函数、类以及命名空间,呈现一个大致的框架。
功能&用法 分析
函数
ros::init()
功能详解
原文:
ROS initialization function.
This function will parse any ROS arguments (e.g., topic name remappings), and will consume them (i.e., argc and argv may be modified as a result of this call).
Use this version if you are using the NodeHandle API
这是ROS的初始化函数。这个函数会分析ROS的参数(例如,话题名程的重新映射),并且利用它们(换句话将,调用这个函数可能会使argc和argv被修改)
在使用NodleHandle API前需调用这个函数的这个版本。
函数原型:
void ros::init ( int & argc,
char ** argv,
const std::string & name,
uint32_t options = 0
)
- 1
- 2
- 3
- 4
- 5
- 6
除此之外,ros::init(...)
还有两种定义的形式,详见ros::init()
参数详解
- argc:参数个数,一般由
int main(int argc, char ** argv)
提供 - argv:指向字符串数组(即参数文本)的指针,一般由
int main(int argc, char ** argv)
提供 - name:节点的名字,必须是基础的名字(基础的含义是不能包括命名空间)
- options:可空,默认options=0,详见ros::init()
类
NodeHandle
1.advertise()
功能详解
原文:
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);
广播话题函数的简单版本。使用这个函数会告诉master节点本节点将在指定的topic上广播。这个方法会返回一个可以在指定topic上广播的Publisher对象。
这个版本的广播函数是一个泛型函数,可以像这样被使用:
ros::Publisher pub = handle.advertise<std_msgs::Empty>("my_topic", 1);
函数原型:
Publisher ros::NodeHandle::advertise(
const std::string & topic,
uint32_t queue_size,
bool latch = false
)
参数详解
- topic:要广播的话题
- queue_size:队列大小,即仓库中最多可以放几条信息
- 可空,默认为false。如果该项为true,当有一个新的订阅者时,会向新的订阅者发送最后一条广播(在其订阅之前的最后一条)
2.subscribe()
功能详解
原文:
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);
ros::NodeHandle nodeHandle;
void Foo::callback(const std_msgs::Empty::ConstPtr& message) {}
boost::shared_ptr<Foo> foo_object(boost::make_shared<Foo>());
ros::Subscriber sub = nodeHandle.subscribe(“my_topic”, 1, &Foo::callback, foo_object);
if (sub) // Enter if subscriber is valid
{
…
}
订阅话题函数,这个版本是针对没有指针指向的友元函数(?不清楚理解的对不对)的。这个函数会告诉master节点订阅哪个topic,本节点会自动和订阅发布者发布在topic上的消息。每当消息收到时,fp会被请求,一个共享的指针会指向接受到的消息。这个消息不能被修改,因为他也被其他订阅者共享。
这个版本的订阅函数是泛型函数,callback是成员函数,可以像上述一样被使用。
函数原型:
Subscriber ros::NodeHandle::subscribe (
const std::string & topic,
uint32_t queue_size,
void(T::*)(M) fp,
T * obj,
const TransportHints & transport_hints = TransportHints()
)
其他版本详见ros::NodeHandle::subscribe()
参数详解
- topic:要订阅的话题
- queue_size: 消息队列的大小(超过此队列容量的消息将被废弃)
- fp:指向回调函数的指针
- obj:Object to call fp on.
- transport_hints:a TransportHints structure which defines various transport-related options
更多与NodeHandle类有关的信息见 NodeHandle
Publisher
简介
官方介绍:
Manages an advertisement on a specific topic.
A Publisher should always be created through a call to NodeHandle::advertise(), or copied from one that was. Once all copies of a specific Publisher go out of scope, any subscriber status callbacks associated with that handle will stop being called. Once all Publishers for a given topic go out of scope the topic will be unadvertised.
Definition at line 47 of file publisher.h.
管理指定topic上的广播
发布者应该总是通过NodeHandle::advertise()
创建,或者从一个已存在的发布者拷贝而来。一旦所有发布者都消亡了,所有订阅者的回调函数将不被调用;一旦所有发布者都消亡了,这个话题将不再被广播。
有关于Publisher的详细介绍见官方文档 ros::Publisher Class Reference
Subscriber
简介
官方介绍:
Manages an subscription callback on a specific topic.
A Subscriber should always be created through a call to NodeHandle::subscribe(), or copied from one that was. Once all copies of a specific Subscriber go out of scope, the subscription callback associated with that handle will stop being called. Once all Subscriber for a given topic go out of scope the topic will be unsubscribed.
Definition at line 46 of file subscriber.h.
管理特定话题上的订阅回调
订阅者应该总是通过NodeHandle::subscribe()
创建,或者从一个已存在的发布者拷贝而来。一旦所有订阅者都消亡,回调函数将不再被调用。一旦所有订阅者都消亡,那么这个之前这些订阅者订阅的topic将不再被订阅。
有关于Subscriber的详细介绍见官方文档 ros::Subscriber Class Reference
ServiceServer
简介
官方介绍:
Manages an service advertisement.
A ServiceServer should always be created through a call to NodeHandle::advertiseService(), or copied from one that was. Once all copies of a specific ServiceServer go out of scope, the service associated with it will be unadvertised and the service callback will stop being called.
Definition at line 45 of file service_server.h.
管理一个广播服务
服务提供者应该总是通过NodeHandle::advertiseService()
创建,或者从一个已存在的服务提供者拷贝而来。一旦所有特定的服务提供者都消亡了,那么相应的服务将不再被提供。
有关于ServiceServer的详细介绍见官方文档 ros::ServiceServer Class Reference
命名空间
命名空间总览
这个页面内,除了命名空间之外,还有所有的class和function,值得一看!
主要的命名空间
- ros::master : Contains functions for querying information from the master
- ros::this_node : Contains functions for querying information about this process’ node
- ros::service : Contains functions for querying information about services
- ros::param : Contains functions for querying the parameter service without the need for a ros::NodeHandle
- ros::names : Contains functions for manipulating ROS graph resource names
参考
- roscpp API概览
- ros::init()
- ros::NodeHandle Class Reference
- ros::Publisher Class Reference
- ros::Subscriber Class Reference
- ros::ServiceServer Class Reference
- ros Namespace Reference
- 中国大学MOOC:机器人操作系统入门
本文转载自: https://blog.youkuaiyun.com/weixin_43361445/article/details/87479126.