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

Creating a service that accepts no arguments

asked 2016-08-24 07:04:13 -0500

gavran gravatar image

updated 2016-08-24 08:10:36 -0500

I am trying to create a service that accepts no arguments and returns a string. My attempt in srv file was

---     
string return_value

The node that implements the service looks like this

from testService.srv import NoArguments
import rospy

def service_implementation():
    return "answer"

if __name__=="__main__":
    rospy.init_node('service_node')
    s = rospy.Service('service_with_no_arguments', NoArguments, service_implementation)
    rospy.spin()

However, when I try to call it, I get an error:

error processing request: service_implementation() takes no arguments (1 given)

What would be the right way to define a service that accepts no arguments?

EDIT: I noticed that one way to do it would be to add (some) argument to service_implementation so that it looks like

def service_implementation(request):
        return "answer"
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2016-08-24 07:19:55 -0500

gvdhoorn gravatar image

updated 2016-08-24 08:27:56 -0500

--- string return_value

The basic approach seems ok, but I'd add a new-line between the --- and your string return_value, like so:

---
string return_value

You write:

However, when I try to call it, I get an error that function accepted 0 arguments and got 1

Could you please add a snippet to your question that shows exactly what you're trying to do, and what the exact error message is?


Edit:

EDIT: I noticed that one way to do it would be to add (some) argument to service_implementation so that it looks like

def service_implementation(request):
        return "answer"

Ah, yes.

When talking about 'services', we (I ?) always think of the abstracted ROS API level. So a 'no arguments service' is a ROS service that takes no arguments (like you defined), but at the C++/Python implementation level, you'll always need to accept (and handle) the request-reply pair that gets supplied by the ROS middleware to your client program.

For a 'no arguments service', that will just mean that the Request type has no (public) member fields. In Python, returning just a str probably then works because it's automatically transformed using the appropriate constructor of the Reply type.

edit flag offensive delete link more

Comments

I edited the question. While doing it, I understood that one solution would be adding the argument to service_implementation function (and then ignoring it). that would be - it is not without arguments, but with an empty argument. If you have additional comments, please add.

gavran gravatar image gavran  ( 2016-08-24 08:08:42 -0500 )edit

the new line issue was a formatting problem

gavran gravatar image gavran  ( 2016-08-24 08:11:04 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-24 07:04:13 -0500

Seen: 6,129 times

Last updated: Aug 24 '16