主要内容

ros2subscriber

Subscribe to messages on a topic

描述

采用the ros2subscriber to receive messages on a topic. When ROS 2 nodes publish messages on that topic, MATLAB®will receive those message through this subscriber.

创建

描述

example

= ros2subscriber(节点,话题)创建一个订阅者,,对于名称的主题话题已经存在于ROS 2网络上。节点是个ros2nodeobject to which this subscriber attaches. The subscriber gets the topic message type from the network topic list.

Note

主题必须位于网络主题列表中。

example

= ros2subscriber(节点,话题,类型)creates a subscriber for a topic and adds that topic to the network topic list. If the topic list already contains a matching topic,will be added to the list of subscribers for that topic. The类型must be the same as the topic. Use this syntax to avoid errors when it is possible for the subscriber to subscribe to a topic before a topic has been added to the network.

example

= ros2subscriber(节点,话题,打回来)specifies a callback function,打回来和订户对象句柄接收到主题消息时,可以运行的可选数据。如果需要在每条消息上拍摄操作,则使用此语法,同时不会阻止代码执行。打回来可以是单个函数句柄或单元格数组。单元阵列的第一个元素需要是函数句柄或包含函数名称的字符串。单元格阵列的剩余元素可以是任意用户数据,该用户数据将被传递给回调函数。

Note

订阅者回调函数使用单个输入参数,收到的消息对象,味精。回调的函数标题如下:

functionsubcallback(src,msg)
You pass additional parameters to the callback function by including both the callback function and the parameters as elements of a cell array when setting the callback.

example

= ros2subscriber(节点,话题,类型,打回来)specifies a callback function打回来,并订阅具有指定名称的主题话题和message type类型

= ros2subscriber(___,姓名,Value)specifies additional options using one or more name-value pair arguments. Specify name-value pair arguments after all other input arguments.

Input Arguments

展开全部

Aros2nodeobject on the network.

已发布主题的名称,指定为字符串标量或字符向量。如果该主题不存在,则对象基于关联的消息类型创建主题。

例子:“/喋喋不休”

此属性是只读的。

订阅邮件的消息类型。

例子:"std_msgs/String"

姓名-Value Arguments

指定可选的参数成对name1 = value1,...,namen = valuen, 在哪里姓名是个argument name and价值是相应的价值。名称 - 值参数必须在其他参数之后出现,但对的顺序无关紧要。

在R2021A之前,使用逗号分隔每个名称和值,并附包姓名在报价中。

确定在队列中存储消息的模式。如果队列填写等待处理的消息,则将删除旧消息以为新的空间腾出空间。如果设置为"keeplast",队列存储由...设置的消息数Depth财产。如果设置为"keepall",队列将所有邮件存储到MATLAB资源限制。

Number of messages stored in the message queue when历史被设定为"keeplast"

例子:42.

数据类型:双倍的

要求邮件交付保证。如果“可靠的”然后保证交付,但可以多次重试。如果"besteffort", then attempt delivery and do not retry.

例子:“可靠的”

数据类型:char|细绳

要求在连接发布者中持久性持久性,这允许延迟加入用户收到指定的旧消息数量Depth。如果"volatile",然后不需要消息持久性,并且在订阅者加入网络时,没有请求消息。如果“transientlocal”,然后订阅者将需要发布者持续消息,并将请求指定的消息数Depth

例子:"volatile"

数据类型:char|细绳

特性

展开全部

已发布主题的名称,指定为字符串标量或字符向量。如果该主题不存在,则对象基于关联的消息类型创建主题。

例子:“/喋喋不休”

数据类型:char

此属性是只读的。

订阅邮件的消息类型。

例子:"std_msgs/String"

数据类型:char|细绳

此属性是只读的。

最近收到的ROS 2消息,指定为a信息对象句柄, received.

此属性是只读的。

订户回调的回调函数。

Note

订阅者回调函数使用单个输入参数,收到的消息对象,味精。回调的函数标题如下:

functionsubcallback(msg)
You pass additional parameters to the callback function by including both the callback function and the parameters as elements of a cell array when setting the callback.

此属性是只读的。

确定在队列中存储消息的模式。如果队列填写等待处理的消息,则将删除旧消息以为新的空间腾出空间。设置到"keeplast",队列存储由...设置的消息数Depth财产。Otherwise, when set to"keepall",队列将所有邮件存储到MATLAB资源限制。

例子:"keeplast"

数据类型:char|细绳

此属性是只读的。

Number of messages stored in the message queue when历史被设定为"keeplast"

例子:42.

数据类型:双倍的

此属性是只读的。

要求邮件交付保证。如果“可靠的”然后保证交付,但可以多次重试。如果"besteffort", then attempt delivery and do not retry.

例子:“可靠的”

数据类型:char|细绳

此属性是只读的。

要求在连接发布者中持久性持久性,这允许延迟加入用户收到指定的旧消息数量Depth。如果"volatile",然后不需要消息持久性,并且在订阅者加入网络时,没有请求消息。如果“transientlocal”,然后订阅者将需要发布者持续消息,并将请求指定的消息数Depth

