How to exit a ROS node on keyboard interrupt?
How do you write a Python ROS node so that it exits in response to a KeyboardInterrupt exception?
I have a trivial node like:
class MyNode:
def __init__(self):
while 1:
do stuff
if __name__ == '__main__':
rospy.init_node('mynode')
MyNode(rospy.get_name())
But pressing ^C doesn't stop it. I see my "^C" interrupt character appear in the output of my "do stuff" code, but the node continues to run, almost as though it's not running in the main thread.
I tried implementing a rospy.on_shutdown()
callback, but it's never called in response to a KeyboardInterrupt.
rospy.on_shutdown()
callback has to be called before SIGKILL. Can you please show your implementation?