Receive messages from ROS 2 network
ROS Toolbox / ROS 2
The Subscribe block creates a Simulink® non-virtual bus that corresponds to the specified ROS 2 message type. The block uses the node of the Simulink model to create a ROS 2 subscriber for a specific topic. This node is created when the model runs and is deleted when the model terminates. If the model does not have a node, the block creates one.
On each simulation step, the block checks if a new message is available on the specific topic. If a new message is available, the block retrieves the message and converts it to a Simulink bus signal. The Msg port outputs this new message. If a new message is not available, Msg outputs the last received ROS 2 message. If a message has not been received since the start of the simulation, Msg outputs a blank message.
IsNew — New message indicator
New message indicator, returned as a logical. If the output is
1, then a new message was received since the last sample hit. This output can be used to trigger subsystems for processing new messages received in the ROS 2 network.
Msg — ROS 2 message
ROS 2 message, returned as a non-virtual bus. The type of ROS message is specified in the Message type parameter. The Subscribe ROS 2 block outputs blank messages until it receives a message on the topic name you specify. These blank messages allow you to create and test full models before the rest of the network has been setup.
Topic source — Source for specifying topic name
Select from ROS network |
Specify your own
Source for specifying the topic name, specified as one of the following:
Select from ROS network— Use Select to select a topic name. The Topic and Message type parameters are set automatically. You must be connected to a ROS network.
Specify your own— Enter a topic name in Topic and specify its message type in Message type. You must match a topic name exactly.
Topic — Topic name to subscribe to
Topic name to subscribe to, specified as a string. When Topic source is set to
Select from ROS network, use Select to select a topic from the ROS network. You must be connected to a ROS 2 network to get a list of topics. Otherwise, set Topic source to
Specify your own and specify the topic you want.
Message type — ROS 2 message type
ROS 2 message type, specified as a string. Use Select to select from a full list of supported ROS 2 messages. Service message types are not supported and are not included in the list.
Sample time — Interval between outputs
–1 (default) | scalar
Interval between outputs, specified as a scalar. In simulation, the sample time follows simulation time and not actual wall-clock time.
This default value indicates that the block sample time is inherited.
For more information about the inherited sample time type, see Specify Sample Time (Simulink).
History — Mode of storing messages in the queue
Keep last (default) |
Determines the mode of storing messages in the queue. The queued messages will be sent to late-joining subscribers. If the queue fills with messages waiting to be processed, then old messages will be dropped to make room for new. When set to
Keep last, the queue stores the number of messages set by the
Depth property. Otherwise, when set to
Keep all, the queue stores all messages up to the MATLAB® resource limits.
Depth — Size of the message queue
1 (default) | positive scalar
Number of messages stored in the message queue when
History is set to
Reliability — Delivery guarantee of messages
Reliable (default) |
Affects the guarantee of message delivery. If
Reliable, then delivery is guaranteed, but may retry multiple times. If
Best effort, then attempt delivery and do not retry.
Durability — Persistence of messages
Volatile (default) |
Affects persistence of messages in publishers, which allows late-joining subscribers to receive the number of old messages specified by
Volatile, then messages do not persist. If
Transient local, then publisher will retain the most recent messages.
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Introduced in R2019b