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

Python: roslaunch KeyboardInterrupt exception not caught try---catch

asked 2016-06-15 11:14:16 -0600

highWaters gravatar image

updated 2016-06-15 11:14:51 -0600

I'm using roslaunch to launch several nodes that do some calculations. I want to print some variables continuously into a file. Part of the code looks like this:

class Agent:

def __init__(self, var1, ...):

    filename = 'stdout_' + str(ID)
    self.stdout_log = open(filename, 'w')

    ## ROS Topics and Services ################################################
    ###########################################################################
    rospy.init_node('agent', anonymous=True)

    myservice = '/serve'
    srv = rospy.Service(myservice, Service_One, self.handle_serve)

    self.publish_global = rospy.Publisher('msgs', Message_Type, queue_size=200)
    rospy.Subscriber('trigger', Message_Type, self.callback)
    ###########################################################################
if __name__ == '__main__':
agent = Agent(var1,...)

try:
    while True:
        time.sleep(1)
        print 'stuff'
        agent.stdout_log.write('stuff')
except rospy.ROSInterruptException:
    pass
finally:
    agent.stdout_log.close()

If I give ctrl-c from the terminal window where I have exectued roslaunch, the file is created but there's nothing in it. After some trials, I suspect that it is because the file doesn't get to close. I try to ctrl-c the nodes themselves, but it doesn't work. If I remove the ros related declarations in __init__ then I can stop the node with ctrl-c. What can I do?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-06-15 11:29:41 -0600

updated 2016-06-15 11:31:20 -0600

In the condition of the main loop use this:

while not ropy.is_shutdown() :
   rospy.sleep(1.0)

rospy.is_shutdown is required to attend to the ending signals of the process.

edit flag offensive delete link more

Comments

Thank you!!!

highWaters gravatar image highWaters  ( 2016-06-15 14:18:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-15 11:14:16 -0600

Seen: 1,119 times

Last updated: Jun 15 '16