0
$\begingroup$

Rosanswers logo

Hi, I wish to create 2 rosnodes with some services having same name. E.g. rosnode N1 has services S1,S2,S4 and rosnode N2 has services S2,S3,S5. The reason to have services of same name is because they for a part of a generic description. However, I am not able to find a solution which can help me call the service from a specific node like N1.S2 and N2.S2. I am using python as my programming environment.

Also is it possible to get the proxy object of one node into another ?Like proxyForN1 = someRosCode.getProxyForNode('N1') The call the service of that node from the proxy like proxyForN1 .callService('S2') ?

It would be great if anyone could help out here !! Cheers !!


Originally posted by amarbanerjee23 on ROS Answers with karma: 5 on 2017-09-11

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

If you make sure to make the services of those nodes private (ie: advertise(..) them on a private nodehandle, or prefix the service name with the node name), then you just need to prefix the nodename to the service name when you create the service proxy as well.

Alternatively, you could start nodes in namespaces, which will automatically prefix service names (provided the service names haven't been made global (ie: start with a /).


Edit:

Hi, Thanks for the response :) Are you suggesting something like to start the server:

s = rospy.Service(' serviceRequest = rospy.ServiceProxy('NodeName/ServiceName', NodeName_ServiceName, callback_function)

No. That doesn't make sense to me.

Note: there is nothing magical about service names, they are just strings that we use as an address or URI.

To advertise a service in rospy that is private to n0, do something like (from the Writing a Simple Service and Client tutorial):

s = rospy.Service('~/add_two_ints', AddTwoInts, handle_add_two_ints)

This should result in a /n0/add_two_ints service server. Note the ~. We could use the node name, but if a user of your node specifies a different name (fi in their launch file), your service won't be properly prefixed.

In your service client you'd have to know that the service server is named n0, and you can then do (again from the tutorial):

add_two_ints = rospy.ServiceProxy('/n0/add_two_ints', AddTwoInts)

If you don't want start private services, you could also use namespacing, but that will require your client to be in the same namespace as your server, or you need to provide your client with the required information (ie: the namespace).


Originally posted by gvdhoorn with karma: 86574 on 2017-09-11

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by amarbanerjee23 on 2017-09-11:
Hi, Thanks for the response :) Are you suggesting something like:- to start the server :- s = rospy.Service(' serviceRequest = rospy.ServiceProxy('NodeName/ServiceName', NodeName_ServiceName, callback_function) ??

Comment by amarbanerjee23 on 2017-09-11:
If not, could you please give a small example ?? :) Cheers !!

Comment by gvdhoorn on 2017-09-11:
Note btw that hard-coding node names like this is not very flexible and will most likely make re-use of your nodes outside this narrow use-case more difficult than it could/should be.

Comment by amarbanerjee23 on 2017-09-12:
Hi @gvdhoorn, The suggested solution works completely fine. I see your point you said above, but for now my problem will be solved !! Thanks a lot man !! Cheers !!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.