Use custom class objects as ros2 msg type
I'm working with ros2 humble in python, trying to implement two nodes say Node1 & Node2. Node2 is subscribed to Node1. My requirement is for Node1 to publish a box object (containing a nested dictionary structure) to Node2. Since box is not a supported format I'm currently converting the box object to string and publishing, which beats the purpose of box. I would like to create a custom message type/interface to send the box object as such. How can I achieve this? Thanks in advance
Asked by benpbabu on 2023-06-14 07:23:35 UTC
Answers
What you want is a custom message interface. This requires building an interface package and defining your custom .msg file. It is not recommended to convert complex datatypes into strings end then decode.
The custom interfaces are explained in this tutorial https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Custom-ROS2-Interfaces.html
You could store one single dictionary key value pair as a separate custom message like so
# DictEntry.msg
string key
string value
And then make your Box datatype holding a list of these DictEntrys
# BoxDict
DictEntry[] data
Asked by Andreas Z on 2023-06-14 08:48:31 UTC
Comments