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

What you just built

Tutorial · Level 1: First contact

Needs first: Run a talker on an emulated board

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.

In this chapter you open the two packages chapter 1 scaffolded and ask nros what it built from them. At the end you can say which file holds the talker’s code, which file chose the board and the middleware, and what an image is: the one program nros run built and booted. You change nothing yet; every step reads.

Run the steps from the root of sensor_ws, where chapter 1 left you.

List the packages

nros list

nros list prints each package of the workspace with its kind and its directory. For a Rust project:

ws list (/home/you/sensor_ws):
  rust       sensor_node               src/sensor_node
  other      sensor_node_bringup       src/sensor_node_bringup
ws list: 0 msg, 1 rust consumer

A C or C++ project lists sensor_node as a CMake package instead. The bringup’s kind is other in every language: it holds no code to compile.

Two packages, two roles. Everything you add to this project in the later chapters is another package with one of these roles, or a few more lines in the bringup. The shape of the workspace never changes; it only gains packages. If you come from ROS 2, both roles are familiar:

Your workspaceRoleIn ROS 2 terms
src/sensor_node/node packagea package holding one composable node (rclcpp_components)
src/sensor_node_bringup/bringup packagea <pkg>_bringup package: configuration and launch, no code

Read the node package

A node package is one node, built as a library. It has no main() and names no board and no middleware. Its files, in the language you picked:

src/sensor_node/
├── Cargo.toml   # a library crate and its dependencies
├── package.xml  # ROS 2 manifest: <depend>std_msgs</depend>
└── src/lib.rs   # the talker

src/lib.rs declares the node in two parts. register runs once at startup and states what the node creates: one publisher on /chatter and one timer that fires every 1,000 ms. on_callback is what runs each time the timer fires:

#![allow(unused)]
fn main() {
fn register(ctx: &mut NodeContext<'_>) -> NodeResult<()> {
    let mut node = ctx.create_node(NodeOptions::new("sensor_node"))?;
    let pub_chatter = node.create_publisher_for_topic::<StringMsg>("/chatter")?;
    let _timer = node.create_timer_for_callback_name(
        "on_tick",
        TimerDuration::from_millis(1000),
    )?;
    node.callback_for_name("on_tick")
        .publishes_entity(&pub_chatter)?;
    Ok(())
}
}

The file’s last line, nros::node!(Talker);, exports the node so a program can run it. Cargo.toml names the class the package exports, sensor_node::Talker, and lists nros as a dependency with version = "*". Nothing of nano-ros comes from crates.io: the build points that dependency at the nano-ros your nros installed.

src/sensor_node/
├── CMakeLists.txt     # the library and the class it registers
├── nros-codegen.toml  # the longest string a message holds
├── package.xml        # ROS 2 manifest: <depend>std_msgs</depend>
└── src/Talker.c       # the talker

src/Talker.c is a state struct, a timer callback and a configure function. configure runs once at startup, on a node and an executor the program hands it, and creates one publisher on /chatter and one timer that fires every second. The file’s last line exports the pair:

RCLC_C_COMPONENT(talker_t, talker_configure)

CMakeLists.txt finds the message package and registers the class the bringup names (comments trimmed):

find_package(nano_ros REQUIRED)
find_package(std_msgs REQUIRED)

nano_ros_add_node(sensor_node CLASS sensor_node::Talker TYPED
    SOURCES src/Talker.c)

Nothing in it chooses the board the project builds for; the bringup does that.

src/sensor_node/
├── CMakeLists.txt                  # the library and the class it registers
├── include/sensor_node/Talker.hpp  # the Talker class
├── nros-codegen.toml               # the longest string a message holds
├── package.xml                     # ROS 2 manifest: <depend>std_msgs</depend>
└── src/Talker.cpp                  # its two members

The node is a class with a configure(rclcpp::Node&) member, the shape of a composable node. configure runs once at startup, creates one publisher on /chatter, and binds on_tick as a timer that fires every second:

class Talker {
    ::rclcpp::Publisher<std_msgs::msg::String> pub_;
    ::rclcpp::Timer timer_;
    int count_ = 0;

    void on_tick();

  public:
    ::rclcpp::Result configure(::rclcpp::Node& node);
};

CMakeLists.txt finds the message package and registers the class the bringup names (comments trimmed):

find_package(nano_ros REQUIRED)
find_package(std_msgs REQUIRED)

nano_ros_add_node(sensor_node CLASS sensor_node::Talker
    SOURCES src/Talker.cpp)

target_link_libraries(sensor_node_sensor_node_component
    PUBLIC std_msgs__nano_ros_cpp)

The last line links the generated C++ code of std_msgs. Nothing in the file chooses the board the project builds for; the bringup does that.

In every language, package.xml is a standard ROS 2 manifest, and its <depend>std_msgs</depend> line is the only place the message package is named. The build generates the std_msgs code from your ROS 2 install, which is why chapter 1 sourced ROS 2 before the first run. You never write message code by hand, and you never commit it.

The same node package builds for any board, because nothing in it says mps2-an385, freertos or zenoh. Those words belong to the bringup.

Read the bringup

cat src/sensor_node_bringup/system.toml

Comments trimmed, the file is:

[system]
name = "sensor_node"
rmw = "zenoh"
ros_edition = "humble"
domain_id = 0

[[component]]
pkg = "sensor_node"
class = "sensor_node::Talker"
name = "sensor_node"

[image.freertos]
board = "mps2-an385-freertos"
stack_floor_image = "talker/rust/zenoh@mps2-an385-freertos"

# `nros test` runs this image and passes once the talker publishes.
[image.freertos.test]
expect = ["Publishing:"]

