How To Install ROS Melodic, rosserial, and more on Raspberry Pi 4 (Raspbian Buster)

The process of building an autonomous robot starts before the first sensor or actuator mounted on the chassis. The build process requires hardware and software setup, which is the topic of this article.

The first step is to set up and install the computer that will manage hardware components and run algorithms to control the robot. For the robotic projects that you will find on this website (at least on the beginning), I will use Raspberry Pi as the main computer on top of the robot.

Different projects might have different requirements. For this reason, I used to work with the most powerful version of Pi: the Raspberry Pi 4 Model B with 4GB of RAM. This version is capable of running ROS, algorithms to detect objects, and up deep learning algorithms.

At the end of this post, you’ll have a Raspberry Pi ready to run Python algorithms, send and receive data from single-board microcontrollers, and with remote access services installed.

I split the post into six parts to be able to switch from one topic to another or to ignore the installation of a particular one. I started with the required step to install the operating system, and I continued with the installation of the frameworks and tools.

I believe that everyone should be able to build robots. Therefore even if you are a beginner or an advanced user, you will be able to have all the software and framework running on the Pi. All you need to do is to read with attention and follow all the steps from this tutorial.

My goal with this tutorial is to have a reliable computer that you will use to build autonomous robots and keep running as long as needed without any timeout.

If you think that this post is long and will keep you blocked for several hours, maybe one day long, you may have an alternative. You can find a list of already prepared images that runs ROS. The significant advantage is that you will have an up and running Pi in minutes at the cost of not having all the frameworks and tools covered in this post.

We are very close to starting our installations, but first, we have to define all the needed hardware resources and specifications. I already mention the version of Pi that I used in this post. Below is the complete list of all the hardware components and accessories used.

List of components:

  • Raspberry Pi 4 Model B with 4GB of RAM [Amazon link]
  • Power Supply 5V 3A USB-C adapter
  • The official 32GB MicroSD card
  • Full-size adapter for the microSD card
  • A keyboard and a mouse [Amazon link]
  • A screen with HDMI input

If you already have a Raspberry Pi 4 (any version with 1,2 or 4GB of RAM), a microSD card, and a power supply, please check the specifications and be sure to have a power supply that is Pi 4 compatible and a microSD card from this list. If not, you may have various errors during installation and are not covered in this tutorial.

Once we have the list of hardware components and accessories, we can start the setup and installation process.

Table of Contents

1. How to install Raspbian Buster on Raspberry Pi 4
2. Enable SSH for remote access
3. Install ROS Melodic
4. Install Arduino IDE and rosserial
5. Install PyCharm IDE
6. Set up Virtual Network Computing(VNC)

1. How to install Raspbian Buster on Raspberry Pi 4

In this part of the tutorial, I explain how to install Raspbian Buster on the Raspberry Pi 4. I choose to install Raspbian on Pi 4 from two reasons:

  • Raspbian is the official operating system of the Raspberry Pi computers;
  • Raspbian Buster is compatible with ROS Melodic. Melodic is a Long Term Support release which ends in 2023;

Raspbian is a free Debian-based (32-bit) computer operating system officially provided by the Raspberry Pi Foundation as the primary operating system for the family of Raspberry Pi. The operating system includes a set of basic programs and utilities that helps you to develop and execute programs. It performs as a bridge between you and the hardware of Pi and manages the CPU, memory, memory disk, GPIO (General Purpose Input/Output) pins, and more.

Installing the Raspbian operating system has become much easier by using the Raspberry Pi Imager software. The program includes the option to erase and to write the microSD memory card used for Pi.

I used the Pi Imager program to erase my memory card. Then I select from the options menu the Raspbian Buster to install on the microSD card. It did not work as expected. So, I decided to download the “Raspbian Buster with desktop and recommended software” image from the Raspberry Pi website and installed it using Raspberry Pi Imager.

1.1 Steps to follow to install Raspbian Buster on Raspberry Pi 4

Let’s download and install the Buster:
Step 1: Download the latest version of the Raspberry Pi Imager and install it. Depending on your operating system you’re using, choose one of the following links:

Step 2: Insert your MicroSD card into the memory card adapter and insert that adapter into the card reader of your computer.

Note: If the memory card used is new, you don’t need to erase and format the card. Otherwise, you have to use the Raspberry Pi Imager to erase and format the card.

Step 3: Download “Raspbian Buster with desktop and recommended software” or any of the three Raspbian versions. For this tutorial, I use the version with desktop and recommended software installed.

