ROS Raspberry Pi 4B Setup
This tutorial will walk you through how to get Ubuntu Linux and ROS working on a Raspberry Pi 4B from scratch. By way of introduction, many types of operating systems exist for the Pi. This includes Ubuntu, Debian, and Raspbian. However, in order to get optimal compatibility with ROS, Ubuntu is really the way to go. As it turns out, the folks that maintain Ubuntu (a.k.a Canonical) maintain support for the Raspberry Pi 2, 3, and 4 as well. You can download these images from the Ubuntu Raspberry Pi page.
Now, as the name suggests, these are Server images, so they do not come with desktops. Similar to the operating systems, there a quite a number of desktop experiences one can choose from - like ubuntu-desktop, kubuntu-desktop, xubuntu-desktop to name a few. After doing some experimenting, We’ve found that the ubuntu-mate-desktop experience provided a pretty nice desktop experience and took up the least amount of RAM.
Since we’re using the Pi 4, and the 64-bit has better performance, you should choose the 64-bit option of the Ubuntu distro you want to use. In this guide, we will installed Ubuntu 20.04.2.
Hardware Requirements
Instead of purchasing components separately, it’s much easier to get a Raspberry Pi 4B Kit (from
Amazon or wherever) that comes with all necessary components. This includes a power cable,
mini-to-full-size HDMI cable, case, fan, heatsinks, a class 10 microSD card (at least 32GB), and of
course, the Pi itself (4GB RAM option seems to be pretty good, and is what we use at Interbotix).
Note that using less than 4GB RAM on the RPi 4 can result in it freezing during code compilation
(like when running the installation script for any of our RPi compatible robots). To get around
this, you can change any mention of the colcon build
command in the robot installation scripts
to colcon build --executor sequential
.
- Raspberry Pi 4B Kit
- HDMI monitor, mouse, Ethernet cable (that connects to Internet), and keyboard
- A laptop or desktop computer
Setup
Flashing Ubuntu
- First, download the Ubuntu 20.04.2 64-bit Server Image for the Raspberry Pi 4.
- Using a tool like balenaEtcher, flash the OS image to the 32GB microSD card using your laptop or desktop computer. The process will take a few minutes.
- When done, eject the SD card and insert it into the Pi 4. Then connect the HDMI monitor, mouse, keyboard, and Ethernet cable to the Pi. Finally, connect the power cable, and turn the Pi on.
- After waiting a minute or so (in which time, some debugging messages will appear on the
screen), you will be prompted to login. The default computer and user name is
ubuntu
, so just typeubuntu
. At this point, you will be prompted to create a password which you should do.
Note
Note that you must plug in the Ethernet and HDMI cables into the Pi before turning the Pi on.
Modifying Username/Hostname
Depending on your application, you may want to change either your username or computer name to be
something more unique than ubuntu
. The commands below illustrate how to modify the default
ubuntu
username to pibot
.
To modify the
ubuntu
user name to something else (in this casepibot
), we need to add a temporary new user to do the modifications.$ sudo adduser temp $ sudo adduser temp sudo $ exit
Now login to the
temp
user, at which point you can modify theubuntu
user to bepibot
.$ sudo usermod -l pibot ubuntu $ sudo groupmod -n pibot ubuntu $ sudo usermod -d /home/pibot -m pibot $ sudo usermod -c "pibot" pibot $ exit
Now login to the
pibot
user, at which point you can delete the temporarytemp
user…$ sudo deluser temp $ sudo rm -r /home/temp
To change the default computer name, follow the commands below. This is useful if you plan to have multiple Pis on the same network later.
Change the ‘hostname’ (a.k.a computer name) from ‘ubuntu’ to ‘pibot’ in the file below.
$ sudo nano /etc/hostname
At this point, it is a good idea to reboot to let the changes take effect. Before doing so, first type the command below to check if Linux is in the process of updating behind the scenes. It does this automatically for security updates.
$ ps aux | grep -i apt
If you see apt.systemd.daily is using the apt process, just wait a few minutes until it’s done. You can verify that it’s done by running the above command every few minutes until apt.systemd.daily stops showing up. At that point, type:
$ sudo reboot
Installing Ubuntu MATE Desktop
Now let’s do system updates.
Login and type…
$ sudo apt update && sudo apt upgrade $ sudo reboot
Finally, install the desktop. The process should take about 20 minutes. Note that a message will pop up in the middle of the install asking you to choose between ‘lightdm’ and ‘gdm3’ as the default display manager. As Ubuntu Mate uses the ‘lightdm’ display manager, make sure to select ‘lightdm’.
$ sudo apt install ubuntu-mate-desktop $ sudo reboot
At boot, the monitor should now display a login screen (instead of the terminal prompts from before). Before logging in, click the Ubuntu sign next to the username text box, and select ‘MATE’ as the desktop environment. Then login. If you’d like to enable automatic login from now on…
- Click the Power Icon on the top right of the desktop followed by System Settings.
- Click Login Window.
- Go to the Users tab.
- Enter
pibot
for the Automatic login text box, then exit.
Custom Boot Configs
By default, the Raspberry Pi 4B has a max CPU clock frequency of 1.5 GHz. However, this can be upped to 2 GHz by following the commands below.
Modify the boot config file
$ cd /boot/firmware/ $ sudo nano usercfg.txt
Add the following lines to the file above.
over_voltage=6 arm_freq=2000
To exit nano, use Ctrl + S to save, and Ctrl + X to exit the application.
Save the file and reboot
$ sudo reboot
Note
Overclocking should only be done if the Pi has onboard heat sinks and an active cooling mechanism (i.e. a fan).
Privileges
As with any Linux system, some operations will require the sudo
command (with password) to
work. This can become cumbersome if you are trying to automate a process on your Pi, and can’t
because somebody needs to enter a password (as is the case when it comes to controlling NeoPixel
LEDs). To fix this, it is possible to give a user (in this case pibot) sudo privileges without
requiring a password. Just open a terminal and type sudo visudo
. This should open up a file at
the bottom of which, you should add the following line:
pibot ALL=(ALL) NOPASSWD:ALL
Fix Bluetooth Issue
For whatever reason, the Bluetooth module on the Pi 4 is by default disabled. To enable on Ubuntu
20.04, just type sudo apt install pi-bluetooth
in a terminal. If, for whatever reason, Ubuntu
18.04 is installed on your Pi, it’s a bit more complicated. Just open a terminal and type the
following.
Add the ubuntu-pi-flavour-makers repo that contains the pi-bluetooth package that’s necessary to enable Bluetooth on the Pi and install pi-bluetooth.
$ sudo add-apt-repository ppa:ubuntu-pi-flavour-makers/ppa $ sudo apt purge linux-firmware-raspi2 $ sudo apt install pi-bluetooth
Note that you will see a message about the hciuart.service not working correctly. This is ok though as after a reboot, it will work just fine.
$ sudo apt install linux-firmware-raspi2
After running the command above, you will see an error message about one package trying to overwrite a file in another package. This can be fixed by typing.
$ sudo dpkg -i --force-all /var/cache/apt/archives/linux-firmware-raspi2_XXXX_arm64.deb #(The Xs are some arbitrary numbers which can be copied from the error message) $ sudo apt update && sudo apt upgrade $ sudo reboot
The Bluetooth fix mentioned above can be found in this Ubuntu Mate community thread.
Install ROS
At this point, you can follow the instructions on the ROS Wiki to install the distro compatible with your Ubuntu version. Because we installed Ubuntu 20.04, we will now install ROS Galactic.
Alternatively, you can use the xsarm convenience script we provide to install ROS, robot-specific packages, and configure any drivers and environment variables. The commands to download, configure and run the script are as follows:
$ sudo apt install curl
$ curl 'https://raw.githubusercontent.com/Interbotix/interbotix_ros_manipulators/main/interbotix_ros_xsarms/install/rpi4/xsarm_rpi4_install.sh' > xsarm_rpi4_install.sh
$ chmod +x xsarm_rpi4_install.sh
$ ./xsarm_rpi4_install.sh -d galactic
PS4 Controller Setup
See the Raspberry Pi 4b & SONY PS4 Controller pairing guide.
Networking
Most users usually work with a Raspberry Pi in a headless state via SSH. This can make updating software on the Pi difficult if the Pi does not automatically connect to the Internet when booted. Typically though, it’s bad practice to have any robot connected to the Internet since it makes it easier for hackers to get control of them. One idea might be to just plug an Ethernet cable into the Pi if Internet is needed, but this doesn’t work in all cases since the Pi might be enclosed by a shell or in a hard to reach area. A better approach is to just connect a ‘master’ computer via Ethernet to an Internet connection, and then create a WiFi hotspot on the ‘master’ as well. Then all you need to do is to configure the Pi to act as a Client and automatically connect to the ‘master’ computer’s hotspot when available. This way, you can share the ‘master’ computer’s Internet connection with the Pi when needed.