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

It is possible to send dynamic request to a service?

asked 2015-07-29 04:10:57 -0500

Leontes gravatar image

updated 2015-07-29 13:11:25 -0500

lucasw gravatar image

Hi, I have a robot written in Python, and I use the different components of that robot using imports.

The problem is that I'm trying to make ros services for this components, but some of the objects that I need to send are created without atributes and added later using the setattr method. So at first I don't know the composition of the object until the execution of the component finish.

It is posible to make a SRV file that accepts something like that?

For example here I declare the class State

class State():
"""A state is just a collection of variable bindings."""
def __init__(self,name):
    self.__name__ = name

And then in execution time I bind new variables to the objects of type State.

edit retag flag offensive close merge delete



Can you give an example? I do not really get it.

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-29 04:27:27 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-07-29 12:25:17 -0500

updated 2015-07-29 12:49:49 -0500

Ok I give it a try. If I understand you right, you want to execute multiple and always different functions with one function call? Well I guess it is not supported by the rosservice, but you could probably use a string and execute the python code in it (storing your function calls) ... what ever you need that for ...

But I really do not know if this would be "good practice", it somehow does not feel like that for me, if you know what I mean and of course I understood you right. ;-)


Yes you can create services for each object you create of course also at runtime, but the service name has to be unique systemwide. So if you have a serviceFoo1 there is only this service called like that, others must be called serviceFoo2 etc... And the service file (with defined parameters) must exist, before you run the application! You can of course do multiple of them and use each multiple times. But they must be there before runtime! The classes are created by catkin

So adding parameters won't work at runtime, well except you would use my first try, that I won't recommend.

Update 2 (Comment):

You can only use standard variables for the parameters: int, uint, bool, string, float (also in arrays and dynamic arrays) and any message type, but they also only use standard types.

You could probably use JSON (in a string) to transmit a class object to your function (would feel ok for me and also works in CPP or python). Or you could send python class code (somehow this does not feel right).

edit flag offensive delete link more


I've modified the post trying to clarify my question. Sorry, I'm not very fluent writting in English. I need to create a a SRV file with a mutable object with unknow variables at first.

Leontes gravatar image Leontes  ( 2015-07-29 12:31:20 -0500 )edit

So... I can't change my code to use Services instead to function calls to the different modules of the robot. Thank you for your help.

Leontes gravatar image Leontes  ( 2015-07-29 12:55:17 -0500 )edit

You could send objects with variables with JSON and define a string message and python has standart functions for it. Maybe you could add what you are up to, what kind of data is in your dataset? Actually something like that is used in ROS already -> diagnostics. Topics with key and value (string)

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-29 13:05:03 -0500 )edit

Question Tools

1 follower


Asked: 2015-07-29 04:10:57 -0500

Seen: 138 times

Last updated: Jul 29 '15