3.23. How to Develop a Package in ROS2#

3.24. What is a package?#

A package is basically the easiest way for you to share your code with others.

3.25. Prerequisites#

By this point in your ROS journey, you should have set up your ROS2 installation, properly installed and used Colcon, and be able to set up your ROS environment by calling setup.bash

If you have reached this point, you can now develop ROS Packages in both C++ and Python, this guide will go over both.

To begin creating any ROS2 Package, use the following command:

ros2 pkg create --license Apache-2.0 <pkg-name> --dependencies [deps]

In the following steps, we will be following a basic tutorial format, so the usage of the above command will also include the --node-name and --license arguments.

3.26. Creating a package in C++ (CMake)#

Start by navigating to your workspace, in the case of our guides, it would be cd ~/ros2_ws/src on Linux and Mac, or cd \ros2_ws\src on Windows

After reaching your workspace, enter the following command to create the package:

ros2 pkg create --build-type ament_cmake --license Apache-2.0 --node-name my_node my_package

And the terminal should return the following message (or similar):

going to create a new package
package name: my_package
destination directory: /home/user/ros2_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['<name> <email>']
licenses: ['Apache-2.0']
build type: ament_cmake
dependencies: []
node_name: my_node
creating folder ./my_package
creating ./my_package/package.xml
creating source and include folder
creating folder ./my_package/src
creating folder ./my_package/include/my_package
creating ./my_package/CMakeLists.txt
creating ./my_package/src/my_node.cpp

And you should see a folder in the src directory with the name my_package, so you have successfully created your package in C++!

3.27. Creating a package in Python#

Start by navigating to your workspace, in the case of our guides, it would be cd ~/ros2_ws/src on Linux and Mac, or cd \ros2_ws\src on Windows

After reaching your workspace, enter the following command to create the package:

ros2 pkg create --build-type ament_python --license Apache-2.0 --node-name my_node my_package

And the terminal should return the following message (or similar):

going to create a new package
package name: my_package
destination directory: /home/user/ros2_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['<name> <email>']
licenses: ['Apache-2.0']
build type: ament_python
dependencies: []
node_name: my_node
creating folder ./my_package
creating ./my_package/package.xml
creating source folder
creating folder ./my_package/my_package
creating ./my_package/setup.py
creating ./my_package/setup.cfg
creating folder ./my_package/resource
creating ./my_package/resource/my_package
creating ./my_package/my_package/__init__.py
creating folder ./my_package/test
creating ./my_package/test/test_copyright.py
creating ./my_package/test/test_flake8.py
creating ./my_package/test/test_pep257.py
creating ./my_package/my_package/my_node.py

And you should see a folder in the src directory with the name my_package, so you have successfully created your package in python!

3.28. Review Quiz#