ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Print subset of message

asked 2018-10-23 03:14:59 -0600

aPonza gravatar image

I would like to programmatically print a subset of this message, specifically only the flags set to true. I have found only this question regarding the display of messages, and the answer suggests to use ros::message_traits::Definition<franka_msgs::Errors>::value() which, in this case is a method which returns a long string made like this:

return "bool joint_position_limits_violation\n\
bool cartesian_position_limits_violation\n\

Is storing this string and parsing it the only way I can get the names of the errors to use in a for loop that checks which flags are true? Is there no way to get the name of the single error from the message itself?

edit retag flag offensive close merge delete



You could take a look at ros_type_introspection.

Afaik there is no OotB support for this, but that library tries to fill that gap.

gvdhoorn gravatar image gvdhoorn  ( 2018-10-23 03:36:13 -0600 )edit

If you know the message (thus all the fields) maybe you could compare each field in the callback and only print if true like that :

if (msg->joint_position_limits_violation)
//Same for each fields of the message
Delb gravatar image Delb  ( 2018-10-23 03:38:01 -0600 )edit

What @Delb suggests is certainly an approach for this specific message.

I took the question by @aPonza to be a bit more general in which case hard-coding wouldn't be feasible.

gvdhoorn gravatar image gvdhoorn  ( 2018-10-23 03:43:36 -0600 )edit

If you are still intereste to know the answer, I can write an example for you (I am the author of ros_type_introspection).

Davide Faconti gravatar image Davide Faconti  ( 2019-01-24 08:24:27 -0600 )edit

I am, I was looking at this question again exactly yesterday to do another similar thing to a message and couldn't remember the name of your package, but in the end delayed the task in favour of another.

aPonza gravatar image aPonza  ( 2019-01-24 09:47:41 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-01-25 10:45:19 -0600

Davide Faconti gravatar image

This is one of the problems that ros_type_intorspection can solve for you.

I created an example here just for you ;)

As you can see, you can deserialize __any__ ROS message into a list of Key/Value pairs, one for each field of the message.

In your particular case, to keep the example simple, we just used the fact that we know that all the fields are bool. here you can see an incomplete snippet of the solution:

void DeserializeAndPrint(RosIntrospection::Parser* parser,
                         const std::string& topic_name,
                         std::vector<uint8_t> & buffer) {

    using namespace RosIntrospection;
    FlatMessage flat_container;
    parser->deserializeIntoFlatContainer( topic_name,
                                          &flat_container, 100 );

    RenamedValues renamed_numerical_values;
    parser->applyNameTransform( topic_name,
                               &renamed_numerical_values );

    for(auto& it: flat_container.value)
        bool value = it.second.convert<int8_t>();

        if( value )
            std::cout << it.first.toStdString() << " : "
                      << it.second.convert<double>() <<  std::endl;
edit flag offensive delete link more

answered 2018-10-24 02:03:39 -0600

aPonza gravatar image

Thanks @gvdhoorn for the suggestion! Thanks @Delb, but as said I'm looking for something more general. I thought there would be some built-in way that I wasn't seeing/finding. I looked into ros_type_introspection and it is more powerful than what I need now... I ended up parsing, for now. Cheers!

edit flag offensive delete link more



I ended up parsing, for now

that's basically what ros_type_introspection does.

gvdhoorn gravatar image gvdhoorn  ( 2018-10-24 02:11:33 -0600 )edit

But it introduces a dependency, where I only really need a tidbit of the functions the introspection grants. The package would seem much more suited to the task if I didn't already know I want to parse a franka_msgs::Errors.

aPonza gravatar image aPonza  ( 2018-10-24 03:26:19 -0600 )edit

If your approach works for you +1.

I just wanted to clarify what ros_type_introspection does.

re: dependency: it's released on every major ROS release, so a single apt-get install .. or rosdep away.

gvdhoorn gravatar image gvdhoorn  ( 2018-10-24 03:29:21 -0600 )edit

I had not realized this. I'll add a todo and think about it in the future. Thanks again for the great help!

aPonza gravatar image aPonza  ( 2018-10-24 03:36:54 -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


Asked: 2018-10-23 03:14:59 -0600

Seen: 401 times

Last updated: Jan 25 '19