ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Replace rclpy.shutdown()
with self.destroy_node()
. Please see the code snippet:
# Copyright 2016 Open Source Robotics Foundation, Inc.
# license removed for brevity
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 = 'Hello World: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1
if self.i==10: # I could put other non hard-coded tests here
msg.data = 'Done'
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.destroy_node() # <-- Here is where I want to the node to quit
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
rclpy.shutdown()
if __name__ == '__main__':
main()
Below is the info it prints at terminal:
$ ros2 run examples_rclpy_minimal_publisher publisher_member_function
[INFO] [1663241782.469494158] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [1663241782.963022122] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [1663241783.464649394] [minimal_publisher]: Publishing: "Hello World: 2"
[INFO] [1663241783.963231469] [minimal_publisher]: Publishing: "Hello World: 3"
[INFO] [1663241784.464505740] [minimal_publisher]: Publishing: "Hello World: 4"
[INFO] [1663241784.964727795] [minimal_publisher]: Publishing: "Hello World: 5"
[INFO] [1663241785.464552693] [minimal_publisher]: Publishing: "Hello World: 6"
[INFO] [1663241785.963467024] [minimal_publisher]: Publishing: "Hello World: 7"
[INFO] [1663241786.464590671] [minimal_publisher]: Publishing: "Hello World: 8"
[INFO] [1663241786.965043845] [minimal_publisher]: Publishing: "Hello World: 9"
[INFO] [1663241786.966554516] [minimal_publisher]: Publishing: "Done"
2 | No.2 Revision |
Replace Please replace rclpy.shutdown()
with self.destroy_node()
. Please However, I am not sure if destroying a node inside a callback is a good idea. Nevertheless, please see the code snippet:
# Copyright 2016 Open Source Robotics Foundation, Inc.
# license removed for brevity
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 = 'Hello World: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1
if self.i==10: # I could put other non hard-coded tests here
msg.data = 'Done'
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.destroy_node() # <-- Here is where I want to the node to quit
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
rclpy.shutdown()
if __name__ == '__main__':
main()
Below is the info it prints at terminal:
$ ros2 run examples_rclpy_minimal_publisher publisher_member_function
[INFO] [1663241782.469494158] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [1663241782.963022122] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [1663241783.464649394] [minimal_publisher]: Publishing: "Hello World: 2"
[INFO] [1663241783.963231469] [minimal_publisher]: Publishing: "Hello World: 3"
[INFO] [1663241784.464505740] [minimal_publisher]: Publishing: "Hello World: 4"
[INFO] [1663241784.964727795] [minimal_publisher]: Publishing: "Hello World: 5"
[INFO] [1663241785.464552693] [minimal_publisher]: Publishing: "Hello World: 6"
[INFO] [1663241785.963467024] [minimal_publisher]: Publishing: "Hello World: 7"
[INFO] [1663241786.464590671] [minimal_publisher]: Publishing: "Hello World: 8"
[INFO] [1663241786.965043845] [minimal_publisher]: Publishing: "Hello World: 9"
[INFO] [1663241786.966554516] [minimal_publisher]: Publishing: "Done"