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

Talk to ROS 2

Tutorial · Level 2: Building real applications

Needs first: Same project, another target

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 chapter so far ended with a ROS 2 command reaching the board. This one looks at what crosses the wire, in both directions, with the project’s own message type. You build tutorial_msgs for ROS 2 with colcon, read the board’s readings with ros2 topic echo, ask the ROS 2 graph what QoS the board offers, and publish a reading from ROS 2 that the board’s listener hears:

[INFO] monitor_node: [    9.159741] I heard: reading 1000 (25000 mC)

At the end you know which parts of ROS 2 a nano-ros image matches exactly, and the few places it does not, each of them a choice made for a microcontroller, whose memory is fixed when its image is built.

Run every step from the root of sensor_ws.

Build the message package for ROS 2

A ROS 2 program can only read a message whose type it knows. std_msgs came with ROS 2; tutorial_msgs is yours, so the ROS 2 side needs it built. It is a stock ROS 2 package, which is why chapter 5 gave it a CMakeLists.txt that nano-ros never runs. Build it with colcon, into a directory under build/, so it stays out of your sources. The ROS 2 development tools from Install include colcon; without them, this line adds it:

sudo apt-get install -y python3-colcon-common-extensions
source /opt/ros/humble/setup.bash
colcon build --base-paths src/tutorial_msgs \
    --build-base build/colcon --install-base build/colcon/install
Starting >>> tutorial_msgs
Finished <<< tutorial_msgs [3.80s]

Summary: 1 package finished [7.31s]

The same .msg file now has two builds: the one nros generates for the board, and the one rosidl generated for ROS 2. They agree on the type’s name, tutorial_msgs/msg/Reading, and on its bytes on the wire.

Read the board from ROS 2

Start the board’s image in one terminal, and leave it running:

source /opt/ros/humble/setup.bash
nros run freertos
I heard: reading 2

In a second terminal, source ROS 2 and the overlay colcon built, select the zenoh RMW, and read one message. --no-daemon asks the router directly, as in chapter 7: a ros2 daemon left over from an earlier chapter still holds a session to a router that nros run has since replaced, and it answers with !rclpy.ok().

source /opt/ros/humble/setup.bash
source build/colcon/install/setup.bash
export RMW_IMPLEMENTATION=rmw_zenoh_cpp
ros2 topic echo --no-daemon /sensor/reading tutorial_msgs/msg/Reading --once
sequence: 11
temperature_mc: 21100
---

The board publishes on the name chapter 7’s remap gave it, with the type chapter 5 defined, and ROS 2 reads both fields by name. Without the overlay, the same command refuses the type, because ROS 2 has no code for a message it has never built.

Ask the graph what the board offers

The same shell can ask what the board’s endpoints look like from the ROS 2 side:

ros2 topic info -v --no-daemon /sensor/reading
Type: tutorial_msgs/msg/Reading
Publisher count: 1

Node name: sensor_node
Node namespace: /
Topic type: tutorial_msgs/msg/Reading
Endpoint type: PUBLISHER
QoS profile:
  Reliability: RELIABLE
  History (Depth): KEEP_LAST (1)
  Durability: VOLATILE
  ...

Subscription count: 1

Node name: monitor_node
Node namespace: /
Topic type: tutorial_msgs/msg/Reading
Endpoint type: SUBSCRIPTION
QoS profile:
  Reliability: RELIABLE
  History (Depth): KEEP_LAST (1)
  Durability: VOLATILE
  ...

Both endpoints are on the graph with their node names, in the root namespace, exactly as two ROS 2 nodes on your machine would be. Their QoS is the one the board actually granted, not the one the code asked for: the code asked for a history of 10, the contract of chapter 8 reserved 1, and the board advertises 1. A ROS 2 tool that sizes its own queue from what a publisher offers sees the truth about the board.

Compare the plan with the graph

nros can say which nodes and topics an image will create before it runs. Ask it about the image that is running now, then ask ROS 2 what it sees:

nros image show freertos --nodes --topics
freertos
  image `freertos` of bringup `sensor_node_bringup`
  ...

PLANNED NODE         PACKAGE             RUNS
/sensor_node         sensor_node         sensor_node
/monitor_node        monitor_node        monitor_node
/server_node         server_node         server_node
/client_node         client_node         client_node
/action_server_node  action_server_node  action_server_node
/action_client_node  action_client_node  action_client_node

