@@ -630,12 +630,25 @@ def _prepare_bag_paths(self, file_path: str):
630630 fused_path .parent .mkdir (parents = True , exist_ok = True )
631631 return fused_path , raw_path
632632
633+ def _make_topic_metadata (self , topic : str ) -> rosbag2_py .TopicMetadata :
634+ """Build TopicMetadata compatible with both legacy and new rosbag2 Python signatures."""
635+ msg_type = 'grid_map_msgs/msg/GridMap'
636+ serialization_format = 'cdr'
637+ try :
638+ metadata = rosbag2_py .TopicMetadata (topic , msg_type , serialization_format , '' )
639+ if metadata .name == topic and metadata .type == msg_type :
640+ return metadata
641+ except TypeError :
642+ # Older rosbag2 versions expect the reader-count placeholder as the first argument
643+ pass
644+ return rosbag2_py .TopicMetadata (0 , topic , msg_type , serialization_format )
645+
633646 def _write_grid_map_bag (self , path : Path , topic : str , grid_map_msg : GridMap ) -> None :
634647 writer = rosbag2_py .SequentialWriter ()
635648 storage_options = rosbag2_py .StorageOptions (uri = str (path ), storage_id = self .save_map_storage_id )
636649 converter_options = rosbag2_py .ConverterOptions ('' , '' )
637650 writer .open (storage_options , converter_options )
638- topic_metadata = rosbag2_py . TopicMetadata ( 0 , topic , 'grid_map_msgs/msg/GridMap' , 'cdr' )
651+ topic_metadata = self . _make_topic_metadata ( topic )
639652 writer .create_topic (topic_metadata )
640653 writer .write (topic , serialize_message (grid_map_msg ), self .get_clock ().now ().nanoseconds )
641654
0 commit comments