Is it possible to define a custom array type in ROS2?
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
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.
Asked by nickcage on 2019-05-31 03:56:21 UTC
Answers
Yes it's possible.
Have a look at the ROS2 field types https://index.ros.org/doc/ros2/Concepts/About-ROS-Interfaces/#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 https://index.ros.org/doc/ros2/Tutorials/Defining-custom-interfaces-(msg-srv)/
Asked by alsora on 2019-05-31 04:40:43 UTC
Comments
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.
Asked by gvdhoorn on 2019-05-31 05:13:58 UTC
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.
Asked by allenh1 on 2019-05-31 15:03:13 UTC