Launch Files and Package Management
Learning Objectives
- Understand ROS 2 package structure and organization
- Create and manage ROS 2 packages using colcon
- Write launch files to start multiple nodes at once
- Use parameters and configuration files effectively
- Implement best practices for system organization
Prerequisites
- Understanding of ROS 2 nodes and topics (Chapters 1-2)
- Basic knowledge of ROS 2 communication patterns (Chapter 3)
- Basic command-line skills
ROS 2 Package Structure
A ROS 2 package is the basic building block of a ROS 2 system. It contains nodes, libraries, and other resources organized in a standard structure:
my_robot_package/
├── CMakeLists.txt # Build configuration for C++
├── package.xml # Package metadata
├── setup.py # Build configuration for Python
├── setup.cfg # Installation configuration
├── src/ # Source code (C++)
│ └── my_robot_package/
│ └── my_node.cpp
├── my_robot_package/ # Python modules
│ ├── __init__.py
│ └── my_node.py
├── launch/ # Launch files
│ └── robot.launch.py
├── config/ # Configuration files
│ └── params.yaml
├── urdf/ # Robot description files
│ └── robot.urdf
└── test/ # Test files
└── test_my_node.py
package.xml
The package.xml file contains metadata about the package:
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>my_robot_package</name>
<version>0.0.0</version>
<description>My robot package for the Neuro Library textbook</description>
<maintainer email="student@neuro-library.org">Student</maintainer>
<license>Apache License 2.0</license>
<depend>rclpy</depend>
<depend>std_msgs</depend>
<depend>sensor_msgs</depend>
<depend>geometry_msgs</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
CMakeLists.txt (for C++ packages)
cmake_minimum_required(VERSION 3.8)
project(my_robot_package)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# Create executables
add_executable(my_node src/my_node.cpp)
ament_target_dependencies(my_node rclcpp std_msgs)
# Install executables
install(TARGETS
my_node
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
Creating and Building Packages
Creating a Python Package
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python my_robot_package
Creating a C++ Package
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake my_robot_package