How to Create a ROS 2 Package – Iron

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

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.

8-cobot-arm-examples

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!

How to Create a ROS 2 Workspace – Iron

In this tutorial, we will go over how to create a ROS 2 workspace.

In ROS 2, a workspace serves as the central point for organizing and developing your robot software. It’s a directory that houses all the different software packages, data files, and configuration scripts related to your specific robot project. Think of it as a dedicated folder for all your “robot building blocks.”

The official instructions for creating a workspace are here, but I will walk you through the entire process, step by step.

Let’s get started!

Prerequisites

Directions

Open a terminal, and type these commands:

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/
colcon build
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc

That’s it! You have now created a ROS 2 workspace.

How to Install and Configure Visual Studio Code for ROS 2

In this tutorial, we will go over how to install and configure Visual Studio Code for your ROS 2 projects.

Visual Studio Code, often shortened to VS Code, is a free and open-source code editor developed by Microsoft. It’s a popular choice for programmers of all levels, from beginners to professionals, because it’s:

  • Lightweight and fast: It doesn’t take up much space on your computer and starts up quickly, so you can get coding right away.
  • Versatile: It supports a wide range of programming languages, including JavaScript, Python, Java, C++, and more. You can also install extensions to add support for even more languages.
  • Customizable: You can personalize VS Code with themes, extensions, and keyboard shortcuts to make it work the way you want.
  • Feature-rich: It comes with built-in features like syntax highlighting, code completion, and debugging, which can help you write better code faster.

Let’s get started!

Prerequisites

Directions

Go to this page.

Click the .deb button.

Open a terminal window, and type:

cd ~/Downloads

Run the following command, replacing “filename.deb” with the actual name of your file:

sudo dpkg -i filename.deb

Enter your password when prompted.

The installation will start. Wait for it to complete.

Open VS Code by typing this command in a terminal window:

code
4-open-vs-code

Double-click on the Extensions button on the bottom left side of the window.

5-extensions-tab

Search for the “C/C++” extension.

6-cpp-extension

Install it.

Search for the “ROS” extension.

7-ros-extension-microsoft

Install the ROS extension from Microsoft by clicking the Install button. 

We haven’t created a ROS 2 workspace, but when we do, we will open VS Code as follows:

cd ~/ros2_ws/src
code .

Now let’s install the CMake extension. Search for “CMake”, and install the version made by twxs.

Install the CMake Tools extensions. Search for “CMake Tools”, and install the version made by Microsoft.

Install the XML extensions. Search for “XML”, and install the version made by Red Hat.

Now let’s install the Python extension. Search for “python”, and install the version made by Microsoft. Note: You may have already installed it. 

Install the autoDocstring extension. Search for “autoDocstring”, and install the version made by Nils Werner.

That’s it! 

Now you can close VS Code by going to File -> Exit.