ROS Packages

Managing ROS packages

Content Outline

    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:

    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 (default ament_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

    ament_cmake_package_structure
    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 program myNode from myNode.cpp.
    • ament_target_dependencies(myNode rclcpp std_msgs): Links node myNode to dependency headers and libraries.
    • install(TARGETS myNode DESTINATION lib/${PROJECT_NAME}): Copies nodes/libraries to workspace’s “install” directory.

    Python-based package

    ament_python_package_structure
    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:

    create a ament_python package
    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.

    install packages from debian packages
    sudo apt-get install ros-humble-teleop-twist-keyboard

    To remove a package use:

    remove a package (but not dependencies)
    sudo apt-get remove ros-<DISTRO>-<PACKAGE-NAME>

    Packages from Source

    1. 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
    1. Install dependencies if need it either manually (painful way), or using rosdep . Keep in mind that rosdep is not magic, it cannot guess the dependencies of your package or third party packages, rosdep will check inside the package.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
    1. Build the workspace using colcon.
    cd ~/ros2_ws/
    colcon build --symlink-install
    1. Source the workspace to make the package available.
    source install/local_setup.bash