rclcpp
rclcpp copied to clipboard
Feature Request: Dynamic Type Handling for Generic Subscriptions
Feature request
Feature description
I am developing an application where I need to subscribe to topics with unknown message types (similar to rosbag2). In the subscription callback, I need access to the deserialized message and its fields without knowing the type in advance.
There is a related example here, but it requires specifying the type (T2
) beforehand. I am looking for a way to use only the topic type
as a string to dynamically obtain the message class.
Using snippets here and there from rclcpp I put together this incomplete example:
node->create_generic_subscription(
topic_name,
topic_type,
qos,
[this, topic_name, topic_type](std::shared_ptr<const rclcpp::SerializedMessage> message,
const rclcpp::MessageInfo & mi) {
void* deserialized_msg = nullptr;
auto ts_lib = rclcpp::get_typesupport_library(topic_type, "rosidl_typesupport_cpp");
auto type_support = rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib);
rmw_deserialize(&message->get_rcl_serialized_message(), type_support, deserialized_msg);
});
The issue is that deserialized_msg is a null pointer ('void *'), and I am unsure how to proceed from here.
If there is currently no way to achieve this, please consider this a feature request. I am open to contributing with some guidance on the implementation. Thanks!
Related:
https://answers.ros.org/question/405796/deserialize-message-with-topic-name-ros2/ https://github.com/ros2/rclcpp/issues/2260 https://github.com/ros2/ros2/issues/1374 https://github.com/facontidavide/rosx_introspection