Step 4: Start the Raspberry Pi Imager and use the menu “Use custom Select a custom .img from your computer” to load the Raspbian image downloaded on your computer.

Select a custom .img from your computer

Step 5: Use the Imager menu to select the SD card drive.
Step 6: Click WRITE to begin writing data to the SD card. This step will take a few minutes.
Step 7: After the process of writing the image of the operating system on the memory card is finished, insert the microSD card into the Raspberry Pi’s slot and power up the board.

You will have a few steps to do. These steps include the setup of your language, a password, and connection of the board to the Internet. Once finishing the setup process, we can go further to the next step and start to enable SSH in Raspberry Pi.

2. Enable SSH for remote access

Secure Shell (SSH) is a network protocol that will allow you to operate the Pi board securely over an unsecured network. You achieve this by logging into your Raspberry Pi via SSH using the user and the IP address from your laptop, desktop, or even your smartphone.

Using this service, you can run commands on Pi without needing to plug in a display, a keyboard, a mouse, or moving yourself to the location of your Raspberry Pi each time. This service is indispensable in the situation when the Pi board is running on a mobile robot.

I assume you are familiar with the shell terminal and the basics of the command line.

Note: If you forgot to select the keyboard layout, navigate to the Raspberry Pi desktop preferences menu and use the standard US Keyboard layout unless you want to choose the one appropriate for your keyboard. For this tutorial, I use the US Keyboard layout.

Step 1: To enable SSH, open a Terminal and type:

Step 2: In the pop-up screen, navigate to ‘5 Interface Options‘ and press enter.
Select ‘P2 SSH‘, press Enter, and then select ‘Yes‘.

Select ‘5 Interfacing Options’

With the two settings above, you can use the SSH service with Pi only after you manually authenticate into the Pi using a keyboard, a mouse, and a screen. But we aim to run commands via SSH right after the Pi is powered up and connected to the Internet. The solution is to permit root login.

Step 3: Open a Terminal and run the command:

Step 4: Scroll down to ‘#Authentication‘ and uncomment the line ‘#PermitRootLogin‘ by deleting the # and then write ‘yes‘.

permit root login

Save the file with ‘Ctrl + o‘ and exit ‘Ctrl + x‘.

Step 5: Run the below command and note the IP of your Pi:

Step 6: Reboot the Pi.

At this moment, we can run commands on Pi via SSH service without using a keyboard, a mouse, and a screen for authentication. Even if we are almost ready to install the first framework on Pi, we still need to install additional software.

For SSH service, I run PuTTY on my laptop. PuTTY is a free and open-source terminal emulator, serial console, and network file transfer application that allows us to run commands on Pi.

We have an operational Pi computer, an SSH service up and running, and a terminal emulator to access the Pi via SSH. At this step, we are ready to go further and install the ROS framework.

3. Install ROS Melodic

ROS (Robot Operating System) is a framework that is running on top of Raspbian. The framework includes a set of libraries and tools that we will use to build robots.

ROS is an all-in-one solution if you want to build different robots. Once knowing how to write nodes, services, actions, and use the software packages, you can make different kinds of robots, including robot arms, drones, or autonomous mobile robots.

ROS provides a wide range of packages for almost any robotic application. Mapping, navigation and localization, are just a few examples of useful packages to build robots.

Do you want to use Python instead of C++ to control your robot? You are allowed to write your Python nodes and make it communicate with C++ nodes.

Do you want to build your robot in a virtual world? You need a simulation tool. ROS has excellent tools for simulation and visualization, such as Rviz and Gazebo.

You can run multiple independent robots and make them communicate with each other. The list with the benefits of using ROS is much longer and covers different areas in robot development.

There is currently no official ROS Melodic installation guide for the Raspberry Pi 4 and Raspbian Buster operating system. Hence we will need to build it from the source.

We are very close to beginning to run the commands to install ROS. Before running the first command, I will do a short introduction to the ROS distribution. For this tutorial and my next projects, I choose to work with ROS Melodic distribution. This distribution was released in 2018 and will have support until 2023.

Let’s begin to install ROS Melodic:
Step 1: Setting up the repositories:

Step 2: Install build tools(compiler, CMake, etc.)

  • rosdep is a command-line tool for installing system dependencies.
  • rosinstall_generator generates rosinstall files containing information about repositories with ROS packages/stacks.
  • wstool is a tool for maintaining a workspace of projects from multiple version-control systems.

