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

How to access contents of a custom message

asked 2018-06-01 03:33:34 -0600

jotator gravatar image

Hello everyone,

I am working on an application that, among other things, must include a ROS node that connects to an off-board ROS network (where the Master runs) and publishes and subscribes to some custom messages (some of them kind of big) with another node. The node I develop must be attached to an already developed C application that has to encode the received messages into some form of C structure, serialize it and send it to yet another application, and viceversa.

The approach I have in mind is to communicate the ROS node and the C application with shared memory (this is because the current design of the C app already uses it), but I am new to C++ and looking at the ROS C++ tutorial I can't clearly see how to access the data fields in a complex message that is three or four fields deep. I fact, I am already thrown off by the fact that the "String" message shown in the publisher/subscriber example only contains one data field but still a "c_str()" function is used as if the message object had member functions and not just the "data" field.

Thank you!

edit retag flag offensive close merge delete

Comments

Just a comment here: std_msgs/String contains a single field data of type string, which in C++ gets mapped to std::string (see wiki/msg).

So in the end msg->data is just a std::string. Makes perfect sense that you can then call c_str(), no?

gvdhoorn gravatar image gvdhoorn  ( 2018-06-01 03:40:50 -0600 )edit

That is one of the reasons why I explicitly mentioned my current lack of experience with C++, I have worked with C only until now. Does that mean that, if my custom messages only contain fields that ultimately "resolve" to basic types, I can access them like I would with a C structure?

Thank you!

jotator gravatar image jotator  ( 2018-06-01 03:46:59 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-06-01 03:45:31 -0600

gvdhoorn gravatar image

updated 2018-06-01 03:49:09 -0600

but I am new to C++

That may be something to fix first. However what you ask is not very difficult, and I would say actually identical between C and C++.

and looking at the ROS C++ tutorial I can't clearly see how to access the data fields in a complex message that is three or four fields deep.

The same way you would do it when traversing a C struct which has embedded structs: by using the dot operator to access individual structure members. Accessing members in nested structs just requires recursive use of the dot operator.


Edit:

Does that mean that, if my custom messages only contain fields that ultimately "resolve" to basic types, I can access them like I would with a C structure?

Yes, that's how this works.

It's just that with C++ those members could/would be actually objects, so they can have methods, which you can then directly invoke on those members.

edit flag offensive delete link more

Comments

Thank you very much, I am working on the C++ issue :)

jotator gravatar image jotator  ( 2018-06-01 03:49:29 -0600 )edit

Btw: C++ objects do not necessarily have to be PODs. You cannot assume anything about the memory layout, so if you're going to use shared memory, I'm not sure how robust / portable that is going to be.

gvdhoorn gravatar image gvdhoorn  ( 2018-06-01 03:51:21 -0600 )edit

You are right, I will be careful with that. That's not ROS, so I will investigate and bring the issue elsewhere. Again, thank you very much for the quick responses!

jotator gravatar image jotator  ( 2018-06-01 03:55:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-01 03:33:34 -0600

Seen: 2,167 times

Last updated: Jun 01 '18