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

Do the calculations are processed on the server in python format?

asked 2019-02-18 01:33:23 -0500

little_bob gravatar image

I followed the official instructions to write simple Service Node, and I find there are some different places between C++ and Python formats.

In pyhton formats code of add_two_ints_client.py:

#!/usr/bin/env python

import sys
import rospy
from beginner_tutorials.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.sum
except rospy.ServiceException, 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)
print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))

It seems that the addition is done within the client code:

add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum

But in C++ code it seems that the addition is done within the server and send the result back to the client. I will attach the link of the official tutorials: python service, c++ service. Is the python format code against Server/Client rule?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-02-18 01:39:30 -0500

gvdhoorn gravatar image

But in C++ code it seems that the addition is done within the server and send the result back to the client.

Correct. That is how the service server & service client examples are set up.

Is the python format code against Server/Client rule? [..] It seems that the addition is done within the client code:

add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum

I can understand why you'd have the impression that add_two_ints(..) is executed on the client, but if you examine the server code carefully, you'll see the addition is actually performed by the server (from the tutorial you linked):

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

The "extra code" in the Python client example just wraps the following steps:

  1. making sure the add_two_ints service exists and is available
  2. creating the actual service client object (ie: ServiceProxy)
  3. invoking the service
  4. waiting for the result
  5. returning the result
  6. (optionally) handling errors (in this case simply printing the exception)

My guess is that the example author felt it would be cleaner to wrap all of that in a function, instead of placing all of those lines in the main() directly.

edit flag offensive delete link more

Comments

Got it, thank you for your reply. I wrongly thought that "add_two_ints(x,y)" is a method, actually, it is a service proxy object, just like the code in C++:client.call(srv).

little_bob gravatar image little_bob  ( 2019-02-18 01:48:53 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-18 01:33:23 -0500

Seen: 192 times

Last updated: Feb 18 '19