ROS Melodic installation on Raspberry Pi 4 with Raspberry Pi OS - brennanyama/RobotOperatingSystem GitHub Wiki

ROS Melodic installation on Raspberry Pi 4 with Raspberry Pi OS

1. Hardware Setup

  • Raspberry Pi 4 Model B 8GB (4GB or 8GB version preferred)
  • A microSD card. Generally any model will do, preferably 128GB or greater and Class 10 or above.
  • Raspberry Pi CPU cooler
    • A cooler is required to get the full performance out of the Pi. Coolers integrated with cases for the Raspberry Pi are common, but for embedded applications, a case is not strictly necessary and limits cooling potential. Make sure you're checking that the cooler is compatible with anything you want to attach to the Raspberry Pi, particularly with the GPIO pin headers.
  • Voltage regulator to 5V. This is usually in the form of a buck (step down) regulator from a higher voltage source.
    • Note: you could use the USB-C port to provide power to the Raspberry Pi 4 in a pinch; however, the Raspberry Pi 4 requires much more power than previous models, and we found that most USB-C chargers (even fast charge models capable of 3A down the 5V line) result in under-voltage warnings, even before overclocking. The best method of ensuring consistent power under load is an external 5V regulator directly to the 5V line of the Raspberry Pi; ensure the regulator outputs a clean signal and is capable of at least 5A.
  • A monitor (preferably a portable one for field work) with the appropriate display adapters and cables.
    • Note: the Raspberry Pi 4 uses a microHDMI (unlike older models which use the standard HDMI connector)
    • Note: if you do purchase a portable monitor, make sure that you pick one that supports display over HDMI or DisplayPort (the two modern display standards). Do not pick a monitor that uses “DisplayLink” (requires a Windows-only software driver that sends display over USB 3.0), and do not pick a monitor that only supports display over USB-C (the Raspberry Pi 4 has a USB-C port, but it is for charging only and does not support display output). Ideally this monitor should support display over HDMI and power via USB or some type of direct power input jack (like a barrel connector). We used both this one and this one.
  • A keyboard and mouse.

2. Raspberry Pi OS installation

We will be installing ROS on top of the native Raspberry Pi OS. Note that previous versions of ROS only directly supported Ubuntu Mate for Raspberry Pi; however, ROS Melodic (and forward) now directly supports the native Raspberry Pi OS.

First, download the Raspberry Pi OS with desktop and recommended software from this link. You will also need a software for flashing the OS to the microSD card; we use the recommended balenaEtcher software from this link. Flash the downloaded Raspberry Pi OS .iso file to the microSD card. Insert the microSD card into the Raspberry Pi and power it on by supplying power into the voltage regulator.

Run all initial setups of the OS, including setting timezone, password, wi-fi, display settings, and initial software updates (apt-get update, apt-get upgrade, apt update, apt upgrade).

2.1. Optional: Overclock Raspberry Pi

Overclocking will allow us to maximize computational performance from the Raspberry Pi; however, it comes at the cost of significantly higher power draw and heat output. Only do this if using the Raspberry Pi with a strong cooler and a good power supply (as previously described). Install using:

sudo nano /boot/config.txt

Add the following text anywhere in the document (preferably at the beginning just after the header lines):

# overclock settings
over_voltage=6
arm_freq=2000
gpu_freq=750

Save and close the file. Reboot the Raspberry Pi by cutting power or:

sudo reboot -h now

Use the Raspberry Pi and check that the system is still stable with the applied overclock.

2.2. Optional: CPU monitoring

When testing real-time measurement processes, it often helps to know if the CPU is imposing performance constraints on the system. A useful tool for monitoring CPU performance (and other metrics) is nmon. Install using:

sudo apt-get install nmon

To run:

nmon

Then type ‘c’ in the active terminal window to view CPU usage.

3. ROS Melodic installation

Following the installation instructions given on this link.

Add the proper ROS repositories:

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

Add the official ROS keys:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

Update apt repositories:

sudo apt-get update
sudo apt-get upgrade
sudo apt update
sudo apt upgrade

Install build dependencies:

sudo apt install -y python-rosdep python-rosinstall-generator python-wstool python-rosinstall build-essential cmake

Initialize rosdep:

sudo rosdep init

Update rosdep:

rosdep update

Create a directory for your catkin workspace:

mkdir -p ~/Ros/Workspaces/catkin_ws
cd ~/Ros/Workspaces/catkin_ws

Generate a list of Melodic dependencies for the ros_comm variant of ROS:

rosinstall_generator ros_comm --rosdistro melodic --deps --wet-only --tar > melodic-ros_comm-wet.rosinstall

Use the wstool to fetch all the remote repos locally to the src folder:

wstool init src melodic-ros_comm-wet.rosinstall

Before compiling the packages in the src folder, resolve all system dependencies using rosdep install:

cd ~/Ros/Workspaces/catkin_ws
rosdep install -y --from-paths src --ignore-src --rosdistro melodic -r --os=debian:buster

Note: if using a 2GB Raspberry Pi, you may run into low memory errors when building, you can preemptively prevent issues by increasing your swap space. See section 3.1. Optional: increase swap space. These steps are not necessary if using a higher memory variant of the Raspberry Pi (4GB+).

Build the catkin workspace using catkin_make_isolated:

sudo ./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release --install-space /opt/ros/melodic

ROS Melodic is now installed. You must source the installation to each new terminal window using:

source /opt/ros/melodic/setup.bash

Alternatively, you can add the setup.bash source to your .bashrc file located in ~/.bashrc. You can edit the .bashrc manually, or use:

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

3.1. Optional: increase swap space

You should do this is you run into memory errors when building your catkin workspace.

First, turn off swap:

sudo dphys-swapfile swapoff

Then edit your swap file:

sudoedit /etc/dphys-swapfile

Change the line CONF_SWAPSIZE=100 to CONF_SWAPSIZE=1024. This will increase the swap size from 100MB to 1024MB. Save and close the file. Commit the changes to swap:

sudo dphys-swapfile setup

Turn swap back on:

sudo dphys-swapfile swapon