0
$\begingroup$

Rosanswers logo

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!


Originally posted by jotator on ROS Answers with karma: 60 on 2018-06-01

Post score: 0


Original comments

Comment by gvdhoorn on 2018-06-01:
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?

Comment by jotator on 2018-06-01:
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!

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2018-06-01

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by jotator on 2018-06-01:
Thank you very much, I am working on the C++ issue :)

Comment by gvdhoorn on 2018-06-01:
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.

Comment by jotator on 2018-06-01:
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!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.