Running ROS2 Foxy on Raspberry Pi Zero 2W

RoboFoundry
7 min readNov 14, 2021

Recently, I watched the interview that MAKE magazine did with Eben Upton when they announced the Raspberry Pi Zero 2W and was excited to hear about the capabilities of the tiny package which costs about $15 and can deliver performance equivalent to a Raspberry Pi 3. So I went ahead and ordered one from Pi Hut, they are just amazing place to shop for Raspberry Pi stuff, I was lucky enough to get my hands on one and here is my experience on how I set it up to run ROS2.

Raspberry Pi Zero 2W running ROS2 with Pi Hut case

Before we start, some basics of the landscape and some pain points I had to go through to figure out what is the right setup.

  • Raspberry Pi Zero 2W is a 64-bit computer [SBC] and it can run both 64-bit or 32-bit versions of the operating system
  • After a lot of trial error with various combinations based on Canonical articles [reference links at the end], I figured out that best combination to run ROS2 on Pi Zero is to install Ubuntu 20.04 Focal 64-bit. This is the most straightforward setup to use existing ROS2 Foxy distributed binaries without having to compile it from source. Believe me, I tried with different combinations of Focal/Impish, 32/64-bit etc and it was not that easy to get it running as it is on 64-bit Focal 20.04.
  • Ubuntu 21.10 Impish has out of box support for Pi Zero 2, but there is no ready to use binary packages available for 32-bit ROS2 Foxy + Ubuntu 21.10 Impish combination, so if you choose to go that route, ROS2 will have to be compiled/built from source instead of using ready to go binary packages. I tried this route as well and it was riddled with many compile errors and hours of trying to figure out what are the differences and dependencies, in the end not worth it.

The whole process can be divided into three parts:

  1. Installing and getting Ubuntu server running on Pi Zero
  2. Post installation updates
  3. Installing ROS2 Foxy from binaries

Part 1 — Main steps to follow to get Ubuntu Server running on Pi Zero in headless mode

  1. First get an SD Card [I used 16 Gb one] and download and write the correct image of Ubuntu 20.04 Focal using RP Imager. Detailed steps here — https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#2-prepare-the-sd-card
  2. Once your SD Card is ready, go ahead and prepare network-config file to allow you to run it in headless mode and connect to your Wifi [see example file in gist below]. Once your file is prepared, simply copy it to system-boot folder when your SD Card is in the SD Card reader connected to your main computer. If you need more information see this article — https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#3-wifi-or-ethernet.

network-config file example:

Here is the gist file you can copy paste in correct indentation/format. You will need to replace two values in the file before you can use it — wifi ssid and your wifi password.

On thing to keep in mind from what I have read, Pi Zero 2W will connect on the 2.4 GHz wifi network so if you have two separate names, use the one for 2.4 GHz.

Tip: I typically just set the RPi boards or other IoT devices as wifi with dhcp enabled and first time when it boots up, you can monitor the list of connected devices in your Wifi router admin interface and once you find it, reserve the IP address for it and rename it to give it some sensible name so that you can easily find it and ssh to it with known IP address in future.

3. Now comes the tricky part, we need to create/change two more files for this whole setup to work.

  • 3a. Change — config.txt file in the system-boot drive when you are connected to your SD Card Reader

For config.txt file you can use github gist here [courtesy Canonical articles referenced at the end]. You can simply create a file somewhere else and copy it to system-boot folder on SD Card. There will be an existing file with the same name, so if you want to back it up first that would be good thing to do.

  • 3b. Create a new file named —bcm2710-rpi-zero-2.dtb

In order to do this open the SD card you prepared using RP Imager in file browser after you insert it in your SD Card reader.

Locate drive names system-boot and locate a file named bcm2710-rpi-3-b.dtb

Make a copy of this file in same folder and rename the file to — bcm2710-rpi-zero-2.dtb

