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

Same project, another target

Tutorial · Level 2: Building real applications

Needs first: Tell the build what your system creates

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

The project has run on one target since chapter 1: the MPS2-AN385 with FreeRTOS, in QEMU. In this chapter you build the same six nodes, with the same launch file and the same contract, for a second target: your Linux host, as an ordinary program on your machine. You add one table to the bringup and change nothing else. At the end the bringup has two images, and the host one prints the same lines the board does:

[INFO] client_node: [8345474.073235] Result of add_two_ints: 5
...
[INFO] action_client_node: [8345482.703991] Result received: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

The board’s image keeps working beside it, and you choose which one runs by its name.

Run every step from the root of sensor_ws. The chapter changes one file:

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

Provision the second target

nros setup linux --rmw zenoh

For the Linux host, nros setup checks the host toolchain and the zenoh sources it builds against, and usually finds everything already present: the host needs no cross-compiler and no emulator.

On the host, the image is a Linux program. Where the board’s image runs nano-ros in a FreeRTOS task, with lwIP under it, the host image runs it in a process, on the host’s own network stack. Nothing in the nodes changes: they were written against nano-ros, not against FreeRTOS.

Add an image row

An image is one [image.<id>] table in the bringup’s system.toml. Add a second one, named linux, at the end of the file:

Add to the end of src/sensor_node_bringup/system.toml

[image.linux]
board = "linux"

board names the target by its nano-ros board name; Supported boards lists them. The image needs no locator: it runs on your host, and the host board’s own default dials the router on tcp/127.0.0.1:7447.

The image’s name, linux, is yours to choose. It appears in the full name of the image, sensor_node_bringup:linux, in the directory its build lands in under build/, and on the command line, so pick a short name that says which target it is.

Everything else the image needs, it inherits: the [[component]] rows, the launch file, the contract, rmw = "zenoh" from [system]. Nothing in src/ outside the bringup changes. The board’s own description supplies the rest: the Rust target or the C toolchain, the linker script, the RTOS configuration and the memory the board has.

Name the image you run

With two images, a command that names nothing no longer picks one. Plain nros run configures the images, then refuses, and its last lines list what would work:

nros run: this workspace declares 2 images and no default.

  sensor_node_bringup:freertos
  sensor_node_bringup:linux

  build one:   nros build freertos
  build all:   nros build --all
  or declare:  [system] default_images = ["freertos"]

nros run, nros build and nros flash all accept the same spellings, tried in this order:

You typeIt means
sensor_node_bringup:linuxthat bringup’s image; always works
linuxthe image named linux, when only one bringup declares it
sensor_nodethe bringup whose only node is sensor_node, with its only image
nothingthe workspace’s only image

The third spelling stopped working in chapter 3, and the fourth stops now. Anything that does not resolve refuses and lists the full spellings; a shortcut never picks one of several. --why names the rule that resolved:

nros run linux --dry-run --why

The plan is chapter 2’s, for another target. Its image lines, near the top and in [provenance], say which rule picked it:

image = "sensor_node_bringup:linux"
image = "rung 2, `<image>`: declared by one bringup, `sensor_node_bringup`"

To make a bare nros build or nros run choose for you, declare the images it means in [system]:

[system]
default_images = ["freertos"]

Run it on the second target

source /opt/ros/humble/setup.bash
nros run linux
I heard: reading 1
Result of add_two_ints: 5
Result received: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
I heard: reading 1
[action_client_node] result last=
I heard: reading 1
[action_client_node] result last=

The first build of the image compiles nano-ros for your host, which takes a minute or two. Then the program starts, and its output (a Rust project’s, trimmed) is the board’s without the board:

nros run: starting the zenoh router on tcp/127.0.0.1:7447
[INFO] nros: session open
[INFO] sensor_node: [8345473.073004] Publishing every 500 ms
[INFO] server_node: [8345473.073180] Waiting for service requests
[INFO] action_server_node: [8345473.073438] Waiting for action goals
[INFO] action_client_node: [8345473.083724] Sending goal
[INFO] sensor_node: [8345473.572971] Publishing: reading 1 (21100 mC)
[INFO] monitor_node: [8345473.573009] I heard: reading 1 (21100 mC)
[INFO] client_node: [8345474.073235] Result of add_two_ints: 5
...
[INFO] action_client_node: [8345482.703991] Result received: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

