Created
February 16, 2023 18:31
-
-
Save PeterMitrano/80c71673115a0cf602afdf7a797c195d to your computer and use it in GitHub Desktop.
One way to read a bagfile from python in ros2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_bagfile(bagfile: Path): | |
reader = rosbag2_py.SequentialReader() | |
storage_options = rosbag2_py.StorageOptions(bagfile.as_posix()) | |
converter_options = rosbag2_py.ConverterOptions("cdr", "cdr") | |
reader.open(storage_options, converter_options) | |
topic_types = reader.get_all_topics_and_types() | |
# Create a map for quicker lookup | |
type_map = { | |
topic_types[i].name: topic_types[i].type for i in range(len(topic_types)) | |
} | |
return reader, type_map | |
def get_first_message_on_topic(reader, type_map, topic): | |
storage_filter = rosbag2_py.StorageFilter(topics=[topic]) | |
reader.set_filter(storage_filter) | |
while reader.has_next(): | |
(topic, data, _) = reader.read_next() | |
msg_type = get_message(type_map[topic]) | |
msg = deserialize_message(data, msg_type) | |
return msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment