ROS 2 Communication Basics: Publishers, Subscribers, Topics

The foundation of ROS 2 communication is made up of three elements, publishers, subscribers, and topics.

Let’s use an analogy to explain these three elements.

Imagine our robot is like a superhero team with lots of helpers. Each helper has a special superpower, like seeing far away with LIDAR, seeing close-up with the camera, and detecting nearby objects with the ultrasonic sensor.

Publishers are like the robot’s messengers. They take the information from each helper (like the distance LIDAR measured or the picture the camera saw) and shout it out loud on a special channel called a topic. Think of it like a walkie-talkie channel just for robot information!

Subscribers are like the robot’s listeners. They tune into the topic and listen for the messages the publishers shout. When they hear something, they understand what it means and tell the robot what’s happening around it. For example, if the LIDAR subscriber hears “Obstacle 5 meters ahead!” the robot knows to stop.

So, the publisher is the one who shares the information, the subscriber is the one who listens and learns, and the topic is the special channel they use to talk to each other. This way, all the robot’s helpers can work together and keep it safe and aware of its surroundings, just like a superhero team!

Remember, just like superheroes have different powers, publishers and subscribers have different jobs, but they all work together to make the robot super smart.

Publishers and subscribers are functionalities within processes called nodes. Nodes are the fundamental building blocks of a ROS 2 system, each representing an independent unit of computation with specific tasks.

Nodes:

  • Individual processes that perform specific tasks within a ROS 2 system.
  • Each node has a unique name and can host multiple publishers and subscribers.
  • Nodes communicate with each other through topics.

Publishers:

  • Functionalities within a node that publish data via topics.
  • Topics are named channels like “sensor_data” or “motor_commands” where data is published and made available to interested subscribers.
  • Any node can subscribe to a topic, regardless of the publisher node.

Subscribers:

  • Functionalities within a node that receive data published to a topic.
  • They can process the received data and utilize it for their specific tasks.
  • Multiple subscribers can listen to the same topic simultaneously.

Here is a graphic from the ROS 2 website that puts all this together.

publish_subscribe_architecture

You don’t need to memorize the information above. Just refer back to it whenever you get confused on the ROS 2 jargon.

Keep building!

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.