Using ROS Packages

The following article will explain what a ROS package is and how you can use them to run programs in ROS, the Robot Operating System.

What are ROS Packages?

As mentioned earlier, every program in ROS is delivered as a package. A package can contain various types of files where the most important files are the CMakeLists.txt file and the package.xml file. These two files are automatically generated when you create a package. These files contain information about the package so it can be build, which means the source code can be compiled so that you can run the programs. This means, that packages usually also contain the source code of the programs you want to run.

Now that you have an idea what a package is, you can see how you can run them.

Running a ROS package

Before running a ROS program, you need to do a couple of extra steps in advance. These steps are to build the catkin workspace, start the roscore and then starting the actual program.

The catkin workspace

A catkin workspace is a directory on your computer that follows some specific guidelines. These make it easier for the software using the directory and its files to find what it needs, in this case, the source code of your software.

To build your catkin workspace which you have created during the installation of ROS, you type the following two commands, assuming you called your catkin workspace also catkin_ws:

cd ~/catkin_ws
catkin_make

After catkin has finished, you can now start a roscore and then run the software.

Starting ROS

Before you can run any ROS program, you need to start a roscore. This can be done by opening a terminal and typing the following text and then hitting ENTER on the keyboard:

roscore

Starting the roscore in the terminal should output something similar to the following image:

Now, the roscore should be running and the computer is ready to run ROS programs. Note: only one single roscore can run in the same robotic system at the same time, so you only need to do this step once.

If the roscore dies, you need to restart it. You can also stop the roscore by pressing CTRL+c on your keyboard.

Running a ROS program

Now, it is time to start a real ROS program. Therefore, you need to open a new terminal and then you can run a program with the following syntax:

rosrun <ros_package> <ros_program>

Of course, the <ros_package> and the <ros_program> are placeholders and need to be replaced by an actual package and program name. For example, you can run the turtlesim program which is an animated 2D turtle that can be controlled with ROS commands just like a real robot.

rosrun turtlesim turtlesim_node

Now, a little window should open on your screen and you should see a little turtle in the middle of a colored canvas.

Next, you can interact with the turtle by starting another node by opening a new terminal and typing:

rosrun turtlesim draw_square

The turtle will start moving in a square shape and it will draw a line on the canvas where it is moving:

At this point, you have two ROS programs running that interact with each other. As mentioned earlier, each ROS program is running as a node. These nodes can be visualized with a program called RQT. You will learn more about this software later, but here is the graph that this software will generate for your current setup:

You have two nodes running and they communicate by using Topics. In the next article, you will learn how to create your own package and how you can simplify the process of starting multiple ROS programs at the same time.

Overview of ROS

After you installed ROS, you will learn how to actually use ROS. Therefore, you will get an overview of what ROS does for you and how it works.

The Robot Operating System (ROS) is a meta-operating system that is installed on top of your actual operating system. In this case, you installed Ubuntu as your operating system.

To simplify things, you can see ROS as a tool-box including libraries and programs to develop and run robots. Some of the libraries are the OpenCV library for computer vision tasks and the tf library for frame transformations. Some of the tools that are included are Gazebo for robot simulations and RVIZ for visualization of your robot. In addition to that, there are software packages that have been created for specific tasks such as robot navigation or motion planning which you can simply install as they are openly available. For more information about ROS, you can have a look at the ROS Wiki.

ROS is released in different versions that go along with the current Ubuntu versions. The version discussed on this website is ROS Melodic which has been released for Ubuntu 18.04.

ROS Structure

Programs within ROS are not simply started like other programs. Each ROS program is called a ROS node. Each node is usually built for one specific task such as navigation, reading sensor data or managing data and tasks. The nodes can communicate with each other to provide data to another node. The nodes can run with different frequency rates and they can even be executed asynchronously, which means one node can wait for the output of another node depending on the application. Having several nodes has the advantage that a failure that may crash a single node will not affect other nodes.

