Cheat sheet
The main command line tool is ros2
, you can use ros2 -h
to list all the available commands and ros2 command -h
to get the documentation of any command.
Package
To create new packages ([--dependencies list of dependencies]
is optional), for Python:
ros2 pkg create --build-type ament_python --node-name node_name package_name [--dependencies list of dependencies]
For C++:
ros2 pkg create --build-type ament_cmake --node-name node_name package_name [--dependencies list of dependencies]
Topics
The main command for topics is ros2 topic
, it can be used to list topics, get information about topics (such as their types)... An other important command is ros2 interfaces
it can be used to get information about a topic type.
The main tutorials are Python and C++.
In Python, to create a publisher to a topic and publish to it:
from package_name.msg import TypeOfTopic
pub = ros_node.create_publisher(TypeOfTopic, 'name_of_topic', 10)
msg = TypeOfTopic()
pub.publish(msg)
And to subscribe to a topic:
from package_name.msg import TypeOfTopic
sub = ros_node.create_subscription(TypeOfTopic, 'name_of_topic', callback, 10)
def callback(msg):
ros_node.get_logger().info('I heard: "%s"' % msg)
Services
The main command for services is ros2 service
, it can be used to list services, get information about services (such as their types)... An other important command is ros2 interfaces
it can be used to get information about a service type.
The main tutorials are Python and C++.
In Python, to create a client to a service and call it:
from package_name.srv import TypeOfService
client = ros_node.create_client(TypeOfService, 'name_of_service')
while not client.wait_for_service(timeout_sec=1.0):
ros_node.get_logger().info('service not available, waiting again...')
req = TypeOfServic.Request()
future = client.call_async(req)
rclpy.spin_until_future_complete(ros_node, future) or executor.spin_until_future_complete(future)
ros_node.get_logger().info('Got '%s' % (future.result()))
And to create a server:
from package_name.srv import TypeOfService
server = ros_node.create_service(TypeOfService, 'name_of_service', callback)
def callback(request, response):
ros_node.get_logger().info('I heard: "%s"' % request)
return response
Actions
The main command for actions is ros2 action
, it can be used to list actions, get information about actions (such as their types)... An other important command is ros2 interfaces
it can be used to get information about an action type.