Skip to content

1.3 ROS Cheat Sheet

Majd Khalife edited this page Oct 12, 2024 · 2 revisions

#DO BASIC CHEATSHEET FOR ROS2

ROS in the terminal

ROS Core:

(roslaunch does this for you automatically but rosrun doesn't)

  1. Start ROS Core (Master):

    $ roscore
  2. Check ROS Master Status:

    $ rostopic list

Nodes and Topics:

  1. List Nodes:

    $ rosnode list
  2. Node Information:

    $ rosnode info <node_name>
  3. List Topics:

    $ rostopic list
  4. Topic Information:

    $ rostopic info <topic_name>
  5. Publish to a Topic:

    $ rostopic pub <topic_name> <message_type> <data>
  6. Echo a Topic:

    $ rostopic echo <topic_name>

Launch Files:

  1. Run a Launch File:

    $ roslaunch <package_name> <launch_file_name>
  2. List Active Launch Files:

    $ roslaunch list

Running Nodes:

  1. Run a Node:
    $ rosrun <package_name> <node_name>

Bags:

  1. Record Data to a Bag File:

    $ rosbag record -a
  2. Play Back a Bag File:

    $ rosbag play <bag_file_name>
  3. Inspect a Bag File:

    $ rosbag info <bag_file_name>

ROS in Python

ROS Node Initialization

import rospy
rospy.init_node('node_name')
  • rospy.init_node('node_name'): Initializes a ROS node with the specified name. A ROS node is a single process that performs a specific task in a ROS-based system. You must call init_node before using other ROS functionalities.

Publishers

from std_msgs.msg import String
pub = rospy.Publisher('topic_name', String, queue_size=10)

msg = String()
msg.data = 'Hello, ROS!'
pub.publish(msg)
  • rospy.Publisher('topic_name', String, queue_size=10): Creates a publisher object that can publish messages of type String to the specified topic. The queue_size parameter determines how many messages will be buffered before they are discarded.

Subscribers

from std_msgs.msg import String

def callback(data):
    rospy.loginfo('Received: %s', data.data)

rospy.Subscriber('topic_name', String, callback)
rospy.spin()
  • rospy.Subscriber('topic_name', String, callback): Creates a subscriber that listens to messages of type String on the specified topic and calls the callback function when messages are received.
  • rospy.spin(): This call blocks forever. It keeps the ROS node running and continues to process callbacks. It allows your program to remain responsive to incoming messages even after reaching the end of the Python file.

Timers

def timer_callback(event):
    # Your code here
    pass

rospy.Timer(rospy.Duration(0.1), timer_callback)
  • rospy.Timer(rospy.Duration(0.1), timer_callback): Creates a timer that calls the timer_callback function at a specified interval (0.1 seconds in this example). You can put your code inside timer_callback to perform periodic tasks.

Parameters

param_value = rospy.get_param('~param_name', default_value)
rospy.set_param('~param_name', param_value)
  • rospy.get_param('~param_name', default_value): Retrieves the value of a parameter named param_name with an optional default value if the parameter is not set.
  • rospy.set_param('~param_name', param_value): Sets the value of a parameter named param_name to the value of param_value.

Parameters can be defined in various ways:

  • In a launch file using <param> tags.
  • Programmatically using rospy.set_param.
  • From the command line when launching a node.

ROS Action Servers:

Creating an Action Server:

import actionlib
from your_package.msg import YourAction, YourActionFeedback, YourActionResult

def execute(goal):
    # Your action server logic here
    result = YourActionResult()
    server.set_succeeded(result)

server = actionlib.SimpleActionServer(
    'action_name',
    YourAction,
    execute,
    auto_start=False
)
server.start()
  • actionlib.SimpleActionServer(...): Creates an Action Server.
  • execute(goal): The function to handle action goals and perform actions.
  • server.set_succeeded(result): Marks the action as succeeded with an optional result.

Action Server Clients

Creating an Action Server client:

import actionlib
from your_package.msg import YourAction, YourActionGoal

client = actionlib.SimpleActionClient('action_name', YourAction)
client.wait_for_server()

goal = YourActionGoal()
# Set goal parameters
client.send_goal(goal)

client.wait_for_result()
result = client.get_result()
  • actionlib.SimpleActionClient(...): Creates an action client for the specified action type and action server name.
  • client.send_goal(goal): Sends a goal to the action server.
  • client.wait_for_result(): Waits for the action server to complete the goal.
  • client.get_result(): Retrieves the result of the action.

ROS Services:

Creating a Service:

import rospy
from your_package.srv import YourService, YourServiceResponse

def handle_request(req):
    # Your service logic here
    response = YourServiceResponse()
    response.result = 'Success'
    return response

rospy.Service('service_name', YourService, handle_request)
  • rospy.Service(...): Creates a service server.
  • handle_request(req): The function to handle service requests and provide responses.

Service Clients

Creating a Service client:

import rospy
from your_package.srv import YourService, YourServiceRequest

rospy.wait_for_service('service_name')
service_proxy = rospy.ServiceProxy('service_name', YourService)

request = YourServiceRequest()
# Set request parameters
response = service_proxy(request)
  • rospy.wait_for_service('service_name'): Waits for the specified service to become available.
  • rospy.ServiceProxy(...): Creates a service client proxy for the specified service type and service name.
  • service_proxy(request): Sends a request to the service and receives a response.

ROS Node Cleanup with on_shutdown:

import rospy

def cleanup():
    # Your cleanup logic here

rospy.on_shutdown(cleanup)
  • rospy.on_shutdown(cleanup): Registers a function (cleanup) to be called when the ROS node is shutting down. This is useful for performing cleanup tasks before the node exits.