Unit 07 · lesson
Build a Publisher Node
Create:
robotnix_pubsub/robotnix_pubsub/talker.py
Use this course adaptation of the official rclpy beginner publisher pattern:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class RobotnixTalker(Node):
def __init__(self):
super().__init__('robotnix_talker')
self.publisher = self.create_publisher(String, 'student_chatter', 10)
self.count = 0
self.timer = self.create_timer(1.0, self.publish_message)
def publish_message(self):
msg = String()
msg.data = f'Robotnix message {self.count}'
self.publisher.publish(msg)
self.get_logger().info(f'Publishing: {msg.data}')
self.count += 1
def main(args=None):
rclpy.init(args=args)
node = RobotnixTalker()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
Trace the ROS responsibilities
self.create_publisher(String, 'student_chatter', 10)
declares a publisher using std_msgs/msg/String on the student_chatter topic.
self.create_timer(1.0, self.publish_message)
schedules the callback repeatedly.
The callback constructs a message, fills msg.data, publishes it, and logs evidence.
Checkpoint
Before running anything, predict:
- node name;
- topic name;
- message type;
- approximate publication interval.