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

Pass an object to Service handler callback() function

asked 2021-06-09 08:58:04 -0500

TELESPIELSTUBE gravatar image

updated 2021-06-09 10:33:13 -0500

gvdhoorn gravatar image


is there a way to pass objects to a callback()? I just found this post:

but if I pass the object like mentioned in the post I get this error message:

Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/", line 737, in receive_once
    self.stat_bytes += recv_buff(sock, b, p.buff_size)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/", line 104, in recv_buff
    d = sock.recv(buff_size)
TypeError: 'AiService' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/", line 932, in _bootstrap_inner
  File "/usr/lib/python3.8/", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/", line 667, in handle
    requests = transport.receive_once()
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/", line 758, in receive_once
    raise TransportException("receive_once[%s]: unexpected error %s"%(, str(e)))
rospy.exceptions.TransportException: receive_once[/image_ack]: unexpected error 'AiService' object cannot be interpreted as an integer

My code (shortened):

def callback(request, arg):
    response = ImageAckResponse()
    ai_machine = arg[0]
    return response

def main():
    ai_machine = AiMachine()
    rospy.Service('image_ack', ImageAck, callback, (ai_machine))
edit retag flag offensive close merge delete


Is service already a tuple? If not, you must enclose it in (). Otherwise you cannot unpack it in your callback.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-09 09:09:30 -0500 )edit

Changed the object name to ai_machine for better understanding and enclosed the object but still getting the same error

TELESPIELSTUBE gravatar image TELESPIELSTUBE  ( 2021-06-09 09:21:02 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-06-09 09:55:45 -0500

gvdhoorn gravatar image

updated 2021-06-09 09:58:04 -0500

Not sure whether anything has changed in rospy in the meantime, but I believe the problem is that rospy.Service(..) does not support that additional args argument described in the Q&A you link. That's for rospy.Subscriber(..) only.

See #q247540 for a potential work-around, using a lambda.

Another option would be to create a class, and make callback(..) a method on that class. It could then refer to any member variables of that class.

The reason you get that error ("") is because rospy.Service(..) expects a buff_size argument (an integer) where you are passing your AiMachine instance. See the documentation:

__init__(self, name, service_class, handler, buff_size=65536, error_handler=None)
edit flag offensive delete link more

Question Tools



Asked: 2021-06-09 08:58:04 -0500

Seen: 363 times

Last updated: Jun 09 '21