0
$\begingroup$

Rosanswers logo

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


Originally posted by rospy_FACEPALM on ROS Answers with karma: 33 on 2012-01-27

Post score: 3

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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)


Originally posted by mmwise with karma: 8372 on 2012-01-27

This answer was ACCEPTED on the original site

Post score: 6


Original comments

Comment by rospy_FACEPALM on 2012-01-28:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.