A Step-by-Step Guide to Setting Up and Using the Robot Operating System (ROS) on Raspberry Pi

This guide will teach you how to set up and use ROS (Robot Operating System) on a Raspberry Pi for robotics development. From installation to creating your own ROS packages, this article covers everyt …


Updated October 16, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This guide will teach you how to set up and use ROS (Robot Operating System) on a Raspberry Pi for robotics development. From installation to creating your own ROS packages, this article covers everything you need to know to get started with ROS on Raspberry Pi.

ROS is an open-source framework that provides libraries and tools for building robotic applications. It simplifies the process of developing complex robotics software by providing a common interface for different hardware components and algorithms. This makes it easier to build, test, and deploy robotics software across various platforms. In this guide, we will show you how to set up ROS on your Raspberry Pi and create a simple ROS package.

Prerequisites

Before we begin, make sure you have the following:

  • A Raspberry Pi with Raspbian installed (preferably Raspbian Stretch Lite)
  • An internet connection to download required packages and software
  • Basic knowledge of Linux and Python programming

Step 1: Install ROS on Raspberry Pi

ROS can be installed on various platforms, including Windows, macOS, and Linux. In this guide, we will focus on installing it on Raspbian Stretch Lite, which is a lightweight version of the Raspbian operating system optimized for robots.

  1. First, update your package lists:
sudo apt-get update
  1. Install some necessary dependencies:
sudo apt-get install -y python-rosdep python-rosinstall ros-kinetic-ros-base
  1. Initialize rosdep:
sudo rosdep init
  1. Update the rosdep database:
rosdep update
  1. Set up your environment by adding the following line to your .bashrc file:
source /opt/ros/kinetic/setup.bash
  1. Source the new .bashrc:
source ~/.bashrc

Now, you have successfully installed ROS on your Raspberry Pi!

Step 2: Create a ROS Workspace

A workspace is where you will store all your ROS packages and catkin configuration. Follow these steps to create a new workspace:

  1. Create a new directory for the workspace:
mkdir -p ~/catkin_ws/src
  1. Change into the src folder:
cd ~/catkin_ws/src
  1. Clone an example package into your workspace:
git clone https://github.com/ros/ros_tutorials.git
  1. Go back to the root of your workspace and build the package:
cd ~/catkin_ws
catkin_make
  1. Source the setup script for your new workspace:
source devel/setup.bash

Now you have a functional ROS workspace on your Raspberry Pi!

Step 3: Create Your Own ROS Package

Creating your own ROS package is an essential step in robotics development. Follow these steps to create your first package:

  1. Change into the src directory of your workspace:
cd ~/catkin_ws/src
  1. Create a new package using catkin_create_pkg:
catkin_create_pkg my_ros_package rospy std_msgs

This will create a new package named my_ros_package with a dependency on the rospy and std_msgs packages. 3. Open the CMakeLists.txt file in your new package:

nano my_ros_package/CMakeLists.txt
  1. Add the following lines at the bottom of the file to build your package:
add_executable(my_node src/my_node.py)
target_link_libraries(my_node ${catkin_LIBRARIES})
  1. Create a new Python script named my_node.py in the src folder of your package:
nano my_ros_package/src/my_node.py
  1. Add the following code to your my_node.py file:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
  1. Build your new package:
cd ~/catkin_ws
catkin_make
  1. Source the setup script for your workspace again:
source devel/setup.bash
  1. Run your node:
rosrun my_ros_package my_node

You should see “hello world” messages printed in your terminal.

Congratulations! You have successfully created a ROS package on your Raspberry Pi and tested it using the rospy Python library. Now you can start building more complex robotics applications with ROS on your Raspberry Pi.