roscpp Overview---Publishers and Subscribers

1、Publishing to a Topic 发布话题

Creating a handle to publish messages to a topic is done using the ros::NodeHandle class(使用ROS :: NodeHandle类创建将消息发布到主题的句柄)

The NodeHandle::advertise() methods are used to create a ros::Publisher which is used to publish on a topic (NodeHandle :: advertise()函数用于创建ROS ::发布者,该发布者用于在主题上发布())
例子:

   1 ros::Publisher pub = nh.advertise<std_msgs::String>("topic_name", 5);//定义个话题,应该是发布5次
   2 std_msgs::String str; //定一个字符串类型,名字叫str
   3 str.data = "hello world"; //输入一个 “hello world”
   4 pub.publish(str); //通过publisher函数中的publish将“hello world”发布出去

也可以通过创建指针来发布,如下:

   ros::Publisher pub = nh.advertise<std_msgs::String>("topic_name", 5);
   std_msgs::StringPtr str(new std_msgs::String);//定义了字符串类型的指针(不太确定)
   str->data = "hello world";//
   pub.publish(str);

注:ros::Publisher advertise()的类模板用法。指定要在主题上发布的消息类型

 1 template<class M>
 2 ros::Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false);

参数描述:
topic [required]
This is the topic to publish on.(发布的话题)
queue_size [required]
This is the size of the outgoing message queue. If you are publishing faster than roscpp can send the messages over the wire, roscpp will start dropping old messages. A value of 0 here means an infinite queue, which can be dangerous. See the rospy documentation on choosing a good queue_size for more information.(传出消息队列的大小,我理解的就是要把发布的话题发几遍。)
latch [optional]
Enables “latching” on a connection. When a connection is latched, the last message published is saved and automatically sent to any future subscribers that connect. This is useful for slow-changing to static data like a map. Note that if there are multiple publishers on the same topic, instantiated in the same node, then only the last published message from that node will be sent, as opposed to the last published message from each publisher on that single topic.

2、Subscribing to a Topic 订阅话题

Subscribing to a topic is also done using the ros::NodeHandle class (covered in more detail in the NodeHandles overview). (还使用ROS :: NodeHandle类(在NodeHandles概述中详细介绍)订阅主题)

void callback(const std_msgs::StringConstPtr& str)
{
...
}

...
ros::Subscriber sub = nh.subscribe("my_topic", 1, callback);
//自我感觉是通过的格式,一个callback函数,一个订阅的模板类。

对ros::Subscriber::subscriber 模板类进行传入参数分析:

template<class M>
ros::Subscriber subscribe(const std::string& topic, 
						  uint32_t queue_size,
						 <callback, which may involve multiple arguments>, 
		const ros::TransportHints& transport_hints = ros::TransportHints());

This is a template argument specifying the message type to be published on the topic. For most versions of this subscribe().(这是一个模板参数,指定要在主题上发布的消息类型。对于此订阅的大多数版本()。
topic
The topic to subscribe to 订阅的话题名称
queue_size
This is the incoming message queue size roscpp will use for your callback. If messages are arriving too fast and you are unable to keep up, roscpp will start throwing away messages. A value of 0 here means an infinite queue, which can be dangerous.(传入的消息队列长度)

Depending on the version of subscribe() you’re using, this may be any of a few things. The most common is a class method pointer and a pointer to the instance of the class. This is explained in more detail later.(回调函数,这个是主要的)

transport_hints

The transport hints allow you to specify hints to roscpp’s transport layer. This lets you specify things like preferring a UDP transport, using tcp nodelay, etc. This is explained in more detail later.

Every generated message provides typedefs for the shared pointer type, so you can also use, for example:(每个生成的消息都为共享指针类型提供类型,因此您也可以使用:例如)

void callback(const std_msgs::StringConstPtr&);
void callback(const std_msgs::String::ConstPtr&);

3、Callback Types 类型

 1 void callback(const std_msgs::StringConstPtr& str)
   2 {
   3 ...
   4 }
   5 
   6 ...
   7 ros::Subscriber sub = nh.subscribe("my_topic", 1, callback);
 1 void Foo::callback(const std_msgs::StringConstPtr& message)
   2 {
   3 }
   4 
   5 ...
   6 Foo foo_object;
   7 ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);
 1 class Foo
   2 {
   3 public:
   4   void operator()(const std_msgs::StringConstPtr& message)
   5   {
   6   }
   7 };
   8  ros::Subscriber sub = nh.subscribe<std_msgs::String>("my_topic", 1, Foo());

参考链接:
http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值