IN THIS LESSON

You will learn how to create package in your workspace.

A package is simply a container for ROS 2 code. Code is organised and shared with others through packages. The folders such as ‘src’, ‘launch’ and ‘install’ all belong to a package. Package creation uses ament as a build system and colcon as a build tool. Packages can be created using either CMake or Python. Packages created using these tools must meet minimum file requirement contents as demonstrated in the table on the right.

This is how you create a new ROS Package.

In MiROS

After creating a new Workspace, you will land in the Mission Control page.

Click the ROS Canvas button in the top tool bar to enter the Package Canvas view. Then click the Plus button to create a new ROS Package.

Note:

The Packages labelled in red are protected by the system. They are not editable by the MiROS interface since these packages come from the robot hardware manufactures.

In Ubuntu (Linux)

A single workspace can contain multiple packages (with each package in its own folder). It is best practice to have a ‘src’ folder within the workspace with packages being created in there. You cannot have nested packages.

To create a package, first navigate to the ‘src’ folder before running the command:

ros2 pkg create --build-type ament_cmake <package_name>

The package_name is the name of the package you wish to create. An optional argument is –node-name followed by the name of the node and package name. An example of this code is below.

 

ros2 pkg create --build-type ament_cmake --node-name my_node my_package

 

There should now be a new folder within the workspace’s ‘src’ directory called my_package. The additional required files mentioned above in the table will be automatically created within the my_package folder. Similarly these packages can be created using the python (rather than CMake) by substituting ‘cmake’ for ‘python’ in the above commands.

 

To build a package you must be in the root of the workspace, allowing you to build multiple packages at once. To build the package run the command:

 

colcon build

 

If there are multiple packages it can sometimes be time consuming to build all packages. To build only a certain package run the command:

 

colcon build --packages-select <package_name>

 

You must now source the setup file, allowing you to use your new package and executable. Open a new terminal and source the ROS 2 installation using the command learnt in 3.2 (source/opt/ros/humble/setup.bash). Then navigate to the workspace directory and run the following command to source that workspace

 

. install/local_setup.bash

 

Now the workspace has been added to your path and you will be able to use your new package’s executables.

You can run the executable you created with the --node-name argument through the command:

 

ros2 run my_package my_node

 

You will be able to see the files and folders that ros2 pkg create automatically generated which include:

 

CMakeLists.txt
include
package.xml
src

 

The package.xml file may need to be modified, particularly if you plan on releasing your package. It is good practice to update the description, license and maintainer fields (marked with TODO) anytime a new package is created. If using python rather than CMake, the setup.py file also needs to be populated with the same information as the package.xml file. The information needs to match exactly in each file.

 

Step 1: Click ROS Canvas button in Mission Control

Step 2: Click the Plus button in Package Canvas