CodeTheRobot

C++ Advanced · Lesson 21 of 23

Writing your own ROS 2 packages

package.xml, CMakeLists.txt with ament_cmake, installing targets and testing.

Estimated time · 90–120 min

Learning goals

What you will understand

  • Explain writing your own ros 2 packages in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A ROS 2 C++ package is a folder with package.xml (name, version, dependencies, license) and CMakeLists.txt using ament_cmake. Dependencies are declared twice: in package.xml so colcon can order the build, and in CMake with find_package.

The rules: link with ament_target_dependencies (or target_link_libraries with imported targets), install executables to lib/${PROJECT_NAME}, install launch and config folders to share/${PROJECT_NAME}, and end with ament_package(). Add tests with ament_add_gtest and run colcon test.

Guided lesson

Work through these steps

  1. 1.

    Read the writing your own ros 2 packages example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

cmake_minimum_required(VERSION 3.16)
<!-- Required CMake version. -->
project(robot_nodes)
<!-- Package name; must match package.xml. -->
set(CMAKE_CXX_STANDARD 17)
<!-- This course uses C++17. -->
find_package(ament_cmake REQUIRED)
<!-- The ROS 2 build helpers. -->
find_package(rclcpp REQUIRED)
<!-- The C++ client library. -->
find_package(std_msgs REQUIRED)
<!-- The message package we use. -->
add_executable(talker src/talker.cpp)
<!-- Build the talker executable. -->
ament_target_dependencies(talker rclcpp std_msgs)
<!-- Add include paths and libraries of the dependencies. -->
install(TARGETS talker DESTINATION lib/${PROJECT_NAME})
<!-- Install where ros2 run looks for executables. -->
install(DIRECTORY launch config DESTINATION share/${PROJECT_NAME})
<!-- Install launch files and configuration. -->
ament_package()
<!-- Must be the last line; exports the package to ROS 2. -->

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Build and run

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.

sourcecompilerprogramBUILD AND RUN

Demo for Writing your own ROS 2 packages. Expected result: ros2 pkg executables robot_nodes -> robot_nodes talker

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# CMakeLists.txt of the robot_nodes package
cmake_minimum_required(VERSION 3.16)
project(robot_nodes)
set(CMAKE_CXX_STANDARD 17)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(talker src/talker.cpp)
# TODO: add the dependencies to talker, install it to lib/robot_nodes, and finish with ament_package()

Expected output

ros2 pkg executables robot_nodes  ->  robot_nodes talker
Stuck? Show the full solution (try it yourself first)
# CMakeLists.txt of the robot_nodes package
cmake_minimum_required(VERSION 3.16)
project(robot_nodes)
set(CMAKE_CXX_STANDARD 17)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(talker src/talker.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
install(TARGETS talker DESTINATION lib/robot_nodes)
ament_package()

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Create robot_nodes with a talker and a listener, matching package.xml dependencies, a launch file and one gtest. Build with colcon build --symlink-install, source install/setup.bash, run colcon test, and check with ros2 pkg executables.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.