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

How to print out multiple responses from service

asked 2021-06-28 16:02:07 -0500

Verismoto gravatar image

updated 2021-06-29 08:11:19 -0500

Hello, I'm currently trying to create a service which can send multiple responses. I edited the tutorial service ( http://wiki.ros.org/ROS/Tutorials/Wri... ) to:

int64 a
int64 b
---
int64 sum
int64 multi

multi = a * b

in another question (https://answers.ros.org/question/3812...) somebody already helped me to adjust the server.py but so far I'm not able to print out sum and multi this is my attempt so far client.py:

#!/usr/bin/env python

from __future__ import print_function

import sys
import rospy
from service_node.srv import *

def add_two_ints_client(x, y):
    rospy.wait_for_service('add_two_ints')
    try:
        add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
        resp1 = add_two_ints(x, y)
        return resp1
    except rospy.ServiceException as e:
        print("Service call failed: %s"%e)

def usage():
    return "%s [x y]"%sys.argv[0]

if __name__ == "__main__":
    if len(sys.argv) == 3:
        x = int(sys.argv[1])
        y = int(sys.argv[2])
    else:
        print(usage())
        sys.exit(1)
    print("Requesting %s+%s"%(x, y))
    resp = add_two_ints_client(x, y)
    print(resp)

server.py:

#!/usr/bin/env python

from __future__ import print_function

from service_node.srv import AddTwoInts,AddTwoIntsResponse
import rospy

def handle_add_two_ints(req):
    print("Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)))
    return AddTwoIntsResponse(sum(req.a + req.b), multi=(req.a * req.b))

def add_two_ints_server():
    rospy.init_node('add_two_ints_server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print("Ready to add two ints.")
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()

output:

Requesting 5+1
Service call failed: service [/add_two_ints] responded with an error: b"error processing request: 'int' object is not iterable"
None

System: ROS noetic, Ubuntu 20.04, Python 3

Traceback from the server:
[ERROR] [1624971980.654584]: Error processing request: multi is not an attribute of AddTwoIntsResponse
['Traceback (most recent call last):\n', '  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_service.py", line 632, in _handle_request\n    response = convert_return_to_response(self.handler(request), self.response_class)\n', '  File "/home/fabi/catkin_ws/src/service_node/scripts/server.py", line 10, in handle_add_two_ints\n    return AddTwoIntsResponse(sum=(req.a + req.b), multi=(req.a * req.b))\n', '  File "/home/fabi/catkin_ws/devel/lib/python3/dist-packages/service_node/srv/_AddTwoInts.py", line 154, in __init__\n    super(AddTwoIntsResponse, self).__init__(*args, **kwds)\n', '  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 361, in __init__\n    raise AttributeError(\'%s is not an attribute of %s\' % (k, self.__class__.__name__))\n', 'AttributeError: multi is not an attribute of AddTwoIntsResponse\n']
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-29 02:43:12 -0500

gvdhoorn gravatar image

updated 2021-06-29 02:47:13 -0500

Is there a traceback printed by the server?

Can you call your service from the command line (ie: with rosservice call ...)?


Edit: this line could be problematic:

print("Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)))

If you change all %s to %d, does it stop complaining?


Edit 2: I don't believe this is completely correct:

return AddTwoIntsResponse(sum(req.a + req.b), multi=(req.a * req.b))

Note the sum(req.a + req.b). That should be sum=(req.a + req.b).

edit flag offensive delete link more

Comments

I added the traceback to my question. Multi seems to be not an attribute of AddTwoIntsResponse. I also fixed the sum = (req.a +req.b) now the output is:

Requesting 5+1
Service call failed: service [/add_two_ints] responded with an error: b'error processing request: multi is not an attribute of AddTwoIntsResponse'
None

Changing from %s to %d also had no impact And I can't call the service with rosservice call but rossrv show service_node/AddTwoInts works

Verismoto gravatar image Verismoto  ( 2021-06-29 08:13:46 -0500 )edit
1

Multi seems to be not an attribute of AddTwoIntsResponse.

then it would suggest that the changes you've made to the service definition have not been consistently applied everywhere.

but rossrv show service_node/AddTwoInts works

and what does that show?

gvdhoorn gravatar image gvdhoorn  ( 2021-06-29 09:33:11 -0500 )edit

after running catkin_make again the output is now

rosrun service_node client.py 5 1
Requesting 5+1
sum: 6
multi: 5

so the service is now working, but multi has the wrong result

rossrv show service_node/AddTwoInts shows the correct .srv file

int64 a
int64 b
---
int64 sum
int64 multi
Verismoto gravatar image Verismoto  ( 2021-06-29 09:58:06 -0500 )edit
1

but multi has the wrong result

how is 5 wrong? 5*1==5, is it not?

gvdhoorn gravatar image gvdhoorn  ( 2021-06-29 10:03:32 -0500 )edit

oh you are right sorry, i always tested with 5 * 2 before now it finally works thanks alot

Verismoto gravatar image Verismoto  ( 2021-06-29 10:29:56 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-06-28 16:02:07 -0500

Seen: 36,394 times

Last updated: Jun 29 '21