PLANNED          KIND     TYPE                                 FROM                 TO
/sensor/reading  topic    tutorial_msgs/msg/Reading            /sensor_node         /monitor_node
/add_two_ints    service  example_interfaces/srv/AddTwoInts    /server_node         /client_node
/fibonacci       action   example_interfaces/action/Fibonacci  /action_server_node  /action_client_node

FROM is the publisher or server side, TO the subscriber or client side.
freertos
  image `freertos` of bringup `sensor_node_bringup`
  ...

PLANNED NODE         PACKAGE             RUNS
/sensor_node         sensor_node         sensor_node
/monitor_node        monitor_node        monitor_node
/server_node         server_node         server_node
/client_node         client_node         client_node
/action_server_node  action_server_node  action_server_node
/action_client_node  action_client_node  action_client_node

PLANNED            KIND     TYPE                                 FROM                 TO
/parameter_events  topic    rcl_interfaces/msg/ParameterEvent    image freertos       -
/sensor/reading    topic    tutorial_msgs/msg/Reading            /sensor_node         /monitor_node
/add_two_ints      service  example_interfaces/srv/AddTwoInts    /server_node         /client_node
/fibonacci         action   example_interfaces/action/Fibonacci  /action_server_node  /action_client_node

FROM is the publisher or server side, TO the subscriber or client side.
`image <name>` marks an endpoint the image's sizing descriptor records without naming its node.
freertos
  image `freertos` of bringup `sensor_node_bringup`
  ...

PLANNED NODE         PACKAGE             RUNS
/sensor_node         sensor_node         sensor_node
/monitor_node        monitor_node        monitor_node
/server_node         server_node         server_node
/client_node         client_node         client_node
/action_server_node  action_server_node  action_server_node
/action_client_node  action_client_node  action_client_node

PLANNED          KIND     TYPE                                 FROM                 TO
/sensor/reading  topic    tutorial_msgs/msg/Reading            /sensor_node         /monitor_node
/add_two_ints    service  example_interfaces/srv/AddTwoInts    /server_node         /client_node
/fibonacci       action   example_interfaces/action/Fibonacci  /action_server_node  /action_client_node

FROM is the publisher or server side, TO the subscriber or client side.
ros2 node list --no-daemon
ros2 topic list --no-daemon
/action_client_node
/action_server_node
/client_node
/monitor_node
/sensor_node
/server_node
/parameter_events
/rosout
/sensor/reading

The six planned nodes are the six on the graph, and the planned topic /sensor/reading is there too. ROS 2 also lists /parameter_events and /rosout, which nodes create for themselves. The launch plan names neither. A C image’s sizing descriptor records /parameter_events, so its table shows it, with image freertos where the node would be: the descriptor records what the image creates, not which node creates it.

Publish to the board from ROS 2

Messages go the other way just as well. From the second terminal:

ros2 topic pub --times 3 /sensor/reading tutorial_msgs/msg/Reading \
    '{sequence: 1000, temperature_mc: 25000}'
publisher: beginning loop
publishing #1: tutorial_msgs.msg.Reading(sequence=1000, temperature_mc=25000)

The board’s console, in the first terminal, prints the readings beside its own:

[INFO] monitor_node: [    9.159741] I heard: reading 1000 (25000 mC)
[INFO] monitor_node: [   10.632566] I heard: reading 1000 (25000 mC)
[INFO] monitor_node: [   11.626410] I heard: reading 1000 (25000 mC)

The listener does not know, and cannot tell, whether a reading came from the talker in its own image or from a ROS 2 machine. Press Ctrl-C in the first terminal when you are done.

Change it

Move the project to another ROS 2 domain and watch the two sides lose each other. Set domain_id = 42 in the bringup’s [system] table and run the board again: ros2 topic list in a shell with no ROS_DOMAIN_ID no longer shows /sensor/reading, because zenoh keeps each domain’s names apart. Run export ROS_DOMAIN_ID=42 in that shell, and the topic is back. Set the bringup’s domain_id back to 0 when you are done, so the commands of this book keep working. Nothing in a node package changed in either direction: the domain is a fact about the deployment, so it lives in the bringup.

What must match

