Creating Packages in ROS2
Creating a C++ package
Step 1: Create the package using the following CLI command:
1 |
cd ~/ros2_ws/src<br>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.
1 2 3 4 5 6 7 8 9 10 |
# 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:
1 |
colcon build --symlink-install |
Creating a Python package
Step 1: Create the package using the following CLI command:
1 2 |
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:
1 2 3 4 |
[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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:
1 |
colcon build --symlink-install |
Official documentation: