CodeTheRobot

Ubuntu Linux for Robotics · Lesson 44 of 96

Writing ROS 2 nodes in C++17

A node class, CMakeLists.txt for C++17, building, running and inspecting the node.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain writing ros 2 nodes in c++17 in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A C++ node is a class derived from rclcpp::Node with a main() that initializes ROS, spins the node and shuts down. In CMakeLists.txt set CMAKE_CXX_STANDARD 17, add the executable, link the dependencies with ament_target_dependencies, and install it to lib/<package> so ros2 run finds it.

The edit-build-run loop on Ubuntu: edit in VS Code, build with colcon build --packages-select --symlink-install, source install/setup.bash, then run with ros2 run and inspect from a second terminal with ros2 node list. A missing install() line is the most common reason 'ros2 run' cannot find an executable.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the writing ros 2 nodes in c++17 commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

set(CMAKE_CXX_STANDARD 17)
# Compile this package as C++17.
find_package(rclcpp REQUIRED)
# Find the C++ client library.
add_executable(hello_node src/hello_node.cpp)
# Build the node from its source file.
ament_target_dependencies(hello_node rclcpp)
# Add the dependency's headers and libraries.
install(TARGETS hello_node DESTINATION lib/${PROJECT_NAME})
# Install where ros2 run looks for executables.
colcon build --packages-select robot_nodes && source install/setup.bash && ros2 run robot_nodes hello_node
# Build, source and run in one line.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

/topicpublishersubscribersubscribermessages delivered: 0MESSAGES BETWEEN NODES

Demo for Writing ROS 2 nodes in C++17. Expected result: [INFO] [hello_node]: hello

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

cd ~/ros2_ws
# TODO: build only robot_nodes, source the workspace and run the executable hello_node

Expected output

[INFO] [hello_node]: hello
Stuck? Show the full solution (try it yourself first)
cd ~/ros2_ws
colcon build --packages-select robot_nodes
source install/setup.bash
ros2 run robot_nodes hello_node

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Write a node that logs a message every second with a timer, register it in CMakeLists.txt, build, run it, and use ros2 node info from a second terminal. Remove the install() line and read the ros2 run error.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.