Subscribe
Receive messages from ROS 2 network
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.
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.
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.