2022-01-31 13:35:42 +01:00
|
|
|
"""Example: Edit timestamps."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2022-04-13 09:40:22 +02:00
|
|
|
from typing import TYPE_CHECKING, cast
|
2022-01-31 13:35:42 +01:00
|
|
|
|
2022-04-13 09:40:22 +02:00
|
|
|
from rosbags.interfaces import ConnectionExtRosbag2
|
2022-01-31 13:35:42 +01:00
|
|
|
from rosbags.rosbag2 import Reader, Writer
|
|
|
|
|
from rosbags.serde import deserialize_cdr, serialize_cdr
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def offset_timestamps(src: Path, dst: Path, offset: int) -> None:
|
|
|
|
|
"""Offset timestamps.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
src: Source path.
|
|
|
|
|
dst: Destination path.
|
|
|
|
|
offset: Amount of nanoseconds to offset timestamps.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
with Reader(src) as reader, Writer(dst) as writer:
|
|
|
|
|
conn_map = {}
|
2022-04-21 15:15:10 +02:00
|
|
|
for conn in reader.connections:
|
2022-04-13 09:40:22 +02:00
|
|
|
ext = cast(ConnectionExtRosbag2, conn.ext)
|
2022-01-31 13:35:42 +01:00
|
|
|
conn_map[conn.id] = writer.add_connection(
|
|
|
|
|
conn.topic,
|
|
|
|
|
conn.msgtype,
|
2022-04-13 09:40:22 +02:00
|
|
|
ext.serialization_format,
|
|
|
|
|
ext.offered_qos_profiles,
|
2022-01-31 13:35:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for conn, timestamp, data in reader.messages():
|
|
|
|
|
# Adjust header timestamps, too
|
|
|
|
|
msg = deserialize_cdr(data, conn.msgtype)
|
|
|
|
|
if head := getattr(msg, 'header', None):
|
|
|
|
|
headstamp = head.stamp.sec * 10**9 + head.stamp.nanosec + offset
|
|
|
|
|
head.stamp.sec = headstamp // 10**9
|
|
|
|
|
head.stamp.nanosec = headstamp % 10**9
|
|
|
|
|
data = serialize_cdr(msg, conn.msgtype)
|
|
|
|
|
|
|
|
|
|
writer.write(conn_map[conn.id], timestamp + offset, data)
|