There is no banner and no network to bring up: the program is a process on a machine that already has both. The number in brackets is your host’s monotonic clock, where the board’s counts from its own boot. Every node line is the same as on the board, because they are the same nodes, and the in-image goal finishes in about ten seconds instead of twenty-five, because a host processor is faster than an emulated Cortex-M3. Press Ctrl-C when the action result has printed.

The contract from chapter 8 sized this image too, because a contract belongs to the launch file, and every image built from that launch file is sized from it. The numbers it derives differ per board, since each board’s pools have their own sizes, but the statement is written once.

The first image still runs, unchanged. Press Ctrl-C when the listener has heard a few readings:

nros run freertos
I heard: reading 3

This build of the board image did one thing it never did before. Its output says so:

entity-census take sensor_node_bringup:freertos: taking the census with `sensor_node_bringup:linux` (linux_entry)

With a host image of the same launch file in the bringup, nros build takes the census that chapter 8 marked as missing: it runs the host program once, records what every node creates, and compares that with the contract before it builds the board’s image. A contract row the code does not match now stops the build, naming the node, the entry and the file to change. That is why chapter 7’s talker declares period_ms as well as reading it: a parameter the contract states and no node declares is refused here.

Change it

Make the board the image a bare command means. Add default_images = ["freertos"] to the [system] table, and plain nros run and nros build stop refusing: they run and build the FreeRTOS image, and nros run linux still names the host. Remove the line to go back to naming one every time. A list of several images makes a bare nros build build each of them.

If it doesn’t work

  • The build stops because the host toolchain is missing: this machine has not provisioned the host target. Run nros setup linux --rmw zenoh, as above.
  • nros run refuses and lists two images: name one, as in the table above. nros run linux and nros run sensor_node_bringup:linux are the same image.
  • The host program starts and never prints I heard:: it did not reach the router. Check that nothing else holds port 7447, or run your own router and pass --router tcp/127.0.0.1:7447.

What an image row can say

The two image tables differ in their board line. An image can also say anything else that is a fact about that program and not about its nodes:

KeyMeaning
boardthe target, by its nano-ros board name
rmwthis image’s RMW, when it differs from [system] rmw
locatorwhere this image’s router or agent is
launcha launch file in the bringup other than the default one
featurescapabilities added to [system] features for this image
os_configRTOS configuration files: Zephyr prj-*.conf fragments or a board network file

system.toml keys lists them all. Values every image shares go in [image_defaults], so eight images do not repeat their RMW eight times; a key in a specific image replaces the default.

On the host, Cyclone DDS runs as well as zenoh, which Get started on the Linux host shows on a project of its own. A host image of the same launch file is also what the census of chapter 8 runs: with one in the bringup, nros build takes the census and compares it with the contract before it builds the board image.

What just happened

You added an image row: a second program built from the same bringup. The bringup now describes one system, six nodes with their names, parameters and contract, deployed two ways, and each image is one table that says how.

That is the shape every nano-ros project grows into. The nodes say what the system does, the launch file and the contract say how the system is put together, and each image says where it runs and through what. A new board is a new image row; a new middleware for one board is an rmw line in its row; a second variant of the system for the same board is a second row naming another launch file. Each row builds and runs on its own, with its own name, and nros never chooses between them for you.

In ROS 2 a launch file starts processes on machines that already run an operating system, so where a node runs is decided when it starts. In nano-ros that is decided when the image is built, because on a microcontroller the program and its target are one thing: there is no loader that could put the same binary on another board. One bringup with several image rows is how one system reaches several targets without being written twice.

Next

Design background

Images as rows of the bringup, and the target names nros run accepts, follow RFC-0121; the board descriptor that supplies an image’s target facts follows RFC-0098.