Skip to content

Commit

Permalink
Add play-for to the CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Biggs <gbiggs@killbots.net>
  • Loading branch information
gbiggs committed Mar 15, 2022
1 parent 757f1c2 commit aa698ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 9 additions & 1 deletion ros2bag/ros2bag/verb/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
9 changes: 6 additions & 3 deletions rosbag2_py/src/rosbag2_py/_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class Player

void play(
const rosbag2_storage::StorageOptions & storage_options,
PlayOptions & play_options)
PlayOptions & play_options,
const std::optional<rcutils_duration_value_t> & duration)
{
auto reader = rosbag2_transport::ReaderWriterFactory::make_reader(storage_options);
auto player = std::make_shared<rosbag2_transport::Player>(
Expand All @@ -132,7 +133,7 @@ class Player
[&exec]() {
exec.spin();
});
player->play();
player->play(duration);

exec.cancel();
spin_thread.join();
Expand Down Expand Up @@ -275,7 +276,9 @@ PYBIND11_MODULE(_transport, m) {

py::class_<rosbag2_py::Player>(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_<rosbag2_py::Recorder>(m, "Recorder")
Expand Down

0 comments on commit aa698ae

Please sign in to comment.