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

rospy: Calling /clear service programatically

asked 2012-01-27 08:44:46 -0600

rospy_FACEPALM gravatar image

updated 2012-02-27 07:34:44 -0600

kwc gravatar image

I have not seen the answer to this in docs or on this forum.

I'm writing a simple program publishing to command_velocity to move a turtle around in turtlesim_node (all of this in rospy). Before I start moving my turtle, I'd like to change the background color.

I've updated the relevant parameters but now I find myself completely unable to call the /clear service from within my python node.

According to elsewhere on the interwebs, this line of code should work:

clear_bg = rospy.ServiceProxy('clear', turtlesim.srv.Clear)

yet I'm getting "AttributeError: 'module' object has no attribute 'Clear'"

And, sure enough, when I roscd over to [...]/ros_tutorials/turtlesim/srv/, there is no clear.srv file. Ros is kicking my butt on this one point and if you can help, you will have my eternal gratitude for at least the rest of the day.

FYI - I'm hoping not only for a line of code in the answer but also a short explanation of how you knew where to find this - it seems frustratingly underdocumented! I'll be working with ROS more in the future so I'd hope to learn something more from this than just the right characters to type in this one uncommon case.

PS - I would prefer not to post my entire code. This is a small part of a class assignment (maybe 1-2%?) and I want to avoid putting the whole of the source code out there for google to index and others to find...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
6

answered 2012-01-27 09:25:58 -0600

mmwise gravatar image

When creating a service proxy the arguments to the function are name and type

rospy.ServiceProxy(service_name, service_type)

The service_name is the name you see in the list when you call rosservice list. The service type is the result you get when you call rosservice type service_name.

What you actually want to do is this:

clear_bg = rospy.ServiceProxy('clear', std_srvs.srv.Empty)

The above line of code basically connects to the service with name clear and type std_srvs.srv.Empty. If you look at the turtlesim documentation page (http://ros.org/wiki/turtlesim) you can see that the service is listed as:

clear (std_srvs/Empty)

You may also want to throw in a wait for service so that you don't get an error when trying to call the service call in a node that is roslaunched.

rospy.wait_for_service('clear')

You can read more about services here: http://ros.org/wiki/rospy/Overview/Services

This is also explained here: http://ros.org/wiki/ROS/Tutorials/WritingServiceClient(python)

edit flag offensive delete link more

Comments

Thank you for the succinct and well-explained answer! I checked both pages you list at the bottom several times on my own but for whatever reason this point never clicked for me.
rospy_FACEPALM gravatar image rospy_FACEPALM  ( 2012-01-28 03:36:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-01-27 08:44:46 -0600

Seen: 5,985 times

Last updated: Jan 27 '12