Subscribe to messages on a topic
Use rossubscriber
to create a ROS subscriber for receiving
messages on the ROS network. To send messages, use rospublisher
. 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.
You can create a Subscriber
object by using the
rossubscriber
function, or by calling
ros.Subscriber
:
rossubscriber
works only with
the global node using rosinit
. It does not require a
node object handle as an argument.
ros.Subscriber
works with additional nodes that are
created using ros.Node
. It requires a node object handle
as the first argument.
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.
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.
receive | Wait for new ROS message |
rosmessage | Create ROS messages |