Step 3: Initializing rosdep
rosdep has rules for all the dependencies, and if something is not working, it might be worth reporting it as an issue. The list of dependencies solved by rosdep is here.

Step 4: Create a catkin workspace to build the core packages.

Step 5: Fetch the core packages
The minimal Desktop install might be the preferred choice for Raspberry Pi since the board probably will be running on top of the robot. This installation doesn’t include 2D/3D simulators, navigation, and 2D/3D perception.

Note: The -j4 option downloads four packages in parallel. If you have a Pi 4 version with 1GB or 2GB of RAM and ‘wstool init’ fails or is interrupted, you can resume the download by running -j2.

Step 6: Install the compatible version of Assimp (Open Asset Import Library) to fix collada_urdf dependency problem and OGRE for rviz.

Step 7: Resolving Dependencies
We have to make sure that all the required dependencies are in place:

Step 8: Build and Source the Installation
In the steps above, we complete downloading the packages and resolving the dependencies. At this step, we are ready to build the catkin packages.

We use the catkin_make_isolated command because there are both catkin and plain cmake packages in the base install when developing on your catkin only workspaces you may choose to use catkin/commands/catkin_make which only works with catkin packages.

At this point, we finish the ROS installation. The last thing is to restart the Raspberry Pi.

After the Pi is up and running, we do a check and start a roscore session:

You should see a roscore session like in the bellow image:

roscore session

Check the list of ROS packages installed:

rospack list-names

To end the roscore session run the command Ctrl+c.

At this point, we are ready to start the work. The first thing is to create workspace to build your packages to run on the robot:

4. Install Arduino IDE and rosserial

Arduino is an open-source electronics platform able to read a sensor input and turn it into an output to control a motor. Well, you may use the Pi board as well for reading a sensor input and control the direction of the robot. Yes, this is true. But, Raspberry Pi and Arduino are quite different boards.

Arduino is a fast prototyping board and requires less effort to interface sensors and other electronic components. On the other hand, Raspberry Pi requires libraries and software for interfacing sensors and other components. If a sensor, a motor driver, or any component is controlled in a much easier manner with a microcontroller, I prefer to use Arduino instead of Raspberry Pi.

If you decide to go on and build robots only with Pi, you can ignore this part of the tutorial. But if you choose to use Arduino with ROS, then you have to go further and install the Arduino IDE and the rosserial library.

You can run ROS on Arduino UNO, Leonardo, MEGA, and DUE.

rosserial_arduino package is the protocol used for communication between Arduino’s UART and Raspberry Pi. Arduino is capable of publishing and subscribing to ROS messages, publishing TF transforms, and getting the ROS system time.

We are very close to starting the installation of the Arduino IDE. Before installing the Arduino IDE, let’s do a check regarding your system architecture. First, you need to install a ‘list hardware’ [lshw] tool to display your system architecture.

To display the architecture of the system, run the below command:

lshw

4.1 Install Arduino IDE 1.8.12

If we check the architecture of a Raspberry Pi 4, we can see that the Model B Rev 1.2 is working on 32 bits. This information is needed to download the proper software version of the Arduino IDE.

Step 1: Download the 32 bits version for Linux ARM: link to Arduino IDE.

This step may require some minutes to download the file. Navigate to the Download directory and unzip the archive.

Step 2: Open a terminal and navigate to the Download directory, then go to Arduino directory and run the command:

At this point, you have finished the installation of the Arduino IDE on Raspberry Pi. The next step is to install rosserial.

4.2 Install Rosserial from Source

Step 1: The apt-get command works on a database of available packages. The first step is to update this database. Otherwise, the system doesn’t know if there are newer packages available or not. Once we have updated the package database, we can upgrade the installed packages.

Step 2: Clone rosserial from the GitHub repository:

Step 3: Generate the rosserial_msgs needed for communication:

Step 4: Make the library files in the catkin_ws/devel/lib directory

Step 5: Install ros_lib into the Arduino Environment:

Arduino and ros_lib

Step 6: We need to source a setup.bash file in every single terminal that we will run ROS commands. These scripts control many important ROS environment variables and non-ROS environment variables.

Step 7: This command is to import the rosserial library into Arduino IDE.

Let’s do a check if everything works as expected. Open the Arduino IDE and navigate to File -> Examples -> ros_lib. Select an example, open it, and then compile the file.

Arduino hello world program

With all of the above steps, we finish the installation of the Arduino IDE and the rosserial library. At this point, we can use Arduino with Raspberry Pi to publish sensor inputs or read commands for a motor driver.

