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 !!