rossubscriber
Subscribe to messages on a topic
Description
Use rossubscriber
to create a ROS subscriber for receiving
messages on the ROS network. To send messages, use rospublisher
. The
most reliable method to receive messages continuously as they are published on the ROS
network is to create a callback for the rossubscriber
object. For
more information, see Create A Subscriber That Uses A Callback Function. Alternatively, to wait for
a new ROS message, use the receive
function with your created
subscriber.
The Subscriber
object created by the
rossubscriber
function represents a subscriber on the ROS
network. The Subscriber
object subscribes to an available topic or to
a topic that it creates. This topic has an associated message type. Publishers can send
messages over the network that the Subscriber
object
receives.
Note
In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.
To use message structures now, set the "DataFormat"
name-value
argument to "struct"
. For more information, see ROS Message Structures.
You can create a Subscriber
object by using the
rossubscriber
function, or by calling
ros.Subscriber
:
rossubscriber
works only with the global node usingrosinit
. It does not require a node object handle as an argument.ros.Subscriber
works with additional nodes that are created usingros.Node
. It requires a node object handle as the first argument.
Creation
Syntax
Description
subscribes to a topic with the given sub
= rossubscriber(topicname
)TopicName
.The
topic must already exist on the ROS master
topic list with an established message type. When ROS nodes publish messages
on that topic, MATLAB® receives those messages through this subscriber.
subscribes to a topic that has the specified name,
sub
= rossubscriber(topicname
,msgtype
)TopicName
, and type,
MessageType
. If the topic list on the ROS
master does not include a topic with that
specified name and type, it is added to the topic list. Use this syntax to
avoid errors when subscribing to a topic before a publisher has added the
topic to the topic list on the ROS
master.
specifies a callback function, sub
= rossubscriber(topicname
,callback
)callback
, that runs when
the subscriber object handle receives a topic message. Use this syntax to
avoid the blocking receive function. The callback
function can be a single function handle or a cell array. The first element
of the cell array must be a function handle or a string containing the name
of a function. The remaining elements of the cell array can be arbitrary
user data that is passed to the callback function.
specifies a callback function and subscribes to a topic that has the
specified name, sub
= rossubscriber(topicname
, msgtype
,callback
)TopicName
, and type,
MessageType
.
provides additional options specified by one or more
sub
= rossubscriber(___,Name,Value)Name,Value
pair arguments using any of the arguments
from previous syntaxes. Name
is the property name and
Value
is the corresponding value.
uses message structures instead of objects. For more information, see ROS Message Structuressub
= rossubscriber(___,"DataFormat","struct")
sub = ros.Subscriber(
subscribes to a topic with name, node
,topicname
)TopicName
. The
node
is the ros.Node
object
handle that this publisher attaches to.
sub = ros.Subscriber(
specifies the message type, node
,topicname
,msgtype
)MessageType
, of the topic.
If a topic with the same name exists with a different message type,
MATLAB creates a new topic with the given message type.
sub = ros.Subscriber(
specifies a callback function, and optional data, to run when the subscriber
object receives a topic message. See node
,topicname
,callback
)NewMessageFcn
for
more information about the callback function.
sub = ros.Subscriber(
specifies the topic name, message type, and callback function for the
subscriber.node
,topicname
,type
,callback
)
sub = ros.Subscriber(___,"BufferSize",
specifies the queue size in value
)BufferSize
for incoming
messages. You can use any combination of previous inputs with this
syntax.
uses message structures instead of objects. For more information, see ROS Message Structuressub
= ros.Subscriber(___,"DataFormat","struct")
Properties
Object Functions
receive | Wait for new ROS message |
rosmessage | Create ROS messages |