5. Install PyCharm IDE

In this tutorial, I wrote about the frameworks and software we need to build robots. An important topic that I didn’t say anything about is the programming language.

I prefer to run Python on Raspberry Pi and Arduino[C++] on Arduino. If you plan to run on the Pi other programming language than Python, you can ignore this part of the tutorial and go further to the next topic. In this part, I will show you how to check the Python version installed on Raspbian and install a Python IDE.

If you are new on PyCharm IDE for building your code, it’s worth to take a look. You have two options to download: one free community edition and a paid version with additional features.

In this tutorial, I will show you how to install the PyCharm Community Edition IDE.

At this point, we are very close to starting running commands and installing the IDE.

Raspbian Buster has Python 3 installed, and you don’t have to do any additional installation. To check your Python version, run the below command:

$python3 –version

We are very close to starting installing the PyCharm IDE.

Step 1: First, let’s start to download the community version of the tool. The link to download the Linux version of PyCharm version 3.3.

Step 2: After the download is finished, go to the Download directory and unzip the file.

Step 3: Go ahead and open a Terminal and navigate to the Download directory. Move the file to /opt, which is a directory where we will install the unbundled packages. The last step is to run the shell executables file to install the IDE.

Note: If during the installation you are getting a Java error on the command, run: $sudo apt-get install openjdk-11-jdk

Step 4: If the installation works smoothly, a pop-up window will appear. Fill the steps with your custom preferences.

  1. From the Tools menu, select “Create Desktop Entry…”
  2. Tick the checkbox if you want the launcher for all users.
  3. When you will be asked, add your password.
  4. Navigate to Application menu -> Programming, and you should see the PyCharm Community Edition in the list.

6. Set up Virtual Network Computing(VNC)

Almost all the time, the Raspberry Pi board will run on top of the robot. There are actions in which an SSH service cannot be used. Instead, we can use the VNC service to have access to the graphical user interface (GUI) when working on the remote machine.

To use VNC, we need a VNC server running on Pi and a VNC client on the local machine. Additionally, we need to know the IP address of the Pi, and the username and password for authentication.

Before installing the VNC service, we have to check the services and options that allow us to run a virtual network.

Step 1: We have to start the GUI/desktop manager service. The Pi must be set to boot to the desktop (service mode). Because we will use Pi in our projects without an HDMI monitor attached to it, we need to specify a screen resolution.

To change the resolution, run the command:

Step 2: Navigate to Advanced Options -> Resolution, and choose an option.

Step 3: Reboot your Pi.

6.1 Install the VNC server on Pi

Step 1: Run the command:

Step 2: Navigate to the ‘5 Interfacing Options‘ menu item and press Enter.
Then navigate to ‘P3 VNC‘ press Enter

P3 VNC

Choose Yes and then press Enter. Then select OK (Enter).

You will be returned to the main menu and choose Finish.

Step 3: Reboot your Pi.

6.2 Install VNC client on your local machine

I used the Real VNC client because it is available for macOS, Linux, Windows, and other operating systems. Choose the right version for your operating system and download the viewer from here.

Once you have a VNC server running on your Pi and the VNC client running on your local machine, follow the steps in:

  1. Enter the IP address of the Pi.
  2. Enter the username and password for the account on the remote system.
  3. Connect to the Pi with GUI.

Summary

You now have a Raspberry Pi board prepared to start running algorithms and communicate with microcontroller platforms.

We start with the Raspbian installation on the Pi and continue with enabling the SSH service that allows us to run commands on Pi over an unsecured network.

Then we continue with ROS Melodic, Arduino IDE, and rosserial installation. All of these software libraries and tools are the foundation to control robots.

Finally, I show you how to install a popular Python IDE and set up the VNC service to have access to the graphical user interface (GUI) when working on the remote machine.

If you have any questions, please feel free to leave a comment in the comments section!

References:

  • Installing operating system images, raspberrypi
  • Installing from source, ros
  • ROS Melodic on Raspberry Pi 4[Debian Buster] + RPLIDAR A1M8, instructables
  • Arduino Setup, ros
  • Arduino Software (IDE), arduino
  • rosserial, ros
  • Installing PyCharm on Raspberry Pi 4, element14
  • Remote development on the Raspberry Pi (or Amazon EC2), pyimagesearch

Share:

Related Posts

Don't Miss Out!

Get the latest news, tutorials, reviews and more direct to your inbox when you subscribe!