A C or C++ project’s file is the same, but for c or cpp in place of rust on the stack_floor_image line. The bringup holds package.xml, this system.toml and a contract file that chapter 3 looks at, and no code. Its three tables answer three questions:

  • [system]: how the nodes reach ROS 2. rmw is the middleware, the --rmw zenoh you passed to nros setup. ros_edition is the ROS 2 release your peers run, and domain_id is the ROS 2 domain.
  • [[component]]: which nodes run. One row per node: the package, the class it registers, and the node’s name at run time. Chapter 3 adds a second row.
  • [image.freertos]: what program is built, for which board. The board line chose the MPS2-AN385 with FreeRTOS under the program. The table has no locator line, so the image reaches its zenoh router the way the board states: TCP to 10.0.2.2:7447, over the board’s emulated Ethernet. stack_floor_image names the template the image was made from, whose measured stack use the build checks this image against; remove it once the image is no longer that template. Its test table makes the image a test: nros test builds it, runs it, and passes once a console line contains Publishing: (Test an image).

Every configuration choice of the project lives in this file, never in a node package. Moving the talker to another board is another [image.*] table; switching the middleware is the rmw line. Neither touches src/sensor_node/.

Ask what nros run runs

nros run sensor_node worked in chapter 1, but you never said which board. Ask nros how it decided, without building or booting anything:

nros run sensor_node --dry-run --why

--dry-run prints the run plan instead of carrying it out, and --why adds where each part of the plan came from. The parts that matter here, trimmed:

image = "sensor_node_bringup:freertos"
verb = "run"

[runner]
package = "nros_runner_qemu"
name = "qemu"

[target]
emulator = "qemu:mps2-an385"

[console]
kind = "semihosting"

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

[provenance]
image = "rung 3, `<node>`: `sensor_node` is the only component of `sensor_node_bringup`, whose only image this is"
link = "board"
"link.host_peer" = "rmw `zenoh` (.../nros-rmw-zenoh/nros-rmw.toml)"
"target.emulator" = "board"

Read it from the top:

  • image is sensor_node_bringup:freertos: the bringup, a colon, and the name of its [image.freertos] table. That is the full name of what you ran.
  • [provenance] image explains the shortcut. You typed the name of a node; sensor_node is the only node of sensor_node_bringup, and that bringup has only one image, so the name picked exactly one image.
  • [target] and [runner] say the image runs in QEMU’s MPS2-AN385, booted by the qemu runner. Their provenance is board: the board’s own description states how it runs, so the bringup does not.
  • [link] is how the image reaches its router, TCP to the host, and it is why nros run started a zenoh router on TCP port 7447. Its provenance is board: the image states no locator, so the board’s applies. Its host_peer is what the zenoh backend’s own descriptor says it needs on this side of the link; that, not the backend’s name, is what decides to start a router.
  • [console] says the program’s printed output comes through QEMU’s semihosting, apart from the network the messages use.

See what the build left behind

Chapter 1’s run left more in the workspace than the two packages:

ls
build  generated  nros-sdk.lock  src
  • src/ holds the two packages, and nothing else. It is the only directory you write in.
  • generated/ holds the message code the build generated from your ROS 2 install: std_msgs, and builtin_interfaces, which std_msgs depends on. Adding a <depend> to a node’s package.xml adds a directory here on the next build. Chapter 5 adds your own message package and finds its code here too.
  • build/ holds the image’s build tree, the program’s generated main(), and each run’s logs. The board’s console from chapter 1 is in build/nros/run/sensor_node_bringup_freertos/console.log, which is the first file to read when a run goes wrong.
  • nros-sdk.lock records what the build was made from: the versions of the ROS 2 message packages it generated code from, and the source of the zenoh client it compiled. The scaffold’s .gitignore leaves it in, so it is committed with your sources and a teammate’s build uses the same inputs.

The scaffold’s .gitignore leaves out build/ and generated/. You can delete both at any time: the next nros run makes them again, which takes as long as chapter 1’s first run did.

What just happened

You met the three nano-ros ideas every later chapter builds on.

A node package is code that runs anywhere. It is a library holding one node. It has no main() and names no board, no middleware and no ROS 2 release, so the same package goes into an image for this emulated board, an image for your Linux host, or an image for a board with an RTOS, without a second copy.

A bringup package says what runs where. It holds no code: only system.toml, which lists the nodes ([[component]]), how they reach ROS 2 ([system]), and the programs to build ([image.*]). Where a ROS 2 project spreads that across launch files, parameter files and environment variables, nano-ros keeps it in one file, because on a microcontroller it must be known when the program is built: there is no shell, file system or environment to read it from at run time.

An image is one program for one board. Each [image.*] table is one image: the bringup’s nodes, built with one middleware for one board, into one file you can boot or flash. nros run built sensor_node_bringup:freertos in chapter 1 and booted it in QEMU. Nothing in src/ contains that program’s main(): nros generated it under build/ from the image table, together with the generated message code and the build settings the board needs.

This is where nano-ros differs most from a ROS 2 workspace. In ROS 2, a package builds executables, and you start each one with ros2 run or a launch file, as its own process, on a machine that already runs an operating system. In nano-ros there is no separate step that starts nodes: the image is the whole system for its board. Every node of the bringup is compiled into it, they share one executor, and booting the image starts all of them. Running two images means two programs, on one board each. A bare-metal board has no processes at all, so one program per board is the only shape that fits it, and nano-ros uses the same shape on every board, Linux included, so a project does not change shape when it changes board.

Everything under src/ is yours to edit and commit. Everything under build/ is derived from src/ and your ROS 2 install, so none of it is committed and none of it is edited; the scaffold’s .gitignore already leaves it out. How a build becomes an image explains the layers inside an image and why the program is generated rather than written.

Next

Design background

The two roles and the generated program follow RFC-0121 D1 to D3. The run plan --dry-run prints is RFC-0118’s.