Unit 07 · lesson
Build a Subscriber Node
Create:
robotnix_pubsub/robotnix_pubsub/listener.py
with:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class RobotnixListener(Node):
def __init__(self):
super().__init__('robotnix_listener')
self.subscription = self.create_subscription(
String,
'student_chatter',
self.receive_message,
10,
)
def receive_message(self, msg):
self.get_logger().info(f'I heard: {msg.data}')
def main(args=None):
rclpy.init(args=args)
node = RobotnixListener()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
Compare to the publisher
Both nodes:
- inherit from
Node; - initialize through
rclpy; - use
std_msgs.msg.String; - spin to process ROS work.
The key difference is the endpoint:
talker creates publisher
listener creates subscription
They must agree on topic name and message type.
Callback behavior
The subscriber does not need a timer for incoming messages. Its callback runs when matching messages arrive.
Checkpoint
Change only the listener's topic name on paper to wrong_topic and predict what the running graph would show. Save that failure idea for the diagnosis lesson.