Ask Your Question
0

Best way to pass parameters to a service

asked 2014-08-19 03:57:24 -0600

lucaluca gravatar image

Hi,

It may seem a trivial question, but I couldn't find a good answer in the internet. I would like to write a service (NOT a node) that receives some parameters in input, but I couldn't find a way to pass them that is similar to what you could do with a node (where you retrieve the parameters through the node handler). The only way seems to use the parameter server, but if possible I would prefer to avoid using those kind of "global variables". Any suggestions?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-08-19 04:43:07 -0600

dornhege gravatar image

Your question is somewhat confusing. A service always IS part of a node, so you'll have to write a node. Any ROS program is a node. As such you can also use the normal ROS parameter APIs for getting parameters to be used by services.

ROS parameters used by nodes are also commonly set on the parameter server. That is the standard use case. Private parameters are by convention those in a node's namespace, although they are globally accessible.

The two other options you have besides that: 1. Pass command line parameters, 2. pass the parameters as part of the service call.

edit flag offensive delete link more

Comments

@dornhege Ok, I was actually a little bit confused. So, I would like to allow the user to pass parameters as part of the service call, so that he can run the service once and make some trials calling it with different parameters. But how do I do this?

lucaluca gravatar imagelucaluca ( 2014-08-19 04:52:52 -0600 )edit

In that case, just add the parameters to the service request.

dornhege gravatar imagedornhege ( 2014-08-19 07:38:40 -0600 )edit

So I have to add them to the service request? But I think in this case they become necessary, while I'd like to have a series of optional parameters, that the user can decide to pass or not..

lucaluca gravatar imagelucaluca ( 2014-08-19 08:25:59 -0600 )edit

A service request is always the full struct. However, you can define some values to be "undefined" and handle those correctly.

dornhege gravatar imagedornhege ( 2014-08-19 09:09:52 -0600 )edit

But in this way the user should set to "undefined" all the parameters he doesn't want to use.. Actually I thought about passing only the node handle as a parameter, so that using getParam() I can check which parameters the user decided to pass, but it doesn't seem a very clean solution..

lucaluca gravatar imagelucaluca ( 2014-08-19 09:12:28 -0600 )edit

You cannot pass a node handle through a service call. That is a local construct. If those parameters directly influence the return value of the service, it seems to me that they belong to the request anyways.

dornhege gravatar imagedornhege ( 2014-08-19 09:19:16 -0600 )edit

Be aware of using parameters and setting them for a request in your way. Parameters are not deleted unless explicitly specified, So if a parameter is not set, it might still have the value of a previous call.

dornhege gravatar imagedornhege ( 2014-08-19 09:20:53 -0600 )edit

Yeah, but I don't want to force the user to insert also unnecessary values. So the two alternatives should be the one you suggested or using the parameter server, so that he can only set on the parameter server only the parameters he needs, right?

lucaluca gravatar imagelucaluca ( 2014-08-19 09:21:42 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-19 03:57:24 -0600

Seen: 836 times

Last updated: Aug 19 '14