Long title, I know. But this was something that turned out to be surprisingly complex and took lots of troubleshooting steps to get right, so I thought I’d share.
So initially, why would I do this? raspicam-node is already available as binaries for Ubuntu, so why try and compile it for Raspberry Pi OS (Raspbian) Bust
Well for one thing, with Raspberry Pi, we’re still kinda stuck in the land of 32 bit if you want accelerated graphics or at least accelerated video operations because of the GPU hardware on the current Raspberry Pi offerings (RPi 3, 4 etc). I’m not completely over all the detail, but apparently right now we just have to accept this and move on.
So this means that if we were to follow through and install a nice 64 bit version of Ubuntu Server to run ROS on our Raspberry Pi, we wouldn’t be able to benefit from accelerated video bits and pieces, and instead rely on (quite slow) CPU operations, which would mean that any video would be quite slow.
So this means that if I want to use my Raspberry Pi, on a mobile robot, to capture stereo camera input to do mapping with, my only real option to make it fast enough to be useful for stereo vision mapping is to figure out how to build the fantastic raspicam-node by Ubiquity Robotics for 32 bit Raspbian.
Here’s the Raspicam_node source:
https://github.com/UbiquityRobotics/raspicam_node
I also got some great help to get started with ROS on Raspbian from :
https://varhowto.com/install-ros-noetic-raspberry-pi-4/
This guide is still a work in progress, and I will in the near future clean it up and edit it to improve it, so take this as a quick dump of info for now to help get you started before I forget.
Enjoy!
Download and image the Raspberry Pi OS 32 bit “buster” lite image onto an SD card and boot up your Raspberry Pi 4 (which is what I used here) with a screen attached (and keyboard if you’re just going to use it directly rather than SSH in). Any command starting with “sudo” may require your password, which the standard for the default “pi” user is “raspberry”.
Now we need to add the official ROS software sources to download from using the following command:
sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu buster main” > /etc/apt/sources.list.d/ros-noetic.list’
Next step is to add the key for this server so that it will be accepted as a usable source with this command:
sudo apt-key adv –keyserver ‘hkp://keyserver.ubuntu.com:80’ –recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
Now update the available packages so we can see the new sources:
sudo apt update
Make sure the whole system (including the kernel) is fully update to date:
sudo apt full-upgrade
Reboot the system into the updates:
sudo reboot
Install the required packages from the ROS sources:
sudo apt-get install -y python3-rosdep python3-rosinstall-generator python-wstool python3-rosinstall build-essential cmake
Initialise the ROS dependency tool (adds hidden files to your home directory):
sudo rosdep init
Update the ROS dependency tool:
rosdep update
Create a “catkin” workspace (catkin is the official build tool for ROS, so directories to hold the sources, build requirements and binaries built are called catkin workspaces) by simply creating a directory called “ros_catkin_ws” in your home directory:
mkdir ~/ros_catkin_ws
Change to this directory:
cd ~/ros_catkin_ws
Use the rosinstall_generator tool to get ready to flesh out the catkin workspace we created above. This basically sets up a special file that will be used to create all of the requirements needed to make a functional catkin workspace for ROS “Noetic” (wet here means released packages):
rosinstall_generator ros_comm –rosdistro noetic –deps –wet-only –tar > noetic-ros_comm-wet.rosinstall
This will initialise the sources for “Noetic” to be built in our catkin workspace:
wstool init src noetic-ros_comm-wet.rosinstall
ROS depencies will be downloaded and put into the ./src directory based on our workspace (required libraries etc) so we can build ROS:
rosdep install -y –from-paths src –ignore-src –rosdistro noetic -r –os=debian:buster
Compiling things takes lots of ram, of which the Raspberry Pi has relatively little in today’s standards, so in order to not ever accidentally bump over the limit it’s a wise idea to modify the amount of swap file we have available to soak up any overruns.
First turn off the swap file:
sudo dphys-swapfile swapoff
Edit the swapfile configuration:
sudoedit /etc/dphys-swapfile
Edit the line in the file that says “CONF_SWAPFILE” to equal 1024 (1GB):
CONF_SWAPSIZE=1024
Save and exit the nano file editor by pressing CTRL-O (O for ostrich) and hitting enter, then press CTRL-X
Setup the required new swap file:
sudo dphys-swapfile setup
Turn swapping back on with the new settings and file:
sudo dphys-swapfile swapon
Now let’s compile ROS Noetic (here I’ve used option -j3 which means use 3 simultaneous processes for compiling to speed things up, this uses more RAM and works the processor harder, but for me works fine for Raspberry Pi 4 with 2GB of ram, if this fails, try -j1):
sudo src/catkin/bin/catkin_make_isolated –install -DCMAKE_BUILD_TYPE=Release –install-space /opt/ros/noetic -j3 -DPYTHON_EXECUTABLE=/usr/bin/python3
The main build and installation of ROS Noetic is now finished. You’ll find your new compiled binaries are in /opt/ros/noetic/
Each time you use ROS you’ll need to source some bash terminal bits with the following command:
source /opt/ros/noetic/setup.bash
If this works you can put this at the end of your .bashrc file which will make bash load it every time you log in. Simply type:
nano ~/.bashrc
And you’ll be using the nano editor like above to see the contents. Scroll to the very bottom, and press enter for a new line and put the above “source” line into this file. Press ctrl-o and press enter to save. Then press ctrl-x to exit.
Try running ROS core to see if it runs to test your installation and bash source with:
roscore
Now we have a fully running ROS installation on Rapsberry Pi and have tested our ability to setup and compile a catkin workspace. So we can move ahead and use these tools to compile the Raspicam_node tool to allow ROS to access the onboard Rasperry Pi camera.
Why do we want this? Well because apart from the amazingly fast interface the special port on the Raspberry Pi has with a wide range of cameras compatible with it, we can also use camera boards like those available from Arducam that put two cameras into a single camera source side by side to source a stereo image. And we know that from a stereo camera source we can then pull 3d image data for things like SLAM mapping. Very useful for mobile computers like Raspberry Pi!
Let’s add the repository for rosdep to understand the dependencies that are laid out in the raspicam source to compile (this is mainly regarding libraspberrypi-dev stuff, without this step, rosdep won’t know where to find the required libraries to build with) – we’ll use the nano editor to create a file:
sudo nano /etc/ros/rosdep/sources.list.d/30-ubiquity.list
Now inside the nano editor we will add this line:
yaml https://raw.githubusercontent.com/UbiquityRobotics/rosdep/master/raspberry-pi.yaml
Save the file in nano, then exit. Now we can run rosdep update to use this new source:
rosdep update
Now that rosdep has knowledge of where to find the stuff needed to build raspicam_node, let’s go and setup a new catkin workspace (perhaps not needed, but let’s do a fresh one just in case) – the “p” here creates the parent directory as well as we’re creating the “catkin_ws” directory in the user home, then the “src” directory underneath it:
mkdir -p ~/catkin_ws/src
Change into this new src subdirectory:
cd ~/catkin_ws/src
Let’s get the raspicam_node source code directly from their Github page:
git clone https://github.com/UbiquityRobotics/raspicam_node.git
Let’s move out of the “src” directory into the top of the new catkin workspace we created:
cd ~/catkin_ws
Let’s have ROS initialise for use the src directory and everything in it to use:
wstool init src
Use rosinstall_generator to set up what is needed in 4 different ways:
Step 1:
rosinstall_generator compressed_image_transport –rosdistro noetic –deps –wet-only –tar > compressed_image_transport-wet.rosinstall
Step 2:
rosinstall_generator camera_info_manager –rosdistro noetic –deps –wet-only –tar > camera_info_manager-wet.rosinstall
Step 3:
rosinstall_generator dynamic_reconfigure –rosdistro noetic –deps –wet-only –tar > dynamic_reconfigure-wet.rosinstall
Step 4:
rosinstall_generator diagnostics –rosdistro noetic –deps –wet-only –tar > diagnostics-wet.rosinstall
Merge these into the “src” directory with wstool in 5 steps:
Step 1:
wstool merge -t src compressed_image_transport-wet.rosinstall
Step 2:
wstool merge -t src camera_info_manager-wet.rosinstall
Step 3:
wstool merge -t src dynamic_reconfigure-wet.rosinstall
Step 4:
wstool merge -t src diagnostics-wet.rosinstall
Step 5:
wstool update -t src
Let’s make rosdep find all the dependencies required to now build all of this:
rosdep install –from-paths src –ignore-src –rosdistro noetic -y –os=debian:buster
Finally, we can now build raspicam_node:
sudo src/catkin/bin/catkin_make_isolated –install -DCMAKE_BUILD_TYPE=Release –install-space /opt/ros/noetic -j3 -DPYTHON_EXECUTABLE=/usr/bin/python3
raspicam_node is now built, and you will find the binaries in /opt/ros/noetic with the rest of ROS we built earlier. Before we can use it though, we must enable the camera port using:
sudo raspi_config
Look for interfaces, and camera – it will ask if you wish to enable it. If it doesn’t automatically reboot, reboot the Raspberry Pi yourself:
sudo reboot
Make sure after reboot that the default pi user (or whatever user you’re using) is added to the video group to access the camera:
sudo adduser pi video
With my Arducam module (probably not necessary for other modules), I had to make sure that the I2C module was added to the kernel options by editing the boot config:
sudo nano /boot/config.txt
Put in:
dtparam=i2c_vc=on
Save (ctrl-o, enter, ctrl-x) and reboot again:
sudo reboot
Test that the camera works directly using the built in Raspberry Pi camera tools:
raspistill -o temp.jpg
You should see an image from the camera on screen for a short moment. If so, success! Time to use the module in ROS!
Let’s source the bash setup file (this might need work below, we should probably only need to source what is in the /opt/ros/noetic directory):
source ~/catkin_ws/devel_isolated/setup.bash
Run roscore in the background:
roscore &
Launch the raspicam_node with the built in config for a v2 camera at 640×480 5fps (there are several build in modes, simply type roslaunch raspicam_node and press TAB a couple of times to see the options). We are again pushing this process to run in the background by putting the “&” symbol at the end:
roslaunch raspicam_node camera_module_v2_640x480_5fps_autocapture.launch &
Now let’s see how fast the update speed is in one of the Raspicam_node topics:
rostopic hz /raspicam_node/image/compressed
If you tried to run the above and got an error about calibration, do the following:
cp -r ~/catkin_ws/src/raspicam_node/camera_info ~/.ros
If you got no errors, and you’re seeing an update of how fast the updates are happening per second, then you’re up and running!
To stop the running processes above first press CTRL-C to kill off the rostopic command. This should now return you to a commandline. Now use the process management tools to bring those other 2 commands to the front to kill by typing:
fg
you’ll now see you can kill off the second process with CTRL-c, and then repeat to kill off the initial roscore.
Sucess! You can now use the Raspberry Pi camera for ROS in a nice fast way with a neat node.
This is not the end though, as for my Arducam, the image comes in as a side by side stereo image in a single image. It needs to be sliced in half in order for us to do stereo image processing. So I’m looking at using another node that does this job (depending on how fast it runs) or otherwise I’ll see if it’s possible to add the feature to raspicam_node itself so it’ll be a one-stop-shop for fast and cheap stereo image sourcing for 3D outcomes.
Stay tuned..