Wednesday, May 6, 2020

Installing ROS on a Raspberry Pi- Part 1

This spring I have been working with students in Barcelona on an autonomous vehicle project. They have been using a motorized car with an Arduino Uno and sensors, with a goal of upgrading to a Raspberry Pi and doing machine learning.
For cost reasons, they have selected the Raspberry Pi 3 Model A+; however I have some concerns about whether this has the performance needed for the project. As an early test case, I want to document what steps need to be taken to get ROS (Robot Operating System) running on the R-Pi.

What is ROS?


"The Robot Operating System (ROS) is a set of software libraries and tools that help you build robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what you need for your next robotics project. And it's all open source."

The latest release of ROS is "Melodic Morenia" and is built for Ubuntu Linux; it is also possible to install ROS onto Raspbian, which we will attempt here.

R-Pi Features and Limitations

The $25 Raspberry Pi 3 Model A+ (https://www.raspberrypi.org/products/raspberry-pi-3-model-a-plus) has the following specs:
  • Extends the Raspberry Pi 3 range into the A+ board format.
  • Broadcom BCM2837B0, Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz
  • 512MB LPDDR2 SDRAM
  • 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN, Bluetooth 4.2/BLE
  • Extended 40-pin GPIO header
  • Full-size HDMI
  • Single USB 2.0 port
  • CSI camera port for connecting a Raspberry Pi Camera Module
  • DSI display port for connecting a Raspberry Pi Touch Display
  • 4-pole stereo output and composite video port
  • Micro SD port for loading your operating system and storing data
  • 5V/2.5A DC power input
The biggest limitations are the amount of SDRAM, followed by the ARMv8 performance. Also only having a single USB port will limit the peripherals we can connect without an external hub.

Our initial hardware components are:
  • Raspberry Pi 3 Model A+
  • 16GB Class 10 micro SD card
  • 5V/2.5A  micro-USB power supply
  • Video monitor
  • Wired USB keyboard
    • While it is possible to install using just a keyboard, a mouse makes life easier
    • You can use a keyboard with a built-in track pad
    • Connect a USB hub to the model A+ to add more ports
      • A keyboard with a USB port for a mouse
      • A monitor with a USB port to connect a keyboard and a mouse
      • A stand-alone hub with ports for more devices
  • The Model A+ has built-in WiFi which we will use to connect to the Internet

Installing Raspbian

In an earlier blog post, we walked through how to set up a Raspberry Pi as a headless system; in this article we will have a keyboard and display, and can use the Raspbian keyboard shortcuts instead of a mouse to access the desktop GUI.

Install the operating system image

Rather than downloading an OS image and using a third party app (e.g. Balena Etcher) to burn the image onto the SD card, we will instead use the Raspberry Pi Imager. This is easiest to use if you only want the OS latest release (in this case "buster") and want to overwrite files on the SD card without having to manually delete its existing disk partitions.
  • Download Raspberry Pi Imager
  • Insert  the micro-SD card into a card reader on your computer
  • Run the executable and under the 'Operating System" selection, choose
    • 'Erase' to format the SD card
    • 'Raspbian' to get the recommended Raspberry Pi Desktop
  • Close the Imager and eject the SD card

Setup the Pi

  • Insert the micro SD card that has the OS image on it into the Pi's card slot
  • Plug in your devices 
    • HDMI video cable
    • USB keyboard
    • Micro-USB power
  • Turn on the power
  • Once the system boots into the desktop GUI, follow the wizard's prompts to complete setup.
    • Select your country/language/timezone/keyboard
    • Change the 'pi' user password
    • Set up screen size if needed
    • Select a WiFi network
    • Enter the WiFi password and connect to the network
    • Update software
  • After updates complete, restart the Raspberry Pi and login to the GUI
  • If you did not successfully update the OS during installation, you can do so manually:
    • From the GUI, open a CLI terminal session: <CTRL> + <ALT> + t
    • Run the commands
$ sudo apt update
$ sudo apt dist-upgrade
$ sudo apt clean
  • Setup remote access for headless operation:
    • From the GUI, open a CLI terminal session: <CTRL> + <ALT> + t
    • Use 'sudo' to run 'raspi-config'
    • Change the user password if needed
    • Update network connection options
    • Select 'Interfacing Options' to
      • Enable SSH
      • Enable VNC
    • You can find the ip addess for the Raspberry Pi with the command 'hostname -I'
  • Since the Model A+ only has 512MB of ram, you will need to increase swap:
    • Check swap size by running the CLI command 'free -h'
    • Edit the /etc/dphys-swapfile to set CONF_SWAPSIZE=1024
    • Reboot the Raspberry Pi for this to take effect.
    • Confirm swap change by re-running 'free -h'

Installing ROS

ROS is originally targeted for Ubuntu, so we will need to build from source to run on Raspbian.
We will be working in our default directory "/home/pi" and following the steps detailed here: http://wiki.ros.org/ROSberryPi to install ROS Melodic on Raspbian Buster :
  • From the GUI
    • Open a CLI terminal session: <CTRL> + <ALT> + t
    • Execute the commands listed in the guide to:
      • Configure prerequisites
        • Setup ROS Repositories and keys
$ 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 C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
$ sudo apt-get update
$ sudo apt-get upgrade
        • Install Bootstrap Dependencies
$ sudo apt install -y python-rosdep python-rosinstall-generator python-wstool python-rosinstall build-essential cmake
        • Initialize rosdep
$ sudo rosdep init
$ rosdep update
      • Install ROS
        • Create a catkin workspace
$ mkdir -p ~/ros_catkin_ws
$ cd ~/ros_catkin_ws
        • Fetch the core packages for either
          • ROS-Comm (a basic configuration that has been tested)
$ rosinstall_generator ros_comm --rosdistro melodic --deps --wet-only --tar > melodic-ros_comm-wet.rosinstall
$ wstool init src melodic-ros_comm-wet.rosinstall
          • ROS Desktop (GUI configuration)
$ rosinstall_generator desktop --rosdistro melodic --deps --wet-only --tar > melodic-desktop-wet.rosinstall
$ wstool init src melodic-desktop-wet.rosinstall
        • Resolve dependencies by running rosdep
$ cd ~/ros_catkin_ws
$ rosdep install -y --from-paths src --ignore-src --rosdistro melodic -r --os=debian:buster
        • Build the catkin workspace using 'catkin_make_isolated' with the '-j1' option to limit the number of CPU threads used on the ARMv8 core
$ sudo ./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release --install-space /opt/ros/melodic -j1
        • Wait a long time for almost 200 packages to compile... :-)
        • Once the install is complete, configure your environment
