CodeTheRobot

ROS 2 Lyrical Luth · Lesson 02 of 86

DDS basics: CycloneDDS and FastDDS

The middleware under ROS 2, discovery, domains and choosing an RMW implementation.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain dds basics: cyclonedds and fastdds in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

DDS (Data Distribution Service) is the publish-subscribe standard ROS 2 uses to move data. ROS 2 hides it behind the RMW (ROS middleware) layer, so your code does not change when you switch implementations such as Eclipse Cyclone DDS (rmw_cyclonedds_cpp) or eProsima Fast DDS (rmw_fastrtps_cpp).

Nodes find each other automatically (discovery) if they share a ROS_DOMAIN_ID (0–101). Different domains are isolated, which is how several robots or students share one network without interfering. Choose the implementation with RMW_IMPLEMENTATION and check that every machine uses the same one.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the dds basics: cyclonedds and fastdds example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

printenv RMW_IMPLEMENTATION
# Show which middleware is forced (empty means the distribution default).
export ROS_DOMAIN_ID=42
# Put this terminal in domain 42; nodes in other domains cannot see it.
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
# Use Cyclone DDS for programs started from this terminal.
ros2 run demo_nodes_cpp talker &
# Start a talker in the background in the chosen domain.
ros2 topic list
# The /chatter topic appears, because discovery worked inside domain 42.
ROS_DOMAIN_ID=7 ros2 topic list
# In another domain the topic is invisible: domains isolate traffic.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

/topicpublishersubscribersubscribermessages delivered: 0MESSAGES BETWEEN NODES

Demo for DDS basics: CycloneDDS and FastDDS. Expected result: The listener in domain 10 prints messages; the listener in domain 11 prints nothing

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# Terminal A
export ROS_DOMAIN_ID=10
ros2 run demo_nodes_cpp talker
# Terminal B (same domain as the talker)
# TODO: put this terminal in domain 10, then start the listener
# Terminal C (different domain)
export ROS_DOMAIN_ID=11
ros2 run demo_nodes_cpp listener

Expected output

The listener in domain 10 prints messages; the listener in domain 11 prints nothing
Stuck? Show the full solution (try it yourself first)
# Terminal A
export ROS_DOMAIN_ID=10
ros2 run demo_nodes_cpp talker
# Terminal B (same domain as the talker)
export ROS_DOMAIN_ID=10
ros2 run demo_nodes_cpp listener
# Terminal C (different domain)
export ROS_DOMAIN_ID=11
ros2 run demo_nodes_cpp listener

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Run a talker in domain 10 and a listener in domain 10, then a listener in domain 11 and confirm it hears nothing. Repeat with rmw_fastrtps_cpp and rmw_cyclonedds_cpp and note any difference.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.