0
$\begingroup$

Rosanswers logo

Hello,

I would like to use some parameters stored in the parameter server within a service function. I tried a lot to pass the nodeHandle as an additional parameter with functions from the boost library, but it didn't work. Can anyone give me some advice how to correctly access the NodeHandle, which is created in the main function, within the service function whenever the service is called? It would be nice if I could get a working example how to achieve this. I program the nodes in C++.

Thanks a lot


Originally posted by Matthi on ROS Answers with karma: 13 on 2018-07-05

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I'm assuming you mean the service callback with service function.

Basically you have to possibilities.

  1. If you really want to use the NodeHandle that is created outside the service callback, make this a member variable (if you are using a class) and just access that, or make it a global variable (if you don't have a class). No need to pass it to the service callback.
  2. You can just create a new NodeHandle in the scope of the service callback. You can have as many NodeHandles as you want. And then use this to retrieve the parameter. Again, no need to pass it in.

Thus, my suggestion would be to use 1 if you have a class (obviously, the service callback needs to be a member function of the class as well) and 2 if you don't.

(As both ways are trivial, I'll not provide example code for now.)


Originally posted by mgruhler with karma: 12390 on 2018-07-06

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by yashtrikannad on 2019-11-23:
I am not able to call class member functions in the service. It shows that the callback is not a non-static member of the class. If I try to make the callback function static, I will not be able to access the class members(parameters). So I am not sure if the first option will work?

Comment by ahendrix on 2019-11-24:
You can pass member function pointer and object when creating the service; then the service handler can be a non-static member function and use class members.

$\endgroup$

Your Answer

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