Skip to main content

Python Integration (rclpy)

rclpy is the official Python client library for ROS 2. It provides a Pythonic interface to all the core ROS 2 concepts, allowing developers to write ROS 2 nodes, publishers, subscribers, services, and actions in Python.

Here is a simple example of a "hello world" publisher in rclpy:

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class HelloWorldPublisher(Node):

def __init__(self):
super().__init__('hello_world_publisher')
self.publisher_ = self.create_publisher(String, 'hello_world', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)

def timer_callback(self):
msg = String()
msg.data = 'Hello, ROS 2!'
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)

def main(args=None):
rclpy.init(args=args)
hello_world_publisher = HelloWorldPublisher()
rclpy.spin(hello_world_publisher)
hello_world_publisher.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()
How can I help you? 💬