As an example, imagine the robotic arm will have an error and causes its node to crash, the navigation and the vision nodes will still be running. Furthermore, ROS can then restart the single node that crashed and send a warning to an operator and write the crash into a log file.

The implementation of having nodes run in parallel is also a very easy way to implement multi-threading which means the processor can run many tasks at the same time instead of one task after another.

Communication between Nodes

When several nodes want to exchange data, they need to communicate with each other. ROS has three main methods of implementing communication between nodes: Topics, Services and Actions.

Topics

Topics are the most important type of communication in a ROS system. They provide a constant flow of data from one node to another. This can be compared with broadcast radio where there is a constant signal with audio information. The radio doesn’t wait for the radio station to send a signal and the station doesn’t care how many radios are listening.

In ROS, such a broadcaster is called a Publisher node which is publishing to a Topic. A node that is listening to that Topic is called a Subscriber which subscribes to this Topic. The Topic is the actual data stream containing a specific type of information such as camera data or movement data.

In other words, Topics send data from a Publisher to a Subscriber in form of a data stream. This is important for applications that require constant data input. These data streams create a lot of data traffic within the ROS network which needs to be kept in mind when creating another Topic.

The most prominent Topic used in a robot is the /cmd_vel Topic that contains the velocity commands for the robot. These commands tell the robot where to go and at what speed.

For more information about Topics, you can have a look at the ROS wiki page.

Services

Services are less frequent than Topics but they can be very handy to reduce the overall data flow. Services do not provide a data stream, but instead, they work with asynchronous data requests similar to a web server hosting a website. When a person is calling a website, the computer sends a request to the server for receiving the web site content. After the web server has executed its task, it will send the requested data to the user and the communication is finished.

A Service is also implemented by setting up a Service Server which will wait for a call while the Service Client will call this Server. The Server will then perform its task once and send a signal back to the Client to tell if it finished successfully or failed. That’s it, there is no more communication between both sites. Also, the Client will wait for the response from the Server and will only resume its task when it received a response.

An example of when to use a Service would be a drone that is taking of, or even landing. When taking off, the drone simply needs a single signal to start the takeoff process and it doesn’t require constant input from the controller. When the drone is up in the air, it will send a quick response to the controller to say that it has succeeded.

For more information about Services, you can have a look at the ROS wiki page.

Actions

Actions can be seen as a middle ground between a Topic and a Service. Just like a Service, there is an Action Server and a Client. Now the difference to a normal Service is that a Service Client is waiting for a response from the Server while an Action Client will continue with another task. On the Action Server, the task will be executed and the Action Server will send regular updates to the Client. These updates are less frequent than a Topic but they occur at a user-determined frequency.

In other words, with Actions, the Client doesn’t need to wait, but similar to a Service it will get a response when the Server has finished its task.

A good example of using an Action would be the action of a mechanical gripper. While it is closing, it will provide you with information on whether it is closed or not. Also, Actions are ideal for tasks that do not need a constant data stream but yet take more time which makes it impossible to simply wait for the result. If your robot shall drive and perform another task, you cannot allow the robot to wait for the feedback of a Service, therefore, an action would be ideal in this case as the robot is still capable of observing its environment while waiting for the result of the Action.

For more information about Actions, you can have a look at the ROS wiki page.

Combination of Topics, Services and Actions

Usually, you take the tools that are best suited for the job. In the case of ROS, you have to choose whether to use a Topic, a Service or an Action. In a complex system, there are usually many different ways of communication used at the same time.

For example, then you want to control an autonomous drone, you will simply use a Service for a takeoff command while you will have a Topic that gives you information on the position and height of the drone. Once you worked with some ROS packages, you will get a feeling of when to use which type of communication.

Software Management

The entire ROS system is managed by something called the roscore which observes all the active nodes and manage all the communication done through Topics, Services and Actions. The roscore is what creates the magic behind ROS as it manages all the tasks in the background.

The roscore is watching each node and its state. If a node crashed, it will keep track of this and depending on the configuration, it will run that node again to make sure the underlying task will be executed. As the roscore is the root of all the ROS behaviour, this node needs to be the first node to be running.

