…
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 theNode
class fromrclpy
. In ROS 2, aNode
is the fundamental building block that represents a process capable of communicating with other nodes.from std_msgs.msg import String
: Imports theString
message type from thestd_msgs
package. This message type is used to send and receive strings of text.
…
class SimplePublisher(Node):
class SimplePublisher(Node)
: Defines a classSimplePublisher
that inherits from theNode
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 theSimplePublisher
object is created. -
super().__init__('simple_publisher')
: Calls the constructor of theNode
class and names this nodesimple_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 thepublish_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 newString
message object. -
msg.data = 'Hello, ROS 2!'
: Assigns the string'Hello, ROS 2!'
to thedata
field of the message.
-
self.publisher_.publish(msg)
: Publishes themsg
to thetopic
. 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 themain
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 theSimplePublisher
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 themain()
function is only called when the script is executed directly (i.e., not imported as a module). -
main()
: Calls themain()
function to start the node and begin publishing messages.