Installing ROS Melodic on Raspbian Stretch

On this page, you can download Rapbian Stretch image with ROS Melodic (ros_core) installed. Purpose of this image is to have standalone ROS system running on RaspberryPi device connected to Arduino.
The image requires at least 4GB SD card and works on RaspberryPi ZeroW device. Bellow you can find detailed creation process of this image.

If you have any question, feel free to contact me

Starting with Raspbian Image (on PC)

Our image is based on standard Raspbian Stretch image from 2019-04-08.
dd bs=4M if=2019-04-08-raspbian-stretch-lite.img of=/dev/mmcblk0 status=progress
mkdir /mnt/boot/
mount /dev/mmcblk0p1 /mnt/boot/
touch /mnt/boot/ssh
Boot created image in Raspberry and log in as pi user..

System updates

sudo apt-get update
sudo apt-get upgrade

sudo rpi-update

sudo reboot

sudo apt-get install vim less unzip unrar-free uptimed screen
sudo apt-get install build-essential cmake python python3 git

GCC9

Since we were not able to build ROS with bundled GCC (internal compiler error), we used newer GCC version (original install procedure can be found here).
git clone https://bitbucket.org/sol_prog/raspberry-pi-gcc-binary.git

cd raspberry-pi-gcc-binary
tar xf gcc-9.1.0-armhf-raspbian.tar.bz2
rm gcc-9.1.0-armhf-raspbian.tar.bz2
sudo mv gcc-9.1.0 /opt

echo 'export PATH=/opt/gcc-9.1.0/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/opt/gcc-9.1.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
sudo ln -s /usr/include/arm-linux-gnueabihf/sys /usr/include/sys
sudo ln -s /usr/include/arm-linux-gnueabihf/bits /usr/include/bits
sudo ln -s /usr/include/arm-linux-gnueabihf/gnu /usr/include/gnu
sudo ln -s /usr/include/arm-linux-gnueabihf/asm /usr/include/asm
sudo ln -s /usr/lib/arm-linux-gnueabihf/crti.o /usr/lib/crti.o
sudo ln -s /usr/lib/arm-linux-gnueabihf/crt1.o /usr/lib/crt1.o
sudo ln -s /usr/lib/arm-linux-gnueabihf/crtn.o /usr/lib/crtn.o

export LIBRARY_PATH=/lib/arm-linux-gnueabihf/:/usr/lib/arm-linux-gnueabihf
export C_INCLUDE_PATH=/usr/include/arm-linux-gnueabihf/
export CPLUS_INCLUDE_PATH=/usr/include/arm-linux-gnueabihf/

gcc-9.1 --version

ROS

Installation from sources

More detailed procedure is here and here
sudo apt-get install dirmngr

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

sudo apt-get update
sudo apt-get upgrade

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

sudo rosdep init

rosdep update

mkdir -p ~/ros_catkin_ws
cd ~/ros_catkin_ws

# build console_bridge dependency
mkdir src
cd src
git clone https://github.com/ros/console_bridge.git
cd ..
mkdir -p build_isolated/console_bridge
cd build_isolated/console_bridge
cmake -DCMAKE_INSTALL_PREFIX=../../install_isolated ../../src/console_bridge/
make install
cd ../../

rosinstall_generator ros_core --rosdistro melodic --deps --tar > melodic-ros_core.rosinstall
wstool init -j4 src melodic-ros_core.rosinstall

rosdep install --from-paths src --ignore-src --rosdistro melodic -y --os=debian:stretch

sudo apt-get install libboost-thread1.62-dev libboost-system1.62-dev libtinyxml2-4

vi src/catkin/cmake/tools/rt.cmake              # add OR UNIX
        if(NOT (APPLE OR WIN32 OR MINGW OR ANDROID OR UNIX))

/opt/gcc-9.1.0/bin/gcc-9.1 -print-search-dirs

