How do you implement a rospy KeyboardInterrupt without killing the node?
I want to run a process in a while loop that prints to screen, and exit the while loop by pressing a key or 'Ctrl+C'. But I don't want the same key press to also kill the ros node - I just want to exit the loop. In python you can use KeyboardInterrupt
like so:
import time
while True:
try:
print 'Help! Im stuck in a while loop! Press Ctrl+C to save me!'
time.sleep(0.5)
except KeyboardInterrupt:
break
do_other_stuff()
But this doesn't work in a ROS node, and I assume it's because rospy
handles the 'Ctrl+C' command in a slightly different way (but please correct me if I'm wrong).
I've tried the ROS equivalent, which replaces KeyboardInterrupt
with rospy.ROSInterruptException
but this doesn't work either - ROS simply never acknowledges the 'Ctrl+C' command. I've also tried checking for rospy.is_shutdown()
in the while loop, as per this question. But this kills the node outright, so I can't continue running other functions or run the while loop again.
Does anyone have a way of achieving this? Or does one of my methods work but I've got the syntax wrong?