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 2014-08-21 17:12:49 -0600

clarkeaa gravatar image

updated 2014-08-21 17:19:05 -0600

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
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-08-21 19:29:56 -0600

Mehdi. gravatar image

updated 2014-08-21 19:30:58 -0600

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)
edit flag offensive delete link more
0

answered 2014-08-21 19:26:02 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-08-21 17:12:49 -0600

Seen: 1,238 times

Last updated: Aug 21 '14