export LIBRARY_PATH=/lib/arm-linux-gnueabihf/:/usr/lib/arm-linux-gnueabihf
export C_INCLUDE_PATH=/usr/include/arm-linux-gnueabihf/
export CPLUS_INCLUDE_PATH=/usr/include/arm-linux-gnueabihf/

# increase swap file size
free -m
vi /etc/dphys-swapfile
        CONF_SWAPSIZE=1024
/etc/init.d/dphys-swapfile restart
free -m

./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/opt/gcc-9.1.0/bin/gcc-9.1 -DCMAKE_CXX_COMPILER=/opt/gcc-9.1.0/bin/g++-9.1 -DPYTHON_INCLUDE_DIR=/usr/include/python2.7/ -DPYTHON_LIBRARY=/usr/lib/python2.7/config-arm-linux-gnueabihf/libpython2.7.so

sudo mkdir -p /opt/ros/melodic/
sudo cp -r install_isolated/* /opt/ros/melodic/

Usage

Now ROS is ready. Before usage we need source setup.bash and since we have used newer GCC, we need to include it in LD path:
source /opt/ros/melodic/setup.bash
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc

export LD_LIBRARY_PATH=/opt/gcc-9.1.0/lib/:$LD_LIBRARY_PATH

Test

If everything went ok, following should work:
roscore

rosnode list

rosnode info /rosout

rosserial

For communication with Arduino, we need rosserial package
git clone https://github.com/ros-drivers/rosserial
cd rosserial
git checkout melodic-devel
cd ..

mkdir catkin_ws
cd catkin_ws
mkdir src
catkin_make

cp -r ../rosserial/rosserial_python/ src/
cp -r ../rosserial/rosserial_msgs/ src/
catkin_make
catkin_make install

source devel/setup.bash
echo $ROS_PACKAGE_PATH

Arduino

Since we expect permanent connection between RPI and Arduino (via logic level shifter), it is convenient to program Arduino using RPI.
sudo apt-get install avra avrdude

sudo apt-get install python-serial

# https://github.com/TOLDOTECHNIK/avrdude-gpio-autoreset
sudo mv /usr/bin/avrdude /usr/bin/avrdude-original
sudo vi /usr/bin/avrdude
        #!/bin/sh
        strace -o "|avrdude-gpio-autoreset" -eioctl /usr/bin/avrdude-original $@
sudo chmod +x /usr/bin/avrdude

git clone https://github.com/TOLDOTECHNIK/avrdude-gpio-autoreset.git
sudo mv avrdude-gpio-autoreset/bin/avrdude-gpio-autoreset /usr/bin/

sudo raspi-config
        Interfacing Options -> Serial
                login shell -> No
                serial port hw -> Yes

sudo avrdude -D -V -F -c arduino -p m328p -P /dev/ttyS0 -U flash:w:Blink.ino.hex:i

sudo avrdude -D -V -F -c arduino -p m328p -P /dev/ttyS0 -U flash:w:comm-arduino.ino.hex:i

Access Point

To be able to communicate with our robot, setup wifi access point (from here).
sudo apt-get install dnsmasq hostapd
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd

sudo vi /etc/dhcpcd.conf
        interface wlan0
                static ip_address=192.168.4.1/24
                nohook wpa_supplicant
sudo service dhcpcd restart

sudo vi /etc/dnsmasq.conf
        interface=wlan0
        dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

sudo vi /etc/hostapd/hostapd.conf
        interface=wlan0
        driver=nl80211
        ssid=RaspbianROS
        hw_mode=g
        channel=7
        wmm_enabled=0
        macaddr_acl=0
        auth_algs=1
        ignore_broadcast_ssid=0
        wpa=2
        wpa_passphrase=RasPasROS
        wpa_key_mgmt=WPA-PSK
        wpa_pairwise=TKIP
        rsn_pairwise=CCMP
sudo vi /etc/default/hostapd
        DAEMON_CONF="/etc/hostapd/hostapd.conf"

sudo systemctl start hostapd
sudo systemctl start dnsmasq
If there is a masked problem with starting hostapd, use following commands:
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd