Ask Your Question

rospy: Calling /clear service programatically

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

rospy_FACEPALM gravatar image

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

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

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

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 ( 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.


You can read more about services here:

This is also explained here:

edit flag offensive delete link more


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 -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

1 follower


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

Seen: 5,103 times

Last updated: Jan 27 '12