import rclpy
from rclpy.node import Node
from std_msgs.msg import String
  • import rclpy: This imports the ROS 2 client library for Python, which provides the core functionality for ROS 2 nodes.
  • from rclpy.node import Node: Imports the Node class from rclpy. In ROS 2, a Node is the fundamental building block that represents a process capable of communicating with other nodes.
  • from std_msgs.msg import String: Imports the String message type from the std_msgs package. This message type is used to send and receive strings of text.



class SimplePublisher(Node):
  • class SimplePublisher(Node): Defines a class SimplePublisher that inherits from the Node class, meaning it will have all the functionalities of a ROS 2 node.

def __init__(self):
    super().__init__('simple_publisher')
    self.publisher_ = self.create_publisher(String, 'topic', 10)
    self.timer = self.create_timer(0.5, self.publish_message)
  • def __init__(self):: This is the constructor method that gets called when the SimplePublisher object is created.

  • super().__init__('simple_publisher'): Calls the constructor of the Node class and names this node simple_publisher. The node name is important for ROS 2 to identify and manage it in the larger ROS 2 system.

  • self.publisher_ = self.create_publisher(String, 'topic', 10): This line creates a publisher.

    • String: Specifies the type of message this publisher will send, which is a simple text string (std_msgs.msg.String).

    • 'topic': The topic name to which this node will publish messages. Other nodes can subscribe to this topic.
    • 10: The size of the message queue, which determines how many messages can be stored if the subscriber isn’t receiving them fast enough.
  • self.timer = self.create_timer(0.5, self.publish_message): This creates a timer that calls the publish_message method every 0.5 seconds (500 milliseconds). Timers are a way to trigger callbacks at a fixed interval.


def publish_message(self):
    msg = String()
    msg.data = 'Hello, ROS 2!'
    self.publisher_.publish(msg)
    self.get_logger().info(f'Publishing: {msg.data}')
  • def publish_message(self):: This is the callback function that will be executed by the timer every 0.5 seconds to publish a message.

  • msg = String(): Creates a new String message object.

  • msg.data = 'Hello, ROS 2!': Assigns the string 'Hello, ROS 2!' to the data field of the message.


  • self.publisher_.publish(msg): Publishes the msg to the topic. Any subscribers to this topic will receive this message.

  • self.get_logger().info(f'Publishing: {msg.data}'): Logs the message content to the terminal so you can see what is being published.


def main(args=None):
    rclpy.init(args=args)
    node = SimplePublisher()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()
  • def main(args=None):: Defines the main function, which is the entry point of the program.

  • rclpy.init(args=args): Initializes the ROS 2 Python client library. This must be called before any ROS 2 nodes are created.

  • node = SimplePublisher(): Creates an instance of the SimplePublisher class, initializing the node.


  • rclpy.spin(node): Keeps the node running and allows it to handle incoming ROS 2 events (like timers and subscriptions). This function doesn’t return until the node is shut down.

  • node.destroy_node(): Destroys the node once it’s no longer needed (cleanup step).

  • rclpy.shutdown(): Shuts down the ROS 2 system, which includes cleaning up resources and stopping the ROS 2 client library.


if __name__ == '__main__':
    main()
  • if __name__ == '__main__':: Ensures that the main() function is only called when the script is executed directly (i.e., not imported as a module).

  • main(): Calls the main() function to start the node and begin publishing messages.


tech_leef