Writing Python Publisher in ROS2
There are three ways to to write a Python publisher in ROS2, namely:
- Old-school approach
- Object-oriented (member-function) approach
- Local function approach
Below is an example of each approach to write a Python node publishing “Hello World”. The code is taken from here: https://github.com/ros2/examples/tree/foxy/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher
Old-school approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
from time import sleep import rclpy from std_msgs.msg import String # We do not recommend this style as ROS 2 provides timers for this purpose, # and it is recommended that all nodes call a variation of spin. # This example is only included for completeness because it is similar to examples in ROS 1. # For periodic publication please see the other examples using timers. def main(args=None): rclpy.init(args=args) node = rclpy.create_node('minimal_publisher') publisher = node.create_publisher(String, 'topic', 10) msg = String() i = 0 while rclpy.ok(): msg.data = 'Hello World: %d' % i i += 1 node.get_logger().info('Publishing: "%s"' % msg.data) publisher.publish(msg) sleep(0.5) # seconds # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() |
Object-oriented (member-function) approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalPublisher(Node): def __init__(self): super().__init__('minimal_publisher') self.publisher_ = self.create_publisher(String, 'topic', 10) timer_period = 0.5 # seconds self.timer = self.create_timer(timer_period, self.timer_callback) self.i = 0 def timer_callback(self): msg = String() msg.data = 'Hello World: %d' % self.i self.publisher_.publish(msg) self.get_logger().info('Publishing: "%s"' % msg.data) self.i += 1 def main(args=None): rclpy.init(args=args) minimal_publisher = MinimalPublisher() rclpy.spin(minimal_publisher) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) minimal_publisher.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() |
Local function approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import rclpy from std_msgs.msg import String def main(args=None): rclpy.init(args=args) node = rclpy.create_node('minimal_publisher') publisher = node.create_publisher(String, 'topic', 10) msg = String() i = 0 def timer_callback(): nonlocal i msg.data = 'Hello World: %d' % i i += 1 node.get_logger().info('Publishing: "%s"' % msg.data) publisher.publish(msg) timer_period = 0.5 # seconds timer = node.create_timer(timer_period, timer_callback) rclpy.spin(node) # Destroy the timer attached to the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) node.destroy_timer(timer) node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() |