A nano-ros image and a ROS 2 program reach each other when four things match:

  • The RMW. The board uses zenoh, so the ROS 2 side sets RMW_IMPLEMENTATION=rmw_zenoh_cpp, and both reach the same router, which nros run started. The ros-<distro>-rmw-zenoh-cpp package, which chapter 1 and every zenoh starter ask for, provides both the RMW and the router. With Cyclone DDS on both sides there is no router at all, as the Linux host starter shows.
  • The domain. ROS 2 reads ROS_DOMAIN_ID from its environment, and defaults to 0. A board has no environment: its domain is [system] domain_id in the bringup, fixed when the image is built. The project’s is 0. A board on domain 42 needs domain_id = 42 and a rebuild, and the ROS 2 shell needs ROS_DOMAIN_ID=42.
  • The ROS 2 edition. [system] ros_edition = "humble" builds the image for Humble’s wire format. Iron and Jazzy add type hashes to every name on the wire, so a Humble image and a Jazzy graph discover nothing of each other. A bringup that serves both declares one image per edition, each with its own ros_edition line, and builds the one it needs: nros new <ws> --ros-edition jazzy scaffolds a Jazzy project from the start.
  • The QoS. As in ROS 2, a subscriber that asks for more than a publisher offers receives nothing: a reliable subscriber hears nothing from a best-effort publisher. A best-effort subscriber hears a publisher of either kind, which is why chapter 1’s ros2 topic echo asked for it.

Where nano-ros differs from ROS 2

On the wire, a nano-ros image speaks what rmw_zenoh_cpp speaks: the same key expressions, the same liveliness tokens for discovery, CDR-encoded messages and the same attachment on each one. You configure none of it. What differs is what the image can afford, and the differences are deliberate:

ROS 2nano-ros
a history depth is whatever the code asks forthe board grants what was reserved at build time, and advertises what it granted
an incompatible QoS pair is reported as an eventit fails when the publisher or subscription is created, so you see it at boot
RMW_IMPLEMENTATION and ROS_DOMAIN_ID are read at start-upthe RMW and the domain are built into the image
zenoh nodes may run in peer mode, with no routerevery nano-ros session is a client of a router; peer mode is refused
every node keeps a full graph cachean image on a serial-only link leaves its own graph cache out to save RAM; ROS 2 still sees the image
status events are polled from a waitsetan event callback runs on the executor, like a message callback

None of these change what a ROS 2 program sees of the board, apart from the QoS it offers, which is the truth. Use QoS, status events and discovery shows the status events a node can register for, and How nano-ros differs from ROS 2 and micro-ROS explains each difference in full.

Troubleshooting

  • ros2 topic echo says the message type is invalid: the shell has not sourced build/colcon/install/setup.bash, or colcon did not build tutorial_msgs. Source it after /opt/ros/humble/setup.bash.
  • The topic is not in ros2 topic list: the ROS 2 shell is on another RMW or another domain. Check RMW_IMPLEMENTATION=rmw_zenoh_cpp, and that ROS_DOMAIN_ID is unset or equals the bringup’s domain_id. Check the edition too: a Humble image on a Jazzy graph is invisible.
  • The topic is listed but no messages arrive: the QoS does not match. Ask for best-effort on the ROS 2 side, --qos-reliability best_effort, which hears any publisher.
  • ros2 warns that it is unable to connect to a zenoh router: the board is not running, so no router is either. Start nros run first; a router you run yourself, ros2 run rmw_zenoh_cpp rmw_zenohd, works as well, with nros run --router tcp/127.0.0.1:7447 on the board’s side.

What just happened

You used the boundary between nano-ros and ROS 2 from both sides, with a message type of your own, and saw where nano-ros differs from ROS 2 on the wire: almost nowhere. Names, types, bytes, discovery and the commands you use are ROS 2’s. What differs is decided when the image is built rather than when it starts (the RMW, the domain, the edition, the depth each endpoint keeps), and the image tells the graph the truth about each of them.

That is the end of the Tutorial. The project you started in chapter 1 is a workspace of six node packages and a message package, with one bringup that holds the system’s launch file and contract, built for two targets, and reachable from any ROS 2 machine on the same router. Everything else in this book builds on that shape.

Next

Design background

Speaking rmw_zenoh_cpp’s wire format, with ROS 2’s own router, follows RFC-0075; saying plainly where nano-ros differs is RFC-0124 Principle 13.