Skip to content
  • Home
  • About the Blog
  • About the Author
  • Sitemap

Abdur Rosyid's Blog

Just a few notes on mechanical engineering and robotics

Creating Packages in ROS2

July 8, 2021 by Abdur Rosyid

Creating a C++ package

Step 1: Create the package using the following CLI command:

cd ~/ros2_ws/src
ros2 pkg create [PACKAGE_NAME] --build-type ament_cmake --dependencies rclcpp std_msgs [OTHER_DEPENDENCY_PACKAGES]

Step 2: Modify package.xml file for package dependencies of the created node(s).

Step 3: Modify CMakeLists.txt.

# Add all the following above ament_package().

add_executable(node_name src/file.cpp)
ament_target_dependencies(node_name rclcpp std_msgs other_dependencies)

# Install node(s)
install(TARGETS node_name DESTINATION lib/${PROJECT_NAME})

# Install launch file(s)
install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})

Step 4: Build using the following CLI command:

colcon build --symlink-install

Creating a Python package

Step 1: Create the package using the following CLI command:

cd ~/ros2_ws/src
ros2 pkg create [PACKAGE_NAME] --build-type ament_python --dependencies rclcpp std_msgs [OTHER_DEPENDENCY_PACKAGES] 

Step 2: Modify package.xml file for package dependencies of the created node(s).

Step 3: Check setup.cfg. It should look like this:

[develop]
script-dir=$base/lib/[PACKAGE_NAME]
[install]
install-scripts=$base/lib/[PACKAGE_NAME}

Step 3: Modify setup.py. This file is like CMakeLists.txt in a C++ package.

import os
from glob import glob
from setuptools import setup

package_name = 'my_package'

setup(
    name=package_name,
    version='0.0.0',
    # Packages to export
    packages=[package_name],
    # Files we want to install, specifically launch files
    data_files=[
        # Install marker file in the package index
        ('share/ament_index/resource_index/packages', ['resource/' + package_name]),
        # Include our package.xml file
        (os.path.join('share', package_name), ['package.xml']),
        # Include all launch files.
        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', *.launch.py'))),
    ],
    # This is important as well
    install_requires=['setuptools'],
    zip_safe=True,
    author='ROS 2 Developer',
    author_email='ros2@ros.com',
    maintainer='ROS 2 Developer',
    maintainer_email='ros2@ros.com',
    keywords=['foo', 'bar'],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: TODO',
        'Programming Language :: Python',
        'Topic :: Software Development',
    ],
    description='My awesome package.',
    license='TODO',
    # Like the CMakeLists add_executable macro, you can add your python
    # scripts here.
    entry_points={
        'console_scripts': [
            'my_script = my_package.my_script:main'
        ],
    },
)

Step 4: Build using the following CLI command:

colcon build --symlink-install

Official documentation:

  • https://docs.ros.org/en/foxy/Tutorials/Creating-Your-First-ROS2-Package.html
  • https://docs.ros.org/en/foxy/Guides/Developing-a-ROS-2-Package.html

Post navigation

Previous Post:

ROS2 vs ROS1 Packages

Next Post:

Writing C++ Publisher in ROS2

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • STEM 101
  • Robotics
  • Kinematics
  • Dynamics
  • Control
  • Robot Operating System (ROS)
  • Robot Operating System (ROS2)
  • Software Development
  • Mechanics of Materials
  • Finite Element Analysis
  • Fluid Mechanics
  • Thermodynamics

Recent Posts

  • Pull Request on Github
  • Basics of Git and Github
  • Conda vs Docker
  • A Conda Cheat Sheet
  • Installing NVIDIA GPU Driver on Ubuntu

Archives

  • June 2025
  • July 2021
  • June 2021
  • March 2021
  • September 2020
  • April 2020
  • January 2015
  • April 2014
  • March 2014
  • March 2012
  • February 2012
  • June 2011
  • March 2008
© 2026 Abdur Rosyid's Blog | WordPress Theme by Superbthemes