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

access parameters in service function

asked 2018-07-05 07:02:54 -0500

Matthi gravatar image


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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2018-07-06 01:54:50 -0500

mgruhler gravatar image

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.)

edit flag offensive delete link more


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?

yashtrikannad gravatar image yashtrikannad  ( 2019-11-23 17:30:53 -0500 )edit

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.

ahendrix gravatar image ahendrix  ( 2019-11-24 00:08:30 -0500 )edit

answered 2018-07-09 00:35:33 -0500

Matthi gravatar image


thanks for your answer. It was really helpful. I've managed it now by creating a global pointer and access it in the needed functions.


edit flag offensive delete link more


Please don't use an answer to leave a comment. This isn't a forum. To leave a comment on someone's answer, click the "add a comment" button below the answer.

jayess gravatar image jayess  ( 2018-07-09 02:05:16 -0500 )edit

Question Tools

1 follower


Asked: 2018-07-05 07:02:54 -0500

Seen: 460 times

Last updated: Jul 09 '18