At this point you are pretty much done with basic setup to get Ubuntu Server up and running on your Pi Zero. Just eject the SD card properly [don’t just pull it out] and then load it into your Pi Zero SD Card slot and power up your Pi Zero.

Part 2 — Updating SD Card to get ready to build ROS2 Foxy

  1. After your Pi Zero boots successfully, it will show you a blinking green light on the only LED that is present on the board. There is no particular pattern to spot in the blinking, it may come on and off several times.
  2. At this point if you have setup the steps mentioned in tip above to reserve a static IP address in your router, you should be able to launch ssh with command.
ssh ubuntu@your_pi_zero_ip_address

Be patient and continue to try ssh command as it can take several minutes for it to boot, especially first time. At one point I thought it was not working and I was about to give up and the ssh window came up with password prompt which was a big turning point.

3. ubuntu [lower case] is the default user name and when you login first time, the default password is also ubuntu [lower case]. When you login, you will be asked to immediately change the password and it will reboot. So you will need to ssh back into it and this time use the new password you just set.

4. After logging back in, based on my experience and reading from others, it is better to complete two following steps before you run any updates:

  • 4a — Create a swapfile — since the memory on Pi Zero is limited, it can be pretty challenging for the little board to do all the heavy lifting without swap space so make sure to do this. Steps on how to do it later.
  • 4b — Switch initramfs to use lz4 compression (which results in larger output on the boot partition, but is much faster and less memory intensive, this coming from Canonical guys)
  • After these two steps are done, you can run the standard:
sudo apt update
sudo apt upgrade

Creating a swap file:

## Please run all commands below without copying $ in front, that's just there to indicate it is a command line and description is in comments starting with ##
## You can check if you have any swap enabled by executing
$ free -m## Create a file:$ sudo fallocate -l 1G /var/swapfile## initialize with zeros$ sudo dd if=/dev/zero of=/var/swapfile bs=1024 count=1048576## Modify permissions for swapfile$ sudo chmod 0600 /var/swapfile## Create a swap file system on it$ sudo mkswap /var/swapfile## Enable swap$ sudo swapon /var/swapfile## Run following commands to ensure that you indeed have some swap space
$ free -m
$ sudo swapon --show
## make it permanent through restarts by adding an entry to /etc/fstab file$ sudo nano /etc/fstab## append following line at the end of /etc/fstab file in nano editor/var/swapfile swap swap defaults 0 0## that's it, reboot and make sure to run free -m and sudo swapon --show to make sure the swap file is still maintained after reboot, if it has not, check the entry in /etc/fstab file to make sure it points to exact location of your swap file e.g. /var/swapfile$ sudo reboot

Switch initramfs to use lz4 compression :

## run following commands and reboot$ sudo apt install lz4$ sudo sed -i -e 's/COMPRESS=zstd/COMPRESS=lz4/' /etc/initramfs-tools/initramfs.conf$ sudo update-initramfs -u

Part 3 — Installing ROS2 Foxy

First make sure you have run :

sudo apt update
sudo apt upgrade

The instructions for doing ROS2 Foxy install are very well documented on ROS documentation page here — https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Debians.html

I simply followed one step at a time and was able to get to a point where I’m able to run two ssh terminals, one running talker and one running listener from ROS2 examples.

I chose the bare bones version of ros2 foxy, not the full desktop version to keep things light. After the install was complete, I also followed steps in Install Development tools and ROS Tools section to install dev tools recommended in the building from source documentation to make sure I was able to access things like colcon and other tools on Pi Zero.

## ros2 foxy bare bones install command
$ sudo apt install ros-foxy-ros-base

Here is a screenshot from Pi Zero ssh session showing the minimal publisher running and publishing messages:

Summary

In summary, I’m looking forward to using this little board to build a robot in future, hopefully it is good enough to run basic ROS2 nodes to read IMU/Encoder, LIDAR sensors and broadcast those to main computer to run the robot.

Hope this helps you save some time not only for doing install on Pi Zero but other environments as well.

Useful Articles:

--

--