How to print out multiple responses from service
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']