ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
2

Hang on wait_for_server() on import, but not direct use

asked 2011-07-26 05:48:56 -0500

joschu gravatar image

updated 2014-01-28 17:10:07 -0500

ngrennan gravatar image

I have a function called gripper.py with the lines

grip_r_client = actionlib.SimpleActionClient('r_gripper_controller/gripper_action', Pr2GripperCommandAction)
print "waiting for right gripper server"
grip_r_client.wait_for_server()

Then I have a script, test_gripper.py. If I try to import gripper.py, the program gets stuck at wait_for_server(). On the other hand, if I just copy those lines into test_gripper.py, or if I do execfile('gripper.py'), it works.

What's going on?

Edit: here's the full code. I would upload the package, but I don't have enough karma.

gripper.py:

import roslib
roslib.load_manifest('test')
import rospy, actionlib
from pr2_controllers_msgs.msg import Pr2GripperCommandAction
grip_r_client = actionlib.SimpleActionClient('r_gripper_controller/gripper_action',Pr2GripperCommandAction)
print "waiting for right gripper server"
grip_r_client.wait_for_server()

test_gripper.py:

import roslib
roslib.load_manifest('test')
import rospy,actionlib
rospy.init_node('test_gripper') 
import gripper
# OR ...
#execfile("gripper.py")

I realize this is non-idiomatic code, and it's really easy to fix the problem (I create a Gripper class, and wait_for_server in its __init__ method), but it's a strange phenomenon.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2011-07-26 08:43:04 -0500

eitan gravatar image

In order for wait_for_server to work, ROS has to be initialized. When you import gripper.py, python tries to run the 3 lines you pasted above since, I assume, they're not contained within any kind of function. So, if you're doing the import at the top of the file before a call to rospy.init_node the wait_for_server call will never return.

Normally, you'd wrap those three lines up in a function, import the file, and then call the function later after a call to rospy.init_node.

I'll admit that I'm guessing on a lot of the points I made since there's not enough information to be sure what's happening in the original post, but hopefully this helps.

edit flag offensive delete link more

Comments

Nope, I did initialize the node before the import. I edited my post to give more detail.
joschu gravatar imagejoschu ( 2011-07-28 12:30:32 -0500 )edit
I think you are missing a rospy.init_node(...)
tpratkanis_wg gravatar imagetpratkanis_wg ( 2011-09-02 12:28:03 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-07-26 05:48:56 -0500

Seen: 432 times

Last updated: Jul 28 '11