Challenge - 5 Problems
ROS 2 Publisher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output of this ROS publisher node?
Consider this ROS 2 Python publisher node code snippet. What will be printed on the console when the node runs?
ROS
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 = f'Hello, world: {self.i}' self.publisher_.publish(msg) self.get_logger().info(f'Publishing: {msg.data}') self.i += 1 def main(args=None): rclpy.init(args=args) minimal_publisher = MinimalPublisher() rclpy.spin(minimal_publisher) minimal_publisher.destroy_node() rclpy.shutdown()
Attempts:
2 left
💡 Hint
Look at the timer callback and how often it triggers.
✗ Incorrect
The timer callback runs every 0.5 seconds, publishing a message and printing the log with an incrementing counter.
📝 Syntax
intermediateWhich option contains a runtime error in creating a ROS 2 publisher node?
Identify the code snippet that will cause a runtime error when defining a ROS 2 publisher node in Python.
Attempts:
2 left
💡 Hint
Check if the topic name is a string literal.
✗ Incorrect
Option B uses 'topic' without quotes, treating it as a variable which is undefined, causing a NameError at runtime when __init__ executes.
❓ state_output
advancedWhat is the value of 'i' after 5 timer callbacks in this ROS 2 publisher node?
Given the following snippet inside a ROS 2 publisher node, what is the value of self.i after the timer callback has run 5 times?
ROS
def __init__(self): super().__init__('minimal_publisher') self.i = 0 def timer_callback(self): self.i += 1
Attempts:
2 left
💡 Hint
The counter starts at zero and increments by one each callback.
✗ Incorrect
Each timer callback adds 1 to self.i starting from 0, so after 5 calls, self.i is 5.
🔧 Debug
advancedWhich option causes a runtime error when running this ROS 2 publisher node?
Identify the code snippet that will cause a runtime error when the node runs.
ROS
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) self.timer = self.create_timer(0.5, self.timer_callback) self.i = 0 self.extra_method() def timer_callback(self): msg = String() msg.data = f'Hello {self.i}' self.publisher_.publish(msg) self.get_logger().info(f'Publishing: {msg.data}') self.i += 1 def extra_method(self): print(self.undefined_variable)
Attempts:
2 left
💡 Hint
Look for undefined variables accessed in methods.
✗ Incorrect
extra_method tries to print an undefined variable, causing AttributeError at runtime when called from __init__.
🧠 Conceptual
expertWhich option best describes the role of QoS profile in ROS 2 publisher creation?
In ROS 2, when creating a publisher with create_publisher(), what does the QoS profile argument control?
Attempts:
2 left
💡 Hint
Think about message delivery guarantees and behavior.
✗ Incorrect
QoS (Quality of Service) profiles define how messages are handled in terms of reliability, durability, and history, affecting communication behavior.
