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

Run a talker on an emulated board

Tutorial · Level 1: First contact

Needs first: Install

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.

By the end of this chapter, an Arm Cortex-M3 board emulated by QEMU is printing Publishing: 'Hello World: 1', 'Hello World: 2' and onward, once a second, and ros2 topic echo on your machine prints the same messages as they arrive. You write no code and edit no configuration to get there: you provision the board, scaffold a project, and run it.

This chapter starts the one project the whole Tutorial grows. Each later chapter adds to the workspace you create here, so keep it.

What you need

  • A Linux host with the install done, so nros is on your PATH.
  • ROS 2 with its zenoh middleware, ros-<distro>-rmw-zenoh-cpp: Humble on Ubuntu 22.04, Jazzy on 24.04 (which edition). ROS 2 gives you the router the board talks to, the ros2 command that shows its messages, and the message definitions the build generates the talker’s code from. If you have no ROS 2 install, the container in Install options has one. The commands here source Humble; on Jazzy, write jazzy where they say humble.

You need no board, no cable and no root access. The emulated board reaches your machine over QEMU’s user network, with no network device to create. WiFi is deferred: no chapter uses it yet.

The board is Arm’s MPS2-AN385, which QEMU models closely. No one buys one: it is the board this book teaches nano-ros on, because it costs nothing and behaves like a real microcontroller. FreeRTOS runs on it, the talker runs as one of its tasks, and lwIP is its network stack. Rust, C and C++ projects all run on this one board, mps2-an385-freertos, so the commands and the output are the same whichever language you pick; only the code differs.

Provision the board

nros setup mps2-an385-freertos --rmw zenoh

This fetches the Arm cross-compiler, a QEMU that emulates the MPS2-AN385, the Rust target for the Cortex-M3, and the FreeRTOS kernel and lwIP sources, into a shared store under ~/.nros. Nothing is installed system-wide, and a second project for the same board reuses what this step fetched. Running it again is quick: it checks what is there and fetches only what is missing.

--rmw zenoh names the middleware the talker uses to reach ROS 2. nano-ros offers three, and zenoh is the one the board support registry verifies on this board, so it is the one this Tutorial uses. nros setup fetches only what that middleware needs on this board. Chapter 4 compares the three and shows what each one needs beside your program.

Scaffold the project

Source ROS 2 first, in the shell you scaffold from. nros new writes the ROS 2 edition the project speaks, and it takes it from the sourced ROS 2; with none sourced, it writes Humble. On Jazzy, source /opt/ros/jazzy/setup.bash instead:

source /opt/ros/humble/setup.bash

Pick the language you will write the project in. Every later chapter shows its code in the same three languages, and the project keeps the language you pick here.

nros new sensor_ws --board mps2-an385-freertos --template talker --lang rust
cd sensor_ws
nros new sensor_ws --board mps2-an385-freertos --template talker --lang c
cd sensor_ws
nros new sensor_ws --board mps2-an385-freertos --template talker --lang cpp
cd sensor_ws

nros new prints what it wrote and the command that runs it. The workspace holds two packages:

sensor_ws/
└── src/
    ├── sensor_node/          (new)  # your node
    └── sensor_node_bringup/  (new)  # what it runs on

sensor_node is the code: a talker that publishes std_msgs/String on /chatter once a second. Its build file is a Cargo.toml in Rust and a CMakeLists.txt in C and C++. sensor_node_bringup says what that code runs on. The scaffold filled it in, and this chapter never needs you to open it. The next chapter opens both.

To keep an untouched copy to read beside the one you change, run the same nros new line again with another workspace name.

Run it

Run it from the same shell, where ROS 2 is sourced. ROS 2 provides the router the board talks to, and its message definitions are what nros run generates the talker’s std_msgs code from. This first run records the ROS 2 prefix in the workspace, so a later build of it needs no sourced shell. The router still does, because it is a ROS 2 program.

nros run sensor_node
Publishing: 'Hello World: 1'
Publishing: 'Hello World: 2'

The first run compiles nano-ros, the FreeRTOS kernel and lwIP for the Cortex-M3 and takes a few minutes; later runs rebuild only what changed and take seconds. Then QEMU boots the program, and the output looks like this (trimmed):

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

========================================
  nros FreeRTOS Platform
========================================

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

Application setup complete — entering spin loop.
[INFO] sensor_node: [    3.122351] Publishing: 'Hello World: 1'
[INFO] sensor_node: [    4.122350] Publishing: 'Hello World: 2'
nros run: starting the zenoh router on tcp/127.0.0.1:7447
Network ready
Publishing: 'Hello World: 1'
Publishing: 'Hello World: 2'
nros run: starting the zenoh router on tcp/127.0.0.1:7447
Network ready
Publishing: 'Hello World: 1'
Publishing: 'Hello World: 2'

