ROS Cheat Sheet

Some definitions

The Robot Operating System (ROS) is a collection of tools and libraries that helps you build complex and robust robot applications.

There are various distributions of ROS, for example ROS Hydro, ROS Indigo, ROS Kinetic, ROS Melodic. Recently ROS 2 was released.

Nodes: A node is an executable that uses ROS to communicate with other nodes.

Topics: Nodes can publish messages to a topic as well as subscribe to a topic to receive messages.

Publisher: A node that writes information to a topic.

Subscriber: node that writes information from a topic

Messages: ROS data type used when subscribing or publishing to a topic.

ROS cheat sheet


Launch a ROS program

roslaunch <package_name> <launch_file>
roslaunch turtlebot_teleop keyboard_teleop.launch
roslaunch bb_8_teleop keyboard_teleop.launch

roslaunch publisher_example move.launch
roslaunch publisher_example stop.launch

roslaunch service_demo service_launch.launch
rosservice call /service_demo "{}"

roslaunch action_demo action_launch.launch
roslaunch action_demo_client client_launch.launch

Run a node within a package

rosrun <package_name> <node_name>

rosrun turtlesim turtlesim_node
rosrun rviz rviz                   # Run Rviz (debugging)
rosrun rqt_plot rqt_plot
rosrun rqt_graph rqt_graph         # Visualise graph (node connections)

Change directories

roscd                  # go to catkin workspace
roscd <package_name>   # go to package_name

Create a package

cd ..
cd src/
catkin_create_pkg <pkg_name> <pkg_dependencies>
cd ..

$ catkin_create_pkg my_package rospy

Install a package from a Github repo

cd ~/catkin_ws/src

# clone repo
git clone<…>.git

# check for missing dependencies
rosdep update
rosdep check --from-paths . --ignore-src --rosdistro melodic

# install missing dependencies (if needed)
rosdep install --from-paths . --ignore-src --rosdistro melodic -y

# build package
cd ~/catkin_ws/

ROS Packages

# List ROS packages
rospack list
rospack list | grep <package_name>

# Refresh the package list
rospack profile

# List first order dependencies
rospack depends1 <package_name>

# List all dependencies
rospack depends <package_name>

Create a Python programme inside a ROS package

roscd <package_name>
cd src/
vim <>
chmod +x <>
roscd <package_name>
mkdir launch
vim launch/<launch_file.launch>

ROS nodes

# List node currently running
rosnode list

rosrun turtlesim turtlesim_node # in another terminal
rosnode list                    # in another terminal  

# Print info about a node
rosnode info /<node_name>

# Test connectivity to node
rosnode ping

# List nodes running on a particular machine or list machines
rosnode machine

# Kill a running node
rosnode kill

# Purge registration information of unreachable nodes
rosnode cleanup

Compile all packages in catkin directory

. ~/catkin_ws/devel/setup.bash      # Source the setup file
source /opt/ros/melodic/setup.bash  # Source environment setup file
echo $ROS_PACKAGE_PATH              # Print the package path

Compile only one package

catkin_make --only-pkg-with-deps <package_name>

ROS parameters

# List ROS parameters
rosparam list

# Get parameter value
rosparam get <parameter_name>

# Assign a value to a parameter
rosparam set <parameter_name> <value>

# Load parameters from files
rosparam load

# Dum parameters to file
rosparam dump

# Delete parameter
rosparam delete

Start ROS core


List ROS environment variables

export | grep ROS

ROS Topics

# List active topics (with verbose option)
rostopic list
rostopic list -v

# Print the output messages of a topic to the screen in real time
rostopic echo /<topic_name>

# Publish message with specified value to a topic
rostopic pub <topic_name> <message_type> <value>

# Display bandwidth used by topic
rostopic bw

# Display delay of topic from timestamp in header
rostopic delay

# Find topics by type
rostopic find

# Display publishing rate of topic
rostopic hz

# Print topic or field type
rostopic type <topic_name>

ROS messages

# Show message description (and its structure)
rosmsg show <message>
rosmsg info <message>

# This is useful to check the structure of the messages
roscd std_msgs/msg/
roscd geometry_msgs/msg/
roscd nav_msgs/msg/

# List all messages
rosmsg list

# Display message called md5sum
rosmsg md5

# List messages in a package
rosmsg package

# List packages that contain messages
rosmsg packages


# start Gazebo

# run Gazebo server

# run Gazebo client

# kill Gazebo server
killall gzserver

Launch file structure

<!-- My Package launch file -->
<node pkg="<package_name>" type="<>" name="<node_name>" output="screen">

Example of publisher node that write messages to a ROS topic

#! /usr/bin/env python

import rospy          # Import the Python library for ROS
from std_msgs.msg import Int32 # Import the Int32 message from the std_msgs package   

rospy.init_node('topic_publisher') # Initiate a Node named 'topic_publisher'
pub = rospy.Publisher('/counter', Int32, queue_size=1) # Create a Publisher object, that will publish on the /counter topic + messages of type Int32

rate = rospy.Rate(2) # Set a publish rate of 2 Hz
count = Int32() # Create a var of type Int32 = 0 # Initialize 'count' variable

while not rospy.is_shutdown(): # Create a loop that will go until someone stops the program execution
    pub.publish(count) # Publish the message within the 'count' variable += 1 # Increment 'count' variable
    rate.sleep() # Make sure the publish rate maintains at 2 Hz

Example of subscriber node that read messages to a ROS topic

#! /usr/bin/env python

import rospy
from std_msgs.msg import Int32

def callback(msg): # Define a function called 'callback' that receives a parameter named 'msg'
    print # Print the value 'data' inside the 'msg' parameter

rospy.init_node('topic_subscriber') # Initiate a Node called 'topic_subscriber'

# Create a Subscriber object that will listen
# to the /counter topic and will call the
# 'callback' function each time it reads
# something from the topic
sub = rospy.Subscriber('/counter', Int32, callback)