Introduction

This instruction covers the installation of ROS Kinetic (Robot Operating System) and OpenCV-3.3.1 on the Raspberry Pi 2 or 3 with Raspbian Stretch.

I have prepared an img file (made on Dec 19 2017) that compiled ROS and OpenCV, you can download it here, the file size is around 4GB in Google Drive. You can download, unzip and burn it to your SD card (at least 16GB) if you don’t care about the installation details.

If you want to make your own installation, please see the following steps.

Step 1: Install pure Raspbian OS

Download the official image file “RASPBIAN STRETCH WITH DESKTOP” (Image with desktop based on Debian Stretch, Version: November 2017, Release date: 2017-11-29, Kernel version: 4.9).

Unzip the file, and burn the img file to an SD card (at least 16GB) using Win32 Disk Imager for Windows or Pi Filler (easy use) for macOS.

Note: Choose “Enable SSH” option if Pi Filler is used.

Step 2: Install ROS Kinetic

Before we install ROS, it’s recommended that increase the swap space size of Pi for faster compiling, but it is optional.

Increase swap space size:

Open /etc/dphys–swapfile using sudo nano, edit CONF_SWAPSIZE variable (change 100MB to 1024MB):

# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=1024

Now we need to activate new swap space:

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

Note: After installing ROS, swap space MUST be changed from 1024 to 100.

Add ROS repo to apt list:

In Raspbian Stretch, there is an error for a missing package, we need to fix it firstly by installing dirmngr for certificate management.

$ sudo apt-get install dirmngr

Then, we can add repo:

$ sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

Update Debian package:

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

Install Bootstrap Dependencies:

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

Install rosdep (package manager):

$ sudo rosdep init
$ rosdep update

Create a catkin Workspace:

$ mkdir ~/ros_catkin_ws
$ cd ~/ros_catkin_ws

Build packages and libraries:

I chose to install ROS-Comm for minimal installation.

ROS-Comm: ROS package, build, and communication libraries. No GUI tools.

$ rosinstall_generator ros_comm --rosdistro kinetic --deps --wet-only --tar > kinetic-ros_comm-wet.rosinstall
$ wstool init -j8 src kinetic-ros_comm-wet.rosinstall

-j8 is for faster processing.

If “wstool init” fails or is interrupted, you can resume the download by running (this step may NOT be necessary):

$ wstool update -j4 -t src

Make sure all dependenices are installed:

$ rosdep install --from-paths src --ignore-src --rosdistro kinetic -y

Build ROS:

$ sudo mkdir -p /opt/ros/kinetic
$ sudo chown pi:pi /opt/ros/kinetic
$ ./src/catkin/bin/catkin_make_isolated -j2 --install --install-space /opt/ros/kinetic -DCMAKE_BUILD_TYPE=Release

-j2 is used because of limited memory of Pi, default parameter is -j4, -j4 may cause error.

Source the new installation:

$ source /opt/ros/kinetic/setup.bash

ROS environment variables could be checked by the following command:

$ export | grep ROS

Now ROS should be installed successfully, remember to change swap space back to 100MB.

Step 3: Install OpenCV

OpenCV installation should be very easy now, there are many good online guides, I won’t repeat here, I recommend using this OpenCV installation guide done by Adrian Rosebrock.

In the above ready-to-use img file, it has installed OpenCV-3.3.1 with opencv_contrib (include SIFT, SURF, and other features), OpenCV is installed in global, NOT in a virtual environment as in the Adrian guide, both python2 and python3 can use OpenCV.

If you don’t know how to use OpenCV in ROS, just take a look at cv_bridge in ROS wiki.

Summary

Now you should install ROS and OpenCV on your Raspberry Pi successfully, but this guide is not only for the specific version of Raspberry Pi and OpenCV, you also can try with other versions.