$ source /opt/ros/melodic/setup.bash
        • You can also setup your bash shell to always have variables set correctly:
$ echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc

Installing ROS on a Raspberry Pi- Part 2

In the previous post, we installed Raspbian 'buster' and ROS 'melodic' onto a Raspberry Pi; here we will test its functionality.

Login to the RPi

Since as part of the Raspbian install we enabled 'headless' mode, let's access its desktop using the 'VNC Viewer' client.
  • Download and install vnc viewer.
  • Find the IP address of your Raspberry Pi on your network.
    • Open a CMD or terminal window on your computer and run the command 'arp -a' and look for the Raspberyy Pi's physical MAC address that starts with 'b8-27-eb' (we can take advantage of the 'pipe' and 'findstr' commands in the Windows 10 Command Prompt to filter the results returned):
> arp -a | findstr " b8-27-eb-"
    192.168.1.126         b8-27-eb-3b-f2-90     dynamic
  • Use the IP address found to login to the Raspberry Pi via VNC Viewer:
  • You will need to enter the password for the 'pi' user that you created as part of the Raspbian configuration steps you did in the first part of this series.

  • Now that we have access to the desktop on the headless system, we can use our local keyboard and mouse as if we were on the system itself. Note that when there is no physical display connected to the Raspberry Pi, it will default to a low-resolution desktop. To get a HD display, you will need to edit the /boot/config.txt file as follows:
    • Un-comment the following lines:
      • hdmi_force_hotplug=1
      • hdmi_group=1
      • hdmi_drive=2
    • Edit the following line to change the value from '1' to '16' to force display to 16:9 1080p
      • hdmi_mode=16
    • Comment out the following line:
      • dtoverlay=vc4-fkms-v3d

Check the ROS installation

  • Open a terminal session and create a catkin workspace in your home directory:
$ mkdir -p ~/catkin_ws/src  
$ cd ~/catkin_ws/  
$ catkin_make
$ source devel/setup.bash
  • Run 'roscore' to check the install:
$ roscore
... logging to /home/pi/.ros/log/8e91c8a2-8fdf-11ea-a15e-b827eb3bf290/roslaunch-rospi-8768.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://rospi:35989/
ros_comm version 1.14.5

SUMMARY
========

PARAMETERS
 * /rosdistro: melodic
 * /rosversion: 1.14.5

NODES

auto-starting new master
process[master]: started with pid [8778]
ROS_MASTER_URI=http://rospi:11311/

setting /run_id to 8e91c8a2-8fdf-11ea-a15e-b827eb3bf290
process[rosout-1]: started with pid [8790]
started core service [/rosout]
  • Use <CTL>- C to exit roscore
  • Continue on with these ROS tutorials.