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

Is it possible to define a custom array type in ROS2?

asked 2019-05-31 03:56:21 -0500

nickcage gravatar image

Hello everyone,

here's my situation: I have a buffer in RAM that is shared between two applications, first is a C program that collects frames and metadata from a camera, fills the buffer with that data and signals the other application, which is a ROS2 node, to grab that data. The ROS2 node is then supposed to pack the data into a sensor_msgs/Image message and publish it so it can be recorded with rosbag record.

Image message has a 'uint8[] data' field which translates into 'vector<uint8> data' on ROS2. The problem is that our whole system is time critical and we cannot afford to copy data from the mentioned buffer in RAM to that vector and there is no mechanism to make a C++ vector's data field point to some fixed address where data is.

So, the question is whether it is possible to create a custom array type for ROS2 messages or not. I would like it to be a regular array so I can just point it to where the data is to avoid time consuming copy operation. If there is some other mechanism to achieve this, info on that would be much appreciated.

Thank you in advance.

edit retag flag offensive close merge delete


there is no mechanism to make a C++ vector's data field point to some fixed address where data is.

I believe there is, it's called placement new. Whether that is usable here is something else.

Perhaps in combination with a custom allocator you could force the ROS msg to be placed in a buffer you've already allocated.

How that'd interact with the serialisation phase in the ROS 2 publication pipeline I wouldn't know though.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-31 05:13:58 -0500 )edit

Have you considered making the two nodes run on separate threads, then just communicating between them directly? Otherwise, I think that a custom allocator is a good candidate.

allenh1 gravatar image allenh1  ( 2019-05-31 15:03:13 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-05-31 04:40:43 -0500

alsora gravatar image

updated 2019-05-31 04:42:28 -0500

Yes it's possible.

Have a look at the ROS2 field types As you can see, ROS2 provides a static array datatype that converts to std::array<T,N> in C++.

What you need to do is to define a custom message, such as a ImageStatic.msg

The fields of this message can be the same of the standard Image.msg However, you will change the data field to be a static array.

For example, if your images are monochrome with a resolution of 320 x 240

uint8[76800] data

Where 76800 is the size of your array (320 x 240 = 76800)

Here you find a tutorial about creating custom interfaces

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-05-31 03:56:21 -0500

Seen: 4,085 times

Last updated: May 31 '19