例子:"volatile"

数据类型:char|细绳

Object Functions

收到 等待新消息
ros2message 创建ROS 2消息结构

例子

collapse all

ROS 2节点以交换数据的主要机制是发送和接收messages。消息在a上传输话题和每个主题都有一个惟一名称2 netw ROSork. If a node wants to share information, it must use a发行商to send data to a topic. A node that wants to receive that information must use a订户for that same topic. Besides its unique name, each topic also has amessage type,它确定允许在特定主题中传输的消息类型。

该发布商用户通信具有以下特征:

  • 主题用于多对多通信。多个发布商可以向同一主题发送消息,并且多个订阅者可以接收它们。

  • 出版商和订阅者通过主题解耦,可以在任何顺序创建和销毁。即使没有活动订阅者,也可以发布到一个主题的消息。

此示例显示如何在ROS 2网络中发布和订阅主题。它还显示了如何:

  • 等到收到新消息,或者

  • 采用打回来s to process new messages in the background

先决条件:Get Started with ROS 2,Connect to a ROS 2 Network

Subscribe and Wait for Messages

使用多个发布商和订阅者创建示例ROS 2网络。

examplehelperros2createsamplenetwork

采用ROS2主题列表to see which topics are available.

ros2话题list
/ parameter_events / pose / rosout / scan

假设你想订阅/扫描话题。采用ros2subscriberto subscribe to the/扫描话题。使用订阅者指定节点的名称。如果主题已存在于ROS 2网络中,ros2subscriber自动检测其消息类型,因此您无需指定它。

侦探= ros2node(“/检测”);暂停(2)激光= ros2subscriber(detectNode,"/scan");暂停(2)

采用收到等待一条新留言。指定10秒的超时。输出scanData包含收到的消息数据。status指示是否已成功收到消息Statustext.provides additional information about thestatus

[Scandata,Status,Statustext] =接收(LaserSub,10);

您现在可以删除订阅者激光和与它相关联的节点。

清除激光清除侦探

使用回调函数订阅

Instead of using收到to get data, you can specify a function to be called when a new message is received. This allows other MATLAB code to execute while the subscriber is waiting for new messages. Callbacks are essential if you want to use multiple subscribers.

订阅/姿势话题, using the callback functionexampleHelperROS2PoseCallback,它将收到的消息作为输入。在主工作空间和回调函数之间共享数据的一种方法是使用全局变量。定义两个全局变量p东方

CONTROLNODE = ROS2NODE("/base_station");姿态= ros2subscriber(controlNode,"/pose",@ examplehelperros2posecallball);全球的p全球的东方

全局变量p东方are assigned in theexampleHelperROS2PoseCallback在接收到新的消息数据时功能/姿势话题。

functionexamplehelperros2posecallback(消息)% Declare global variables to store position and orientation全球的p全球的东方%从ROS消息中提取位置和方向并分配%数据到全局变量。pos = [message.linear.x message.linear.y message.linear.z];Orient = [message.angular.x message.angular.y message.angular.z];end

等待网络发布另一个时刻/姿势信息。显示更新的值。

暂停(3)DISP(POS)
-0.0409 0.0076 0.0183
DISP(东方)
0.0047 -0.0074 0.0144

如果you type inp东方在命令行中有几次,您可以看到持续更新值。

通过清除订户变量来停止姿势用户

清除姿态清除controlNode

Note:除了使用全局之外,还有其他方法可以从回调函数中提取来自回调函数的信息。例如,您可以将句柄对象作为回调函数的其他参数传递给回调函数。看看回调定义documentation for more information about defining callback functions.

发布消息

Create a publisher that sends ROS 2 string messages to the/chatter话题。

chatterPub = ros2publisher(node_1,“/喋喋不休”,"std_msgs/String");

创建和填充ROS 2消息以发送到/chatter话题。

chattermsg = ros2message(Chatterpub);chattermsg.data ='你好世界';

采用ROS2主题列表验证/chatterROS 2网络中有主题可用。

ros2话题list
/ chatter / parameter_events / pose / rosout / scan

Define a subscriber for the/chatter话题。exampleHelperROS2ChatterCallback收到新消息时被调用,并在消息中显示字符串内容。

chatterSub = ros2subscriber(node_2,“/喋喋不休”,@ examplehelperros2chattercallback)
chatterSub = ros2subscriber with properties: TopicName: '/chatter' LatestMessage: [] MessageType: 'std_msgs/String' NewMessageFcn: @exampleHelperROS2ChatterCallback History: 'keeplast' Depth: 10 Reliability: 'reliable' Durability: 'volatile'

发布消息/chatter话题。观察字符串由订户回调显示。

发送(Chatterpub,Chattermsg)暂停(3)
ans ='hello world'

TheexampleHelperROS2ChatterCallbackfunction was called when the subscriber received the string message.

断开ROS 2网络连接

从ROS 2网络删除样本节点,发布者和订阅者。还清除全局变量p东方

清除全球的p东方清除

Next Steps

扩展能力

Version History

Introduced in R2019b