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

create rosmsg content from string

asked 2017-06-24 06:29:38 -0500

knxa gravatar image

With python: I want to in a generic way dynamically create ros messages where the values comes from a string representation. For example lets say I have a message definition:

float32 a
uint8 b

And I have the values of a and b in some string format, for example

a="1.23" b="17"

I need some conversion that knows that "a" is a float and "b" is an int. Does ROS messages comes with any "fromString/fromXml/fromYaml" methods? I know yaml can be used from the command line, e.g. with "rostopic pub". Is any of this functionality available from a python script?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-06-24 07:12:29 -0500

NEngelhard gravatar image

updated 2017-06-24 07:12:49 -0500

You know that 'rostopic pub' has this functionality, so your first step should be to check its code.

It is implemented in rostopic [Line 1707]. It's python so that there has to be a way to fill a message from such a string representation. The user input is stored in pub_args, so the next function to check is argv_publish, which leads to publish_message [L 1651], there, we find _fillMessageArgs [L. 1687], which then uses the final 'genpy.message.fill_message_args(msg, pub_args, keys=keys)' to fill the message. So the search continues to genpy.message and we finally find:

def fill_message_args(msg, msg_args, keys={}):
    """
    Populate message with specified args. Args are assumed to be a
    list of arguments from a command-line YAML parser. See
    http://www.ros.org/wiki/ROS/YAMLCommandLine for specification on
    how messages are filled. (...)

This should be the function you are looking for.

edit flag offensive delete link more

Comments

1

Thanks for pointing me in the right direction. It seems I don't need the fill_message_args itself. The msg_args in that function has already been converted to a dict with the proper types when called. But that conversion is done with the function yaml.load(...) which seems to be what I need. Thanks

knxa gravatar image knxa  ( 2017-06-25 13:16:34 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-06-24 06:29:38 -0500

Seen: 1,476 times

Last updated: Jun 24 '17