ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Why won't my node exit

asked 2022-01-12 09:14:42 -0500

renob321 gravatar image

updated 2022-01-12 10:13:35 -0500

I'm running a simple node with a single timer(10hz), when a flag running is set the node should destroy itself and I expect to exit out of rclpy.spin(), however nothing happens. I need to Ctrl+C to exit the program. Is this to be expected?

Code:

def MyNode(Node):
 def __init__(self, name):
  super().__init__(name)
  self.running = false
  self.create_timer(0.1, self.action)
 def action(self):
  self.running=true
  self.destory_timer()
  self.destory_node()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-14 02:42:27 -0500

jannkar gravatar image

Destroying the node won't exit rclpy.spin() loop. One way to implement this is by checking your node's self.running status in main loop and using rclpy.spin_once() -function. Here is a fully working example:

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node


class MyNode(Node):
    def __init__(self, name):
        super().__init__(name)
        self.running = True
        self.timer = self.create_timer(0.1, self.action)

    def action(self):
        print("Callback")
        self.running = False
        self.destroy_timer(self.timer)


def main(args=None):
    rclpy.init(args=args)

    my_node = MyNode("my_node")
    while my_node.running and rclpy.ok():
        try:
            rclpy.spin_once(my_node)
        except KeyboardInterrupt:
            break

    my_node.destroy_node()
    print("Shutting down rclpy")
    rclpy.shutdown()


if __name__ == '__main__':
    main()
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-01-12 09:14:42 -0500

Seen: 412 times

Last updated: Jan 14 '22