Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pick and configure the RMW

Tutorial · Level 1: First contact

Needs first: Add a second node

Status: this chapter runs on nros-board-mps2-an385-freertos: Verified in emulator (daily). From the board support registry; Board Support Levels explains the words.

Every nano-ros image talks to ROS 2 through one RMW, the ROS middleware layer: zenoh, Cyclone DDS or XRCE-DDS. In this chapter you find which one the project uses and the link it reaches ROS 2 over, compare what each backend needs beside the board, and write the image’s link into the bringup, where you would change it. At the end, nros run --dry-run --why says the link comes from your image rather than the board, and ros2 topic echo still receives every message.

This is the part of the workspace the chapter changes:

sensor_ws/
└── src/
    └── sensor_node_bringup/
        └── system.toml  (changed)

Run every step from the root of sensor_ws.

Find the project’s RMW

Open src/sensor_node_bringup/system.toml. Two places in it decide how the project reaches ROS 2 (trimmed):

[system]
name = "sensor_node"
rmw = "zenoh"

[image.freertos]
board = "mps2-an385-freertos"

rmw = "zenoh" in [system] picks the middleware for every image of the bringup. The [image.freertos] table would say where the image finds its zenoh router, in a locator line, but it has none, so the board’s own applies: TCP to 10.0.2.2:7447, your host as QEMU’s user network sees it. Chapter 2’s --dry-run --why showed that link with the provenance board.

The RMW is chosen when the image is built, not when it starts. ROS 2 reads RMW_IMPLEMENTATION when a process starts and loads that middleware’s shared library; a microcontroller has no shared libraries to load and no environment to read. So the build links the chosen backend into the image, and only that one: an image built for zenoh contains no DDS code at all. Changing the RMW means editing the rmw line and building again.

Compare the three backends

All three carry topics, services and actions between a nano-ros image and ROS 2. They differ in what has to run beside your image, and in what the image needs from its board:

zenohCyclone DDSXRCE-DDS
Runs beside the imageROS 2’s zenoh router, rmw_zenohdnothingthe Micro XRCE-DDS Agent, MicroXRCEAgent
ROS 2 sidermw_zenoh_cpprmw_cyclonedds_cpp, directlyany DDS RMW, through the agent
Linksserial, TCP, UDPUDP, multicast discoveryserial, UDP, TCP
Board needs (planning numbers)about 16 KB RAM, 100 KB flasha heap, about 32 KB RAM, 150 KB flash, an IP stack with IGMPabout 3 KB RAM, 75 KB flash
rmw ="zenoh""cyclonedds""xrce"

The RAM and flash figures are order-of-magnitude planning numbers, not measurements of your image; chapter 8 measures this project’s. The RMW feature matrix lists what each backend supports, generated from the backends’ sources: in short, all three do publish and subscribe, services and status events, and they differ in the less common features, such as graph introspection and zero-copy loans.

How to choose:

  • zenoh when the board is small, when its only link to the host is a serial port, or when the ROS 2 side already runs rmw_zenoh_cpp. It needs the router, and the router comes from ROS 2, so a machine with ROS 2 installed already has it.
  • Cyclone DDS when the target has an operating system with a network stack, such as the Linux host or an RTOS board with Ethernet, and you want nothing running beside it. A stock ROS 2 node using rmw_cyclonedds_cpp sees it directly, with the same wire protocol and the same discovery.
  • XRCE-DDS when RAM is the tightest constraint, or when the device joins a micro-ROS or DDS deployment that already runs an agent. It is not heap-free: each allocation goes through nros_platform_alloc, once per session or entity, never per message. The device does no discovery of its own; while the agent is down, the device has no connectivity, and an agent that restarts makes the device open its session again.

On this board the choice is made for you: the board support registry verifies the MPS2-AN385 images with zenoh, which is why chapter 1 provisioned --rmw zenoh. Chapter 9 runs the same project on the Linux host, where Cyclone DDS runs too.

How nano-ros differs from ROS 2 explains how each backend reaches the ROS 2 graph, and what happens when the router, the agent or a peer goes away.

A board’s locator is a default. Write it into the image when the image should not depend on it: a router on another machine, another port, or a link the board does not default to. Add the board’s own locator to [image.freertos], so the image states it:

In src/sensor_node_bringup/system.toml, change the marked lines
 [image.freertos]
 board = "mps2-an385-freertos"
+locator = "tcp/10.0.2.2:7447"

