Subscribe to messages on a topic
Use 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.
creates a subscriber, sub
= ros2subscriber(node
,topic
)sub
, for a topic with name
topic
that already exists on the ROS 2 network.
node
is the ros2node
object to which this subscriber attaches. The subscriber gets the
topic message type from the network topic list.
Note
The topic must be on the network topic list.
creates a subscriber for a topic and adds that topic to the network topic list. If the
topic list already contains a matching topic, sub
= ros2subscriber(node
,topic
,type
)sub
will be added to
the list of subscribers for that topic. The type
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.
specifies a callback function, sub
= ros2subscriber(node
,topic
,callback
)callback
, and optional data, to run
when the subscriber object handle receives a topic message. Use this syntax if action
needs to be taken on every message, while not blocking code execution.
callback
can be a single function handle or a cell array. The first
element of the cell array needs to 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 will
be passed to the callback function.
Note
The subscriber callback function uses a single input argument, the received
message object, msg
. The function header for the callback is as
follows:
function subCallback(src, msg)
specifies additional options using one or more name-value pair arguments. Specify
name-value pair arguments after all other input arguments.sub
= ros2subscriber(___,Name,Value
)
receive | Wait for new message |
ros2message | Create ROS 2 message structures |