From aa698aeac12eaa708ad4fad1a53a2e8eab9a7789 Mon Sep 17 00:00:00 2001 From: Geoffrey Biggs Date: Tue, 15 Feb 2022 21:45:55 +0900 Subject: [PATCH] Add play-for to the CLI Signed-off-by: Geoffrey Biggs --- ros2bag/ros2bag/verb/play.py | 10 +++++++++- rosbag2_py/src/rosbag2_py/_transport.cpp | 9 ++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ros2bag/ros2bag/verb/play.py b/ros2bag/ros2bag/verb/play.py index d82f1a2a7b..9f8a4f1791 100644 --- a/ros2bag/ros2bag/verb/play.py +++ b/ros2bag/ros2bag/verb/play.py @@ -105,6 +105,10 @@ def add_arguments(self, parser, cli_name): # noqa: D102 "Note that this option is valid only if the publisher\'s QOS profile is " 'RELIABLE.', metavar='TIMEOUT') + parser.add_argument( + '-f', '--duration', type=float, default=None, + help='Play for SEC seconds. Default is None, meaning that playback will continue ' + 'until the end of the bag. Valid range > 0.0') def main(self, *, args): # noqa: D102 qos_profile_overrides = {} # Specify a valid default @@ -145,5 +149,9 @@ def main(self, *, args): # noqa: D102 play_options.start_offset = args.start_offset play_options.wait_acked_timeout = args.wait_for_all_acked + # Gets the duration in nanoseconds when a value is provided for player + # consumption. + duration = int(args.duration * 1e9) if args.duration else args.duration + player = Player() - player.play(storage_options, play_options) + player.play(storage_options, play_options, duration) diff --git a/rosbag2_py/src/rosbag2_py/_transport.cpp b/rosbag2_py/src/rosbag2_py/_transport.cpp index c11ca23929..a6e45e3af8 100644 --- a/rosbag2_py/src/rosbag2_py/_transport.cpp +++ b/rosbag2_py/src/rosbag2_py/_transport.cpp @@ -120,7 +120,8 @@ class Player void play( const rosbag2_storage::StorageOptions & storage_options, - PlayOptions & play_options) + PlayOptions & play_options, + const std::optional & duration) { auto reader = rosbag2_transport::ReaderWriterFactory::make_reader(storage_options); auto player = std::make_shared( @@ -132,7 +133,7 @@ class Player [&exec]() { exec.spin(); }); - player->play(); + player->play(duration); exec.cancel(); spin_thread.join(); @@ -275,7 +276,9 @@ PYBIND11_MODULE(_transport, m) { py::class_(m, "Player") .def(py::init()) - .def("play", &rosbag2_py::Player::play) + .def( + "play", &rosbag2_py::Player::play, py::arg("storage_options"), py::arg( + "play_options"), py::arg("duration") = std::nullopt) ; py::class_(m, "Recorder")