ROS cheat sheet

4 min read

ROS_logo

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. ROS 2 was released in 2021.

A Node is process that performs computation and that can communicate with other nodes.

Topics are buses over which nodes exchange messages. Nodes can publish messages to a topic and/or subscribe to a topic to receive messages.

A Message is a data structure (supporting integer, floats, boolean, arrays, etc…) that is used by nodes to communicate with each other.

A Publisher is a Node that writes information TO a Topic.

A Subscriber is a node that writes information FROM a Topic.

A Service is a client/server system that allows Nodes to communicate data to each other. Its an alternative to Topics.

Gazebo is an open-source physics simulator integrated in ROS. It can set up a world and run robotics simulations.

Rviz is a visualization software, that allows you to view Gazebo data (if you are simulating) or real world data (if you are using the real robot).

A Package is a directory where a ROS program is stored.

Catkin is the official build system of ROS.

Cheat sheet

You can download a printable ROS cheat sheet here (Source).

ROS Packages

  • Launch a ROS package
roslaunch <package_name> <launch_file>

For example:

# PACKAGE
roslaunch turtlebot_teleop keyboard_teleop.launch
roslaunch bb_8_teleop keyboard_teleop.launch

# PUBLISHER
roslaunch publisher_example move.launch
roslaunch publisher_example stop.launch

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

# ACTION
roslaunch action_demo action_launch.launch
roslaunch action_demo_client client_launch.launch
  • List ROS packages
rospack list
rospack list | grep <package_name>
  • Refresh the package list
rospack profile
  • List first order dependencies
rospack depends <package_name>
  • List all dependencies
rospack depends <package_name>
  • Create a package
roscd
cd ..
cd src/
catkin_create_pkg <pkg_name> <pkg_dependencies>
cd ..
catkin_make

For example,

catkin_create_pkg my_package rospy
  • Install a package from a Github repository
cd ~/catkin_ws/src

# clone repo
git clone https://github.com/<...>.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/
catkin_make
  • Create a Python programme inside a ROS package
roscd <package_name>
cd src/
vim <python_file_name.py>
chmod +x <python_file_name.py>
roscd <package_name>
mkdir launch
vim launch/<launch_file.launch>
  • Compile all packages in catkin directory
roscd
catkin_make
. ~/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
roscd
catkin_make --only-pkg-with-deps <package_name>

ROS nodes

  • List all the nodes currently running
rosnode list

For example,

roscore
rosrun turtlesim turtlesim_node # in another terminal
rosnode list                    # in another terminal  
  • Run a node within a package
rosrun <package_name> <node_name>

For example:

rosrun turtlesim turtlesim_node
rosrun rviz rviz                   # Run Rviz (debugging)
rosrun rqt_plot rqt_plot
rosrun rqt_graph rqt_graph         # Visualise graph (node connections)
  • 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

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
  • Dump parameters to file
rosparam dump
  • Delete parameter
rosparam delete

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

Gazebo

  • start Gazebo
gazebo
  • run Gazebo server
gzserver
  • run Gazebo client
gzclient
  • kill Gazebo server
killall gzserver

Other commands

  • Start ROS core
roscore
  • List ROS environment variables
export | grep ROS
  • Go to the Catkin workspace
roscd
  • Go to “package_name”
roscd <package_name>

Launch file structure

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

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
count.data = 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
    count.data += 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 msg.data # 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) 

rospy.spin()

Leave a comment