roscpp中常用的函数、类以及命名空间

roscpp中常用的函数、类以及命名空间

思维导图

图中列出了部分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 
				) 	

除此之外,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 
	) 		

除此之外,该函数还有两个版本,详见 ros::NodeHandle::advertise

参数详解
  • 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

命名空间

命名空间总览

ros Namespace 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

参考

  1. roscpp API概览
  2. ros::init()
  3. ros::NodeHandle Class Reference
  4. ros::Publisher Class Reference
  5. ros::Subscriber Class Reference
  6. ros::ServiceServer Class Reference
  7. ros Namespace Reference
  8. 中国大学MOOC:机器人操作系统入门

更多文章欢迎访问我的博客https://www.chen517.xyz

  • 10
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值