Everything after the router line is the board’s console: FreeRTOS brings up the board’s Ethernet controller and takes the address 10.0.2.15, then the talker publishes once a second. In Rust, the number in brackets is the board’s own clock, in seconds since it booted; a C or C++ talker prints its Publishing: line without the [INFO] prefix.

nros run keeps streaming until you press Ctrl-C, which stops QEMU and the router together. Leave it running for the next step.

See it in ROS 2

Leave nros run going. In a second terminal:

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: …'

ros2 topic echo prints one data: line per message, followed by ---:

data: 'Hello World: 7'
---
data: 'Hello World: 8'
---

The command asks for best-effort delivery, which receives from a publisher of either kind, so it works whatever QoS a publisher offers. This talker’s publisher happens to be reliable, as ros2 topic info -v /chatter shows, and a plain ros2 topic echo /chatter hears it too. Chapter 10 explains the QoS rules that decide what a subscriber hears, and where nano-ros differs from ROS 2 on the wire.

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

Change it

Open the talker’s source, change the "Hello World" text, and run nros run sensor_node again. It rebuilds what changed and boots the new program.

The text is in src/sensor_node/src/lib.rs, in the line that formats the message:

#![allow(unused)]
fn main() {
let _ = write!(msg.data, "Hello World: {}", *state);
}

The text is in src/sensor_node/src/Talker.c, in on_tick:

snprintf(self->msg.data.data, self->msg.data.capacity, "Hello World: %d",
         (int)self->count);

The text is in src/sensor_node/src/Talker.cpp, in on_tick:

snprintf(payload, sizeof(payload), "Hello World: %d", count_);

Keep the message under 64 bytes: the talker formats it into a fixed buffer, because each message is built in a buffer whose size is fixed when the program is built.

What just happened

You ran a ROS 2 talker on a microcontroller without writing any of the program around it. Four things worked together, and each later chapter changes one of them:

  1. The build. nros run compiled your talker, nano-ros and FreeRTOS for the Cortex-M3, a 32-bit Arm core with no memory protection. The program it built contains the talker, the FreeRTOS kernel and the task that runs your node, lwIP, and the zenoh client that carries messages.
  2. The board. QEMU booted that program as the MPS2-AN385 would. QEMU’s user network connects the board’s emulated Ethernet controller, a LAN9118, to your host: on that network the board is 10.0.2.15 and your host is 10.0.2.2. What the program prints reaches your terminal separately, through QEMU itself, which is why the console and the messages do not mix.
  3. The router. nros run started ROS 2’s zenoh router, rmw_zenohd, on TCP port 7447 of your host. The board dials it at 10.0.2.2:7447, and every ROS 2 program on your machine reaches it the same way. nano-ros ships no router of its own: the one ROS 2 already uses is the one the board reaches.
  4. ROS 2. ros2 topic echo with RMW_IMPLEMENTATION=rmw_zenoh_cpp connected to the same router and subscribed to /chatter. To ROS 2, the board is one more node on the graph.

The talker itself is the same kind of node you would write for ROS 2: a publisher, a timer, and a callback that publishes on every tick. What differs is around it. The program is built for one board, its messages are built in buffers of fixed size, and its memory is fixed when it is built. The next chapter opens the two packages and shows where each of those facts is stated.

No QEMU, no router: run it on this machine

The same flow runs on your Linux host, with Cyclone DDS, which needs no router. Only the board in the scaffold line changes; the node code has the same shape, and so does every command after it. Get started on the Linux host takes it step by step. Chapter 9 adds the host to this project as its second target.

If nothing appears

  • nros run stops at once and names AMENT_PREFIX_PATH: this shell has not sourced ROS 2, and no earlier build of the workspace recorded it. Run source /opt/ros/humble/setup.bash and run again.
  • nros run says the router exited (code 127): the router is a ROS 2 program, and this shell has not sourced ROS 2, so it could not start. Source ROS 2 in that shell and run again.
  • nros run stops at once and names the router: it could not find the zenoh router. Install ros-<distro>-rmw-zenoh-cpp if it is missing, or point NROS_RMW_ZENOHD at an rmw_zenohd binary.
  • No Publishing: line within 30 seconds of boot: the router started but the board did not reach it. A cold boot through FreeRTOS, lwIP and the zenoh session takes 10 to 15 seconds. nros run names the router’s log when it stops.
  • ros2 topic echo prints nothing: check the --qos-reliability best_effort flag and the RMW_IMPLEMENTATION line.

Troubleshooting index has the rest.

Next

Design background

The chapter follows RFC-0116 D5 and D6, RFC-0121 D4 and D5, and RFC-0124 D3: one Tutorial project on a Verified board.