In this tutorial, we will go over how to create a ROS 2 package.
A ROS 2 package is the fundamental building block of robot software in ROS 2. Imagine it as a Lego brick in your robot’s software architecture. Each package contains a specific piece of functionality or capability, like motor control, sensor processing, or communication with other systems.
Remember, just like Legos, you can combine multiple ROS 2 packages to create complex robot functionalities. Each package plays its specific role, and together they build the complete software system for your robot.
The official instructions for creating a package are here, but I will walk you through the entire process, step by step.
Let’s get started!
Prerequisites
- You have created a ROS 2 workspace.
Directions
Open a terminal, and type these commands (note: we are using the Apache 2.0 license, but you can remove that piece if you want):
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 cobot_arm_examples
Now let’s build our new package:
cd ~/ros2_ws
colcon build
Let’s see if our new package is recognized by ROS 2.
Either open a new terminal window or source the bashrc file like this:
source ~/.bashrc
ros2 pkg list
You can see the newly created package right there at the top.
Install Useful Packages (Optional)
In a month or so, I will begin building a robotic arm. The package in this tutorial will serve as the foundation for the ROS 2 software.
Let’s install some useful packages that will help us along the way.
Begin by installing a tool called Terminator. Terminator will enable you to have multiple terminal panes within a single interface.
Open a terminal window, and type the following commands:
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install terminator
To open terminator, you can either click the 9 dots in the bottom left of your Desktop and search for “terminator”….or you can type terminator in a regular terminal window.
You can use CTRL + Shift + E or CTRL + Shift + O to split the terminal window into different panels.
Let’s install some useful ROS 2 packages. Open a terminal window, and type the following:
sudo apt-get install ros-$ROS_DISTRO-gazebo-ros
sudo apt-get install ros-$ROS_DISTRO-gazebo-ros2-control
sudo apt-get install ros-$ROS_DISTRO-joint-state-publisher-gui
sudo apt-get install ros-$ROS_DISTRO-moveit
sudo apt-get install ros-$ROS_DISTRO-xacro
sudo apt-get install ros-$ROS_DISTRO-ros2-control
sudo apt-get install ros-$ROS_DISTRO-ros2-controllers
sudo apt-get install libserial-dev
sudo apt-get install python3-pip
pip install pyserial
If you want to use Amazon Alexa Voice Assistant down the road to control your robotic arm, install the following:
pip install ask-sdk
pip install flask
pip install flask-ask-sdk
Configure Colcon
colcon is the primary command-line tool for building, testing, and installing ROS packages.
In your terminal window, type the following command:
echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
That’s it! Keep building!