Complete the code to initialize a ROS node named 'talker'.
rclpy.init(args=[1])The rclpy.init() function initializes the ROS client library. Passing None means no command-line arguments are forwarded.
Complete the code to create a publisher for the 'chatter' topic with message type String.
publisher = node.create_publisher(std_msgs.msg.[1], 'chatter', 10)
The publisher must use the String message type to send text messages on the 'chatter' topic.
Fix the error in the code to publish a message with data 'Hello World'.
msg = std_msgs.msg.String()
msg.data = [1]
publisher.publish(msg)The message data must be a string literal enclosed in quotes. Using single quotes 'Hello World' is correct.
Fill both blanks to create a ROS node and spin it to keep it alive.
rclpy.init(args=[1]) node = rclpy.create_node('[2]') rclpy.spin(node)
Initialize ROS with None for no args, then create a node named talker. Spinning keeps the node running.
Fill all three blanks to create a publisher node that publishes 'Hello ROS' every second.
import rclpy from rclpy.node import Node from std_msgs.msg import String class TalkerNode(Node): def __init__(self): super().__init__('talker') self.publisher_ = self.create_publisher(String, 'chatter', 10) self.timer = self.create_timer(1.0, self.timer_callback) def timer_callback(self): msg = String() msg.data = [1] self.publisher_.publish(msg) self.get_logger().info('Publishing: "%s"' % [2]) def main(args=None): rclpy.init(args=[3]) node = TalkerNode() rclpy.spin(node) node.destroy_node() rclpy.shutdown()
The message data is the string 'Hello ROS'. The logger prints msg.data. The init function uses None for args.
