Publish
Send messages to ROS 2 network
Libraries:
ROS Toolbox /
ROS 2
Description
The Publish block takes in as its input a Simulink® non-virtual bus that corresponds to the specified ROS 2 message type and publishes it to the ROS 2 network. It uses the node of the Simulink model to create a ROS 2 publisher 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 sample hit, the block converts the Msg input from a Simulink bus signal to a ROS 2 message and publishes it. The block does not distinguish whether the input is a new message but instead publishes it on every sample hit. For simulation, this input is a MATLAB® ROS 2 message. In code generation, it is a C++ ROS 2 message.
Quality of Service (QoS) events related to different QoS policies trigger default warnings for Publish 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 publishing, which enable zero-copy data transfer through shared
memory. This reduces processing overhead and improves throughput for large or frequent
messages. Loaned message support is automatically applied in both local and remote deployments
but can be toggled 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 laoned 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:
<?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.