rosbags.rosbag2
Rosbags support for rosbag2 files.
Readers and writers provide access to metadata and raw message content saved in the rosbag2 format.
- class rosbags.rosbag2.Reader(path)
Bases:
object
Reader for rosbag2 files.
It implements all necessary features to access metadata and message streams.
Version history:
Version 1: Initial format.
Version 2: Changed field sizes in C++ implementation.
Version 3: Added compression.
Version 4: Added QoS metadata to topics, changed relative file paths.
Version 5: Added per file metadata.
Version 6: Added custom_data dict to metadata.
Version 7: Added type_description_hash to topic metadata.
Version 8: Added ros_distro to metadata.
Version 9: Changed QoS metadata serialization and enums.
- Parameters:
path (Path | str)
- close()
Close rosbag2.
- Return type:
None
- property compression_format: str | None
Compression format.
- property compression_mode: str | None
Compression mode.
- property duration: int
Duration in nanoseconds between earliest and latest messages.
- property end_time: int
Timestamp in nanoseconds after the latest message.
- property message_count: int
Total message count.
- messages(connections=(), start=None, stop=None)
Read messages from bag.
- Parameters:
connections (
Iterable
[Connection
]) – Iterable with connections to filter for. An empty iterable disables filtering on connections.start (
int
|None
) – Yield only messages at or after this timestamp (ns).stop (
int
|None
) – Yield only messages before this timestamp (ns).
- Yields:
tuples of connection, timestamp (ns), and rawdata.
- Raises:
ReaderError – If reader was not opened.
- Return type:
Generator
[tuple
[Connection
,int
,bytes
],None
,None
]
- open()
Open rosbag2.
- Return type:
None
- property ros_distro: str | None
ROS distribution.
- property start_time: int
Timestamp in nanoseconds of the earliest message.
- property topics: dict[str, TopicInfo]
Topic information.
- exception rosbags.rosbag2.ReaderError
Bases:
Exception
Reader Error.
- class rosbags.rosbag2.Writer(path, *, version=None)
Bases:
object
Rosbag2 writer.
This class implements writing of rosbag2 files in version 8. It should be used as a contextmanager.
- Parameters:
path (Path | str)
version (Literal[8, 9] | None)
- class CompressionFormat(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
IntEnum
Compession formats.
- class CompressionMode(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
IntEnum
Compession modes.
- add_connection(topic, msgtype, *, typestore=None, msgdef=None, rihs01=None, serialization_format='cdr', offered_qos_profiles=())
Add a connection.
This function can only be called after opening a bag.
- Parameters:
topic (
str
) – Topic name.msgtype (
str
) – Message type.typestore (
Typestore
|None
) – Typestore.msgdef (
str
|None
) – Message definiton.rihs01 (
str
|None
) – Message hash.serialization_format (
str
) – Serialization format.offered_qos_profiles (
Sequence
[Qos
] |str
) – QOS Profile.
- Return type:
Connection
- Returns:
Connection object.
- Raises:
WriterError – Bag not open or topic previously registered.
- close()
Close rosbag2 after writing.
Closes open database transactions and writes metadata.yaml.
- Return type:
None
- open()
Open rosbag2 for writing.
Create base directory and open database connection.
- Return type:
None
- set_compression(mode, fmt)
Enable compression on bag.
This function has to be called before opening.
- Parameters:
mode (
CompressionMode
) – Compression mode to use, either ‘file’ or ‘message’.fmt (
CompressionFormat
) – Compressor to use, currently only ‘zstd’.
- Raises:
WriterError – Bag already open.
- Return type:
None
- set_custom_data(key, value)
Set key value pair in custom_data.
- Parameters:
key (
str
) – Key to set.value (
str
) – Value to set.
- Raises:
WriterError – If value has incorrect type.
- Return type:
None
- write(connection, timestamp, data)
Write message to rosbag2.
- Parameters:
connection (
Connection
) – Connection to write message to.timestamp (
int
) – Message timestamp (ns).data (
bytes
|memoryview
) – Serialized message data.
- Raises:
WriterError – Bag not open or topic not registered.
- Return type:
None
- exception rosbags.rosbag2.WriterError
Bases:
Exception
Writer Error.