-
Notifications
You must be signed in to change notification settings - Fork 3
1.3 ROS Cheat Sheet
Majd Khalife edited this page Oct 12, 2024
·
2 revisions
#DO BASIC CHEATSHEET FOR ROS2
(roslaunch does this for you automatically but rosrun doesn't)
-
Start ROS Core (Master):
$ roscore
-
Check ROS Master Status:
$ rostopic list
-
List Nodes:
$ rosnode list
-
Node Information:
$ rosnode info <node_name>
-
List Topics:
$ rostopic list
-
Topic Information:
$ rostopic info <topic_name>
-
Publish to a Topic:
$ rostopic pub <topic_name> <message_type> <data>
-
Echo a Topic:
$ rostopic echo <topic_name>
-
Run a Launch File:
$ roslaunch <package_name> <launch_file_name>
-
List Active Launch Files:
$ roslaunch list
-
Run a Node:
$ rosrun <package_name> <node_name>
-
Record Data to a Bag File:
$ rosbag record -a
-
Play Back a Bag File:
$ rosbag play <bag_file_name>
-
Inspect a Bag File:
$ rosbag info <bag_file_name>
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 callinit_node
before using other ROS functionalities.
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 typeString
to the specified topic. Thequeue_size
parameter determines how many messages will be buffered before they are discarded.
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 typeString
on the specified topic and calls thecallback
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.
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 thetimer_callback
function at a specified interval (0.1 seconds in this example). You can put your code insidetimer_callback
to perform periodic tasks.
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 namedparam_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 namedparam_name
to the value ofparam_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.
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.
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.
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.
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.
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.