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

ERROR: service [/thruster_state] responded with an error: service cannot process request: handler returned invalid value: Invalid number of arguments, args should be ['success', 'message'] args are(False,)

asked 2020-05-19 22:25:23 -0500

SWAPNEEL001 gravatar image

updated 2022-01-21 10:47:12 -0500

130s gravatar image

I'm running the code below and getting the error stated above.

 #!/usr/bin/env python 
import rospy
from std_msgs.msg import String
from std_srvs.srv import SetBool

state = False
flag = 0
def thrusterstate(req):
    if req.data:
        global flag
        global state
        flag = 1
        state = True
        return True,"thruster is on"

     global flag
     global state
     flag = 1
     state = False
     return False,"thruster is off"


if __name__=="__main__":
    rospy.init_node("task2pynode")
    rospy.loginfo("Service Server started")
    service = rospy.Service ("/thruster_state",SetBool,thrusterstate)
    pub = rospy.Publisher("/thruster_message",String,queue_size = 10)
    rate = rospy.Rate(1)
    while not rospy.is_shutdown():
        msg = String()
        global state
        global flag
        if flag == 1:
            if state:
                msg.data = "ON"
            else:
                msg.data = "OFF"
            pub.publish(msg)
            rate.sleep()
        flag = 0
        rospy.spin()

I'm calling the service with rosservice call /thruster_state "data: false"

edit retag flag offensive close merge delete

Comments

1

Unrelated to your question, but a problem you might run into at some point: rospy.spin() is not meant to be used in while not rospy.is_shutdown(). You already have rate.sleep(), bring that outside the if-block

abhishek47 gravatar image abhishek47  ( 2022-01-22 02:36:52 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-01-21 10:59:21 -0500

130s gravatar image
Invalid number of arguments, args should be ['success', 'message'] args are(False,)

I agree rospy's Service server implementation doesn't do super great job with this error message. The 2nd "args" part is what you're passing while the 1st "args" is what the Server expects.

From this, we can tell that the Server is getting False,. I can assume that is what the line return False,"thruster is off" generates. The string portion is somehow cut off (which I have no idea why). Anyways, you're not passing in the format Server expects.

In rospy Service client implementation, you must use the specific class for the response. As you see in the tutorial wiki.ros.org/ROS/Tutorials/WritingServiceClient where the Server returns AddTwoIntsResponse while the Server's instantion declares AddTwoInts. Service name + Response is Python class auto-generated by the build system (i.e. Catkin etc.)

edit flag offensive delete link more
0

answered 2022-01-22 11:37:25 -0500

Mike Scheutzow gravatar image

Your thrusterstate() function should be returning a SetBoolResponse object.

If you don't provide one (and I think it is a terrible feature), the RosService code tries to auto-convert whatever you did return into a SetBoolResponse object. It screws this up a lot, so you are better off just returning the proper thing in the first place.

You can look at the SetBool python class definition file to understand how to create the SetBoolResponse object:

/opt/ros/melodic/lib/python2.7/dist-packages/std_srvs/srv/_SetBool.py

A quick look indicates this should work (untested):

resp = SetBoolResponse(False, "thruster off")
return resp
edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-05-19 22:25:23 -0500

Seen: 1,865 times

Last updated: Jan 22 '22