Subscribe
R2026bReceive messages from ROS 2 network by polling at each time step or processing immediately on arrival
Libraries:
ROS Toolbox /
ROS 2
Description
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.
The block supports two message processing modes, which you can select using the Process messages parameter. In synchronous polling mode, the block checks for new messages at each sample time step and outputs them for processing. In asynchronous event-driven mode, the block processes messages immediately upon arrival by triggering an event signal. Depending on the parameter setting, the block uses one of these modes. The IsNew output port is used for polling mode execution. The block supports asynchronous mode only for code generation.
You can assign a ROS 2 callback group to the subscriber using the Callback group type parameter to control how the ROS 2 executor schedules the subscriber callback relative to other callbacks on the same node. This is useful for preventing deadlocks, enabling concurrent callback execution, improving throughput for high-frequency message processing, reducing jitter, and aligning Simulink simulation behavior with generated C++ code execution semantics.
Quality of Service (QoS) events related to different QoS policies trigger default warnings for the Subscribe block. For more information on the different QoS events and the triggered warnings respectively, see QoS Events section in Manage Quality of Service Policies in ROS 2 example.
In generated code, ROS 2 nodes created by this block can use loaned
messages for subscribing. This enables zero-copy data transfer through shared
memory and reduces processing overhead and improves throughput for large or frequent messages.
ROS 2 automatically applies loaned message support in both local and remote deployments, but
you can toggle with the environment variable ROS_DISABLE_LOANED_MESSAGES by
setting it to 0 for enabling and 1 for disabling.
Currently, only selected RMW implementations support loaned messages, such as Fast DDS. To
achieve true zero-copy communication, you must enable shared memory in the middleware by
configuring the DEFAULT_FASTRTPS_PROFILES.xml file as shown in this
code:
<?xml version="1.0" encoding="UTF-8" ?> <profiles xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles"> <data_writer profile_name="default publisher profile" is_default_profile="true"> <qos> <publishMode> <kind>ASYNCHRONOUS</kind> </publishMode> <data_sharing> <kind>AUTOMATIC</kind> </data_sharing> </qos> <historyMemoryPolicy>PREALLOCATED_WITH_REALLOC</historyMemoryPolicy> </data_writer> <data_reader profile_name="default subscription profile" is_default_profile="true"> <qos> <data_sharing> <kind>AUTOMATIC</kind> </data_sharing> </qos> <historyMemoryPolicy>PREALLOCATED_WITH_REALLOC</historyMemoryPolicy> </data_reader> </profiles>
If a message type does not support loaning (for example, variable-length arrays or strings), the system issues a runtime warning and falls back to regular message handling.