A zenoh locator is <link>/<address>: tcp/10.0.2.2:7447 is TCP to port 7447 of the host, the zenoh router’s usual port. A router on another machine of your network would be tcp/<its address>:7447; a board whose only link is a UART names its serial port, as in Use a serial link. Nothing else changes: the node packages do not name a link, and the board’s description already says which network devices it has and how its network stack is built.

Ask nros where the link comes from now:

nros run --dry-run --why
[link]
kind = "tcp"
locator = "tcp/10.0.2.2:7447"
rmw = "zenoh"
host_peer = "zenoh-router"

[provenance]
image = "rung 4, no target named: the workspace's only image"
link = "image"

The plan is the same as chapter 2’s, but its provenance is image: the locator now comes from your bringup, and a change to that line changes the run.

Run it

source /opt/ros/humble/setup.bash
nros run
nros run: starting the zenoh router on tcp/127.0.0.1:7447
I heard: [Hello World: 1]

The image is rebuilt with the new line, then boots. A Rust project’s output, trimmed:

nros run: starting the zenoh router on tcp/127.0.0.1:7447

Initializing LAN9118 + lwIP...
  MAC: 02:00:00:00:00:00
  IP:  10.0.2.15
Network ready.

[INFO] sensor_node: [    3.127668] Publishing: 'Hello World: 1'
[INFO] monitor_node: [    3.133778] I heard: [Hello World: 1]

nros run read the locator and started the router listening on TCP port 7447 of your host’s loopback address, because QEMU’s user network delivers 10.0.2.2 to your host’s loopback. It starts the router only when the image needs one and the address is this host’s. With --router none it starts nothing, and with --router tcp/<host>:<port> it uses a router you already run elsewhere.

See it in ROS 2

Leave nros run going. The ROS 2 side has not changed: it reaches the same router. In a second terminal, the same command as in chapter 1:

source /opt/ros/humble/setup.bash
export RMW_IMPLEMENTATION=rmw_zenoh_cpp
ros2 topic echo /chatter std_msgs/msg/String --qos-reliability best_effort
data: 'Hello World: …'

Press Ctrl-C in both terminals when you have seen enough.

What switching the RMW takes

On a board that supports more than one backend, switching is two edits and a provisioning step. This board’s image stays on zenoh, so read this section rather than run it; Switch the RMW per image is the full procedure.

  1. Provision the backend for the board: nros setup <board> --rmw cyclonedds (or --rmw xrce) fetches what that backend needs, as chapter 1’s --rmw zenoh did for zenoh.

  2. Change the rmw line. In [system], it changes every image of the bringup. Set inside one [image.*] table, it changes that image only, so one bringup can build its board image with zenoh and its host image with Cyclone DDS:

    [image.linux]
    board = "linux"
    rmw = "cyclonedds"
    
  3. Change the link to the backend’s. Cyclone DDS needs no locator: it finds its peers by multicast on the network, and its domain is [system] domain_id. XRCE-DDS dials its agent, so its locator names the agent’s address and port, such as udp/10.0.2.2:2018, and nros run starts MicroXRCEAgent on that port the way it starts the zenoh router.

Then nros run again. The node packages do not change for any of the three: the same code publishes over each, because the backend sits below the client library the nodes call.

What just happened

You configured the project’s RMW in use: the backend linked into the image, and the link it uses to reach whatever runs beside it.

Three facts carry over to every board and every later chapter:

  • The RMW is a build-time choice, per image. The rmw line picks it, in [system] for every image or in one [image.*] for that image. The build links that backend alone. There is no RMW_IMPLEMENTATION switch on an embedded target, and the image cannot change its backend after it is built.
  • Each backend has its own peer beside the image. zenoh needs a router, XRCE-DDS an agent, and Cyclone DDS nothing but a network that carries multicast. nros run starts the router or the agent when the image’s link points at your host, which is why the project has never needed you to start one by hand.
  • The link is configuration, not code. The board states a default link, and one locator line in an image overrides it: another address, another port, or a serial port on a board whose only link is a UART. A network needs addresses, which the board’s description and the image supply; a serial link needs a port and a baud rate. WiFi is deferred: no chapter of this book uses it yet.

Next

Design background

Choosing the backend at build time follows RFC-0031; using ROS 2’s own router follows RFC-0075; nros run starting it follows RFC-0118 D8. RMW use before RMW implementation is RFC-0124 D5.