First time here? Check out the FAQ!


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

How can I restart rospy after pressing Ctrl-C? (Python, Bash)

asked Aug 21 '14

clarkeaa gravatar image

updated Aug 21 '14

Hello,

I searched through the rospy section of the tutorials but I can't find out simply how to restart rospy after it has been shutdown. I made a code that runs a routine, then asks if the user wants to restart the routine. If i press yes, it will restart successfully if the routine was complete. But if I press ctrl-C within the routine to interrupt it, it asks if the user wants to restart the routine, and then if you choose Yes, it will run the loop again except rospy is no longer running, so nothing happens. I put test messages in my code in order to verify that the loop is indeed running again, and I found that the sections where I run "while not rospy.is_shutdown" all fail to run. Therefore I know that rospy is shutting down as soon as I press ctrl-C. How can I either prevent shutdown or restart rospy after pressing ctrl-C?

Here is the form of my code:

 def robot_exec():
   # Initialize Rospy
   rospy.init_node('my_robot_exec')

   # List of robot arm poses:
   *many_many_lines_of_position_vectors*

   # List of robot gripper poses:
   *open_position*
   *close_position*

   while 1:
     #*Call function that moves the robot arm(s) to a position on the list using publisher*
     #*Call function that moves the robot gripper(s) to a position on the list using publisher*
     #*continue calling functions many times until the desired routine is complete*
     n = raw_input("Restart the routine? (Y/N)")
     if n.strip() == 'n' or 'N':
       break
     elif n.strip() == 'y' or 'Y':
       continue
     else:
       print "Invalid input. Exiting..."
       break
Preview: (hide)

2 Answers

Sort by » oldest newest most voted
0

answered Aug 22 '14

Mehdi. gravatar image

updated Aug 22 '14

There is probably better ways to do what you want to do but for your case, you could catch the SIGINT signal and then restart your function. Try adding this to your code

import signal
import sys
def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        #Reexecute function
        robot_exec()
signal.signal(signal.SIGINT, signal_handler)
Preview: (hide)
0

answered Aug 22 '14

ahendrix gravatar image

ctrl-C is the standard way to terminate a command-line program. you should try to respect that, not circumvent it.

I would probably change your while 1: loop to while not rospy.is_shutdown

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Aug 21 '14

Seen: 1,238 times

Last updated: Aug 21 '14