LOEWE-emergenCITY / ros2_babel_fish

ROS2 communication introspection library to allow ROS2 communication with at compile time unknown message, action and service types.
MIT License
22 stars 8 forks source link

Getting CompoundArrayMessage datatype #5

Closed aaron-jca closed 3 months ago

aaron-jca commented 4 months ago

Some context; I'm currently writing a C++ MQTT bridge that converts ROS messages to json, sends them via MQTT, or receives a message via MQTT in json format, then converts it back to a ROS message and publishes it. Things were working relatively well, until I came to handling messages that have members that are CompoundArrayMessages, or arrays of ROS messages. My issue was in getting the ros datatype (eg. geometry_msgs/msg/Pose) of a CompoundArrayMessage.

The workaround was;

// Append an empty item to the compound array
          (*message)[item.key()].as<
              ros2_babel_fish::CompoundArrayMessage>().appendEmpty();
// access that CompoundMessage at index 0 and ask for the datatype
          std::string ros_datatype = (*message)[item.key()].as<
              ros2_babel_fish::CompoundArrayMessage>()[0].datatype();

Where item.key() is the json key at which the CompoundArrayMessage is stored, and message is a CompoundMessage.

I was then able to construct the CompoundMessage using that datatype and fill out the array using the json objects stored in that array.

While this workaround does work, ArrayMessageBase provides access to the MessageMemberIntrospection member via elementIntrospection. I believe the CompoundArrayMessage should override this and somehow provide access to MessageMembersIntrospection (note the 's' after members) or otherwise provide some wrapper that allows us to get the ros datatype via a function. Please let me know any way I can support this, I would love to be able to give back and support this library any way I can.

StefanFabian commented 3 months ago

Thank you for the report. This seems like a sensible addition. I have implemented an elementDatatype() and an elementName() method in CompoundArrayMessage_. Please let me know if you encounter any further issues.