Intro

Abstract

How to create, build, and run a C++ ROS2 package with a minimal setup.

References

Workspace Setup

β€’ Create ROS2 workspace

mkdir -p ~/ros2_ws/src  

β€’ Initialize:

cd ~/ros2_ws && colcon build  

β€’ Source environment

source install/setup.bash

Create a Package

β€’ Navigate to src:

cd ~/ros2_ws/src

β€’ Create package:

ros2 pkg create --build-type ament_cmake my_package --dependencies rclcpp std_msgs

β€’ Folder structure:

ROS2 CMake Packages

my_package/  
β”œβ”€β”€ CMakeLists.txt      # Sets rules to build the package so you can run ros2 run... 
β”œβ”€β”€ include/  
β”‚   └── my_package/     # Directory containing the public headers for the package  
β”œβ”€β”€ package.xml         # File containing meta-information about the package  
└── src/                # Directory containing the source code (AKA ROS nodes) for the package  

Configure VS Code


Update CMakeLists.txt

β€’ Add executable and link libraries:

add_executable(simple_publisher src/simple_publisher.cpp)
ament_target_dependencies(simple_publisher rclcpp std_msgs)
install(TARGETS simple_publisher DESTINATION lib/${PROJECT_NAME})

Build the Package

β€’ Build workspace:

cd ~/ros2_ws && colcon build
Starting >>> my_package
Finished <<< my_package [2.41s]                  

Summary: 1 package finished [2.61s]

β€’ Source install:

source install/setup.bash

Build a node inside a ROS2 C++ package