Book Image

Robot Operating System Cookbook

By : Kumar Bipin
Book Image

Robot Operating System Cookbook

By: Kumar Bipin

Overview of this book

This book will leverage the power of ROS with an introduction to its core and advanced concepts through exciting recipes. You will get acquainted with the use of different synchronous and asynchronous communication methods, including messages, services, and actions. You will learn how to use the various debugging and visualization tools used in development and how to interface sensors and actuators with the ROS framework. Firstly, you will get to grips with ROS simulation frameworks, such as Gazebo and RotorS for modeling and simulating any physical robot and virtual environment. You will also cover mobile robotics, micro-aerial vehicles, and robotic arms, which are the leading branches of robotic applications. Robot Operating System Cookbook will also guide you in the development of an autonomous navigation framework for both mobile robots and micro-aerial vehicles. Finally, you will explore ROS-Industrial, an open source project that extends the advanced capabilities of ROS software to manufacturing industries.
Table of Contents (12 chapters)

Using ROS from a Linux container

As the industry moves beyond the virtual machine consolidation paradigm, several types of containers have come into prominence. Two flavors currently enjoy the lion's share of deployments on the Linux operating system: Docker and LXC.

Getting ready

LXC (Linux Containers) is an OS-level virtualization technology that allows the creation and running of multiple isolated Linux virtual environments (VE) on a single control host. These isolation levels or containers can be used to either sandbox specific applications or emulate an entirely new host. LXC uses the Linux cgroups functionality, which was introduced in version 2.6.24, to allow the host CPU to better partition memory allocation into isolation levels called namespaces (https://linuxcontainers.org/).

Docker, previously called dotCloud, was started as a side project and only open sourced in 2013. It is really an extension of the LXC capabilities. This it achieved using a high-level API that provides a lightweight virtualization solution to run processes in isolation. Docker is developed in the Go language and utilizes LXC, cgroups, and the Linux kernel itself. Since it's based on LXC, a Docker container does not include a separate operating system; instead it relies on the operating system's own functionality, as provided by the underlying infrastructure. So, Docker acts as a portable container engine, packaging the application and all of its dependencies in a virtual container that can run on any Linux server (https://www.docker.com/).

Note that a Linux VE is distinct from a virtual machine (VM).

How to do it...

Since Docker is an open platform that helps to distribute applications and complete systems and is based on LXC, we will discuss working with ROS Docker Images and how to distribute complex applications along with complete systems as a standalone image.

Installing Docker

Before installing Docker, it will require the updated packages:

$ sudo apt-get update

Use the following command to add the GPG key for the official Docker repository to the system:

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Adding the Docker repository to APT sources

For Ubuntu 16.04, add the following:

$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

For Ubuntu 18.04, add the following:

$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list 

Updating the package database with the Docker packages from the newly added repository is done as follows:

$ sudo apt-get update

Make sure that we are about to install from the Docker repository instead of the default Ubuntu repository:

$ apt-cache policy docker-engine

Notice that docker-engine is not installed yet; to install docker-engine, use the following command:

$ sudo apt-get install -y docker-engine

Check whether Docker is started or not:

$ sudo systemctl status docker

To start the Docker service, use the following command:

$ sudo service docker start
$ docker

We can search for images available on Docker Hub by using the docker command with the search subcommand:

$ sudo docker search Ubuntu

To run the Docker container, use the following command:

$ sudo docker run -it hello-world

Getting and using ROS Docker images

Docker images are akin to packages such as virtual machines or complete systems that are already set up. There are servers that provide the images, and users only have to download them. The main server is Docker Hub, which is located at https://hub.docker.com. Here, it is possible to search for Docker images for different systems and configurations.

Well! All ROS Docker images are listed in the official ROS repository on the web at https://hub.docker.com/_/ros/. We will use ROS Kinetic images, which are already available here:

$ sudo docker pull ros
$ sudo docker pull kinetic-ros-core 
$ sudo docker pull kinetic-ros-base
$ sudo docker pull kinetic-robot
$ sudo docker pull kinetic-perception

Similarly, we could pull ROS Melodic images, which are also already available there:

$ sudo docker pull ros
$ sudo docker pull melodic-ros-core
$ sudo docker pull melodic-ros-base
$ sudo docker pull melodic-robot
$ sudo docker pull melodic-perception

After the container is downloaded, we can run it interactively with the following command:

$ docker run -it ros

This will be like entering a session inside the Docker container. The preceding command will create a new container from the main image wherein we have a full Ubuntu system with ROS Kinetic already installed. We will be able to work as in a regular system, install additional packages, and run the ROS nodes.

We can list all the Docker containers available and the origin of their images:

$ sudo docker ps

Congratulations! We have completed the Docker installation. To set up the ROS environment inside the container in order to start using ROS, we have to run the following command (ros_version: kinetic/melodic):

$ source /opt/ros/<ros_version>/setup.bash

However, in principle, running docker should be enough, but we could even SSH into a running Docker container as a regular machine, using name or ID.

$ sudo docker attach 665b4a1e17b6 #by ID ...OR
$ sudo docker attach loving_heisenberg #by Name

If we use attach, we can use only one instance of the shell. So, if we want to open a new terminal with a new instance of a container's shell, we just need to run the following:

$ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID ...OR 
$ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name

Moreover, Docker containers can be stopped from other terminals using docker stop, and they can also be removed with docker rm.

See also

This book comes with a working Docker Image, which is basically an extension of ROS Kinetic with the code for the examples. The instructions to download and install it will be on the GitHub repository with the rest of the code.