ros2/rosbag2

Failed to seek rosbag2

atinfinity opened this issue · 3 comments

Description

I tried to use seek API of rosbag2_py.
But, I could not seek rosbag2.

import rosbag2_py

reader = rosbag2_py.SequentialReader()
input_bag = "rosbag2/rosbag2_py/test/resources/talker/talker.db3"
reader.open(
    rosbag2_py.StorageOptions(uri=input_bag, storage_id="sqlite3"),
    rosbag2_py.ConverterOptions(
        input_serialization_format="cdr", output_serialization_format="cdr"
    ),
)

reader.seek(10 ** 9)
topic1, data1, timestamp1 = reader.read_next()
print(f"timestamp1 = {timestamp1}")

reader.seek(0)
topic2, data2, timestamp2 = reader.read_next()
print(f"timestamp2 = {timestamp2}")

Expected Behavior

reader.seek(10 ** 9)
topic1, data1, timestamp1 = reader.read_next()
print(f"timestamp1 = {timestamp1}")

reader.seek(0)
topic2, data2, timestamp2 = reader.read_next()
print(f"timestamp2 = {timestamp2}")

I expected that timestamp1 and timestamp2 are different value.

Actual Behavior

reader.seek(10 ** 9)
topic1, data1, timestamp1 = reader.read_next()
print(f"timestamp1 = {timestamp1}")

reader.seek(0)
topic2, data2, timestamp2 = reader.read_next()
print(f"timestamp2 = {timestamp2}")

But timestamp1 and timestamp2 is same value.

To Reproduce

sudo apt install ros-humble-rosbag2-py

And, I run the following script.

import rosbag2_py

reader = rosbag2_py.SequentialReader()
input_bag = "rosbag2/rosbag2_py/test/resources/talker/talker.db3"
reader.open(
    rosbag2_py.StorageOptions(uri=input_bag, storage_id="sqlite3"),
    rosbag2_py.ConverterOptions(
        input_serialization_format="cdr", output_serialization_format="cdr"
    ),
)

reader.seek(10 ** 9)
topic, data, timestamp = reader.read_next()
print(f"timestamp = {timestamp}")

reader.seek(0)
topic, data, timestamp = reader.read_next()
print(f"timestamp = {timestamp}")

I used rosbag2_py/test/resources/talker/talker.db3 for this check.

System (please complete the following information)

  • OS: Ubuntu 22.04
  • ROS 2 Distro: ROS 2 Humble
  • Install Method: APT
  • Version: release

Additional context

I checked rosbag2/rosbag2_py/test/resources/talker/talker.db3.

$ ros2 bag info rosbag2/rosbag2_py/test/resources/talker/talker.db3
[INFO] [1708947745.825881625] [rosbag2_storage]: Opened database 'rosbag2/rosbag2_py/test/resources/talker/talker.db3' for READ_ONLY.

Files:             rosbag2/rosbag2_py/test/resources/talker/talker.db3
Bag size:          16.0 KiB
Storage id:        sqlite3
Duration:          4.531s
Start:             Apr  2 2020 22:23:55.112 (1585866235.112)
End:               Apr  2 2020 22:23:59.643 (1585866239.643)
Messages:          20
Topic information: Topic: /rosout | Type: rcl_interfaces/msg/Log | Count: 10 | Serialization Format: cdr
                   Topic: /topic | Type: std_msgs/msg/String | Count: 10 | Serialization Format: cdr

I also checked on ROS 2 Iron and got the same result.

As you confirmed the start/end times of this bag file using the "ros2 bag info" command, it is approximately 4.5 seconds records from 1585866235.1 [sec] to 1585866239.6 [sec]. Since the seek function accepts timestamps in nanosecond units, if you set start_timestamp_sec = 1585866236, you should get results as if seeking up to that point.

import rosbag2_py

reader = rosbag2_py.SequentialReader()
input_bag = "rosbag2/rosbag2_py/test/resources/talker/talker.db3"
reader.open(
    rosbag2_py.StorageOptions(uri=input_bag, storage_id="sqlite3"),
    rosbag2_py.ConverterOptions(
        input_serialization_format="cdr", output_serialization_format="cdr"
    ),
)
start_timestamp_sec = 1585866236
reader.seek(start_timestamp_sec * 10 ** 9)
topic1, data1, timestamp1 = reader.read_next()
print(f"timestamp1 = {timestamp1}")

reader.seek(0)
topic2, data2, timestamp2 = reader.read_next()
print(f"timestamp2 = {timestamp2}")

output

timestamp1 = 1585866236112742168
timestamp2 = 1585866235112411371

@dskkato Thank you for your comment. I understood.

start_timestamp_sec = 1585866236

I found that this value could get from metadata.

metadata = rosbag2_py.Info().read_metadata(input_bag, "")
print(f"starting_time = {metadata.starting_time.timestamp()} sec.")

output

starting_time = 1585866235.112411 sec.

@dskkato Thanks for your support on this issue.

  • Closing this issue as it is not a bug and behavior consistent with design and API spec.