Besides from watching the nodes, the roscore also manages the data traffic by looking which Topics, Services and Actions are currently available, which of these are currently called and which node is calling which message type at what time.

Programs in ROS

ROS officially supports two different programming languages: C++ and Python. C++ is more resource-efficient and the code runs faster as it is a rather low-level programming language and because it is compiled (this means, the source code is converted into machine code) before the execution. Python on the other hand is slower as it is a scripting language but this makes it faster to test the code as no compilation is necessary. If you want to learn more about how to program with Python, have a look at this programming guide!

Next to C++ and Python, many more programming languages are not officially supported by ROS, but there are libraries to bring the ROS functionality to the corresponding language such as Go or Rust.

Packages in ROS

ROS is built to be modular. This means that the software is split into several packages that can interact with each other. usually, there are packages for navigation, sensing the environment, mapping the environment, computer vision and general control of the robot. Having several packages makes it easy to share the functionality of one package across several different robots. At the same time, when you create a new package, you only need to program the parts that are not already implemented in another package. To increase reusability, it is important to keep your code robot-agnostic which means the code should work for all situations and not only for this very specific robot. If the package works despite it doesn’t know which robot it is running on, it is more modular and can be shared with colleagues across the world or simply within the robotics group that you are working with.

Installing ROS Melodic

The Robot Operating System, or for short ROS, is a so called meta-operating system for robot development. Basically it is a framework including libraries, tools, modules and complete solutions to make a robot do what you want.

The easiest and most reliable way to install ROS on your machine is to install it on an Ubuntu based system. Make sure you have Ubuntu installed on your system before you proceed as this guide will be based on Ubuntu. The instructions provided in this guide are from the official ROS Melodic installation page.

Note:

The following sections will use some common commands for the bash shell. One of them is sudo which is the way to tell bash to run the given command with administrator rights. This will usually require you to type in your password. Another common command is apt which is calling the package manager. The package manager is responsible fr installing, updating and removing software from the official repositories on your system.

Installing ROS

First you need set up your computer to accept software from packages.ros.org:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

Next you need to add the key to your system:

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

Next you need to update the system:

sudo apt update

Now, you can install the software. There are different versions. If you want to use it for developing software on your computer, you will probably want the full package:

sudo apt install ros-melodic-desktop-full

You might get ask to confirm the installation with a [Y/n] prompt. Press enter to confirm the default action. This might take a while as the software is quite large.

Setting up the Environment

The first thing you want to do is to configure the .bashrc file. This file is a hidden (as its name starts with a dot) configuration file for the bash shell (the software running in the Terminal). The .bashrc file is read by the shell every time you open a new tab or window.

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

This command will write the source /opt/ros/melodic/setup.bash to the .bashrc file. So every time you open a new terminal window or tab, it will call the setup script to enable the ROS functionality.

Setting up the Dependencies

Before setting up the dependencies, you first need to install some software that allows you to initialize the dependencies:

sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential

The next thing you need to do is to initialize the ROS dependencies:

sudo apt install python-rosdep
sudo rosdep init
rosdep update

Now, the ROS environment is ready so far, but you still want to make some changes on your setup to make the use more convenient.

Setting up the Workspace

Next, you should create your workspace. ROS is using a system called catkin which is a way to organize and structure the workspace. Therefore, the workspace is usually called catkin_ws. You can create it as follows:

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make

Now that your workspace has been created, you need to initialize it:

source ~/catkin_ws/devel/setup.bash

As this needs to be done for every new terminal window and tab, you can also write this into your .bashrc file:

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc

When using the visualization tool RVIZ, I noticed some buggy behavior. This can be solved with the following. Open the .bashrc file with a text editor and add the following line at the bottom:

LC_NUMERIC="en_US.UTF-8"

This is it, you have installed ROS and set up your workspace. The software packages that you are writing need to be placed inside the /catkin_ws/src directory.