ROS Packages
Managing ROS packages
Packages in ROS 2
ROS package
A ROS package is an organizational unit for your code, configuration files and any other resources necessary for the proper operation of your code.
Just think for a moment:
Why do we need a so called 'ROS Package'?
ROS Packages allow you to make your code shareable within other systems that also use ROS, so it is reasonable to think of a ROS package as an analogue to for example a python package (like numpy ↗ ).
ROS 2 support the following packages types:
-
ament_python
↗ : This package type is design to contain python code. ROS follows a standard Python package ↗ structure defined by the Python Packaging Authority ↗ . Such packaging functionality is provided bycolcon
, which internally uses setuptools ↗ . -
ament_cmake
↗ : This package type is design to contain C++ code. ROS uses CMake ↗ with a set of custom cmake macros to handle C++ packaging. For those curious check out ament_cmake github ↗ -
ament_cmake_python
↗
Regardless of the type of package, all of them must contain a file called package.xml
which is a Package Manifest File with meta-data about the package. The latest (version 3) format specification is under REP 149 ↗
- Common tags:
<buildtool_depend>
: Needed to build itself. (Typically ament_cmake)<build_depend>
: Needed to build this package.<exec_depend>
: Needed to run code in this package.<depend>
: Needed to build, export, and execution dependency.
- Uncommon tags:
<build_export_depend>
: Needed to build against this package.<test_depend>
: Only additional dependencies for unit tests.<doc_depend>
: Needed to generate documentation.
ROS cli
Tool for Packages
ROS offers a command line interface (CLI), which has functions for interacting with packages, for example:
ros2 pkg create
: to create the minimal template structure of a given package type (defaultament_cmake
).ros2 pkg list
: list all the “available” packages. Note that you need to source the corresponding workspace to make the package available for ROS.
CMake-based package
my_package
├── include/my_package/
├── src/
├── CMakeLists.txt
└── package.xml
CMakeLists.txt
: standard CMake rules to build the package. Most relevant parts:
add_executable(myNode src/myNode.cpp)
: Builds programmyNode
frommyNode.cpp
.ament_target_dependencies(myNode rclcpp std_msgs)
: Links nodemyNode
to dependency headers and libraries.install(TARGETS myNode DESTINATION lib/${PROJECT_NAME})
: Copies nodes/libraries to workspace’s “install” directory.
Python-based package
my_package/
├── resource/my_package
├── my_package/
├── setup.cfg
├── setup.py
└── package.xml
setup.py
: python package definition. Most relevant parts:
data_files
entry_points
Let’s create a ament_python
into the src
folder inside the workspace:
cd ~/ros2_ws/src/
ros2 pkg create my_package --build-type ament_python --dependencies rclpy std_msgs
Important:
Don't forget to build and source the workspace after adding packages to it.
Install Existing Packages
Debian Packages
- Easy to install
- Usually stable
- Recommended
Structure: sudo apt-get install ros-<DISTRO>-<PACKAGE-NAME>
For example, to install the package called teleop_twist_keyboard
using debian packages:
Important:
When installing ros packages using debian packages replace '_' with '-' in the name of the package.
sudo apt-get install ros-humble-teleop-twist-keyboard
To remove a package use:
sudo apt-get remove ros-<DISTRO>-<PACKAGE-NAME>
Packages from Source
- Git clone the repository (watch out the git branch) of the package into your
src
folder into the workspace.
cd ~/ros2_ws/src
git clone https://github.com/harleylara/ros2-intro.git
- Install dependencies if need it either manually (painful way), or using
rosdep
↗ . Keep in mind thatrosdep
is not magic, it cannot guess the dependencies of your package or third party packages,rosdep
will check inside thepackage.xml
of the package for the defined dependencies, so be organized and don’t forget to add your dependencies inside the package manifest.
sudo apt update && rosdep install -r --from-paths . --ignore-src --rosdistro $ROS_DISTRO -y
- Build the workspace using
colcon
.
cd ~/ros2_ws/
colcon build --symlink-install
- Source the workspace to make the package available.
source install/local_setup.bash