14.1. Configuring Fast DDS in ROS 2

To use specific Fast-DDS features within a ROS 2 application, XML configuration files can be used to configure a wide set of QoS. Please refer to XML profiles to see the whole list of configuration options available in Fast DDS. There are two possibilities for providing Fast DDS with XML configuration files:

  • Recommended: Define the location of the XML configuration file with environment variable FASTRTPS_DEFAULT_PROFILES_FILE (see Environment variables).

    export FASTRTPS_DEFAULT_PROFILES_FILE=<path_to_xml_file>
    
  • Alternative: Create a DEFAULT_FASTRTPS_PROFILES.xml and place it in the same directory as the application executable.

14.1.1. Default profiles

Under ROS 2, the entity creation does not allow for selecting different profiles from the XML. To work around this issue, the profiles can be marked with an attribute is_default_profile="true", so when an entity of that type is created, it will automatically load that profile. The mapping between ROS 2 entities and Fast DDS entities is:

ROS entity

Fast DDS entity Foxy

Fast DDS entity Eloquent & below

Context

Participant

Not DDS direct mapping

Node

Not DDS direct mapping

Participant

Publisher

Publisher

Publisher

Subscription

Subscriber

Subscriber

Service

Publisher + Subscriber

Publisher + Subscriber

Client

Publisher + Subscriber

Publisher + Subscriber

For example, a profile for a ROS 2 Node would be specified as:

XML

<?xml version="1.0" encoding="UTF-8" ?>
<profiles xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles">
    <participant profile_name="participant_profile_ros2" is_default_profile="true">
        <rtps>
            <name>profile_for_ros2_node</name>
        </rtps>
    </participant>
</profiles>

14.1.1.1. Configure Publication Mode and History Memory Policy

By default, rmw_fastrtps sets some of the Fast DDS configurable parameters, ignoring whatever configuration is provided in the XML file. Said parameters, and their default values under ROS 2, are:

Parameter

Description

Default ROS 2 value

MemoryManagementPolicy

Fast DDS preallocates memory for the publisher
and subscriber histories. When those histories fill
up, a reallocation occurs to reserve more memory.

PREALLOCATED_WITH_REALLOC_MEMORY_MODE

PublishModeQosPolicy

User calls to publication method add the messages
in a queue that is managed in a different thread,
meaning that the user thread is available right
after the call to send data.

ASYNCHRONOUS_PUBLISH_MODE

However, it is possible to fully configure Fast DDS (including the history memory policy and the publication mode) using an XML file in combination with an environment variable RMW_FASTRTPS_USE_QOS_FROM_XML.

export FASTRTPS_DEFAULT_PROFILES_FILE=<path_to_xml_file>
export RMW_FASTRTPS_USE_QOS_FROM_XML=1
ros2 run <package> <application>

14.1.1.2. Example

The following example uses the ROS 2 talker/listener demo, configuring Fast DDS to publish synchronously, and to have dynamically allocated publisher and subscriber histories.

  1. Create a XML file ros_example.xml and save it in path/to/xml/

    XML

    <?xml version="1.0" encoding="UTF-8" ?>
    <profiles xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles">
        <participant profile_name="participant_profile_ros2" is_default_profile="true">
            <rtps>
                <name>profile_for_ros2_node</name>
            </rtps>
        </participant>
    
        <publisher profile_name="ros2_publisher_profile" is_default_profile="true">
            <qos>
                <publishMode>
                    <kind>SYNCHRONOUS</kind>
                </publishMode>
            </qos>
            <historyMemoryPolicy>DYNAMIC</historyMemoryPolicy>
        </publisher>
    
        <subscriber profile_name="ros2_subscription_profile" is_default_profile="true">
            <historyMemoryPolicy>DYNAMIC</historyMemoryPolicy>
        </subscriber>
    </profiles>
    
  2. Open one terminal and run:

    export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
    export FASTRTPS_DEFAULT_PROFILES_FILE=path/to/xml/ros_example.xml
    export RMW_FASTRTPS_USE_QOS_FROM_XML=1
    ros2 run demo_nodes_cpp talker
    
  3. Open one terminal and run:

    export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
    export FASTRTPS_DEFAULT_PROFILES_FILE=path/to/xml/ros_example.xml
    export RMW_FASTRTPS_USE_QOS_FROM_XML=1
    ros2 run demo_nodes_cpp listener