Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish clock after delay is over and disable delay on next loops #1861

Open
wants to merge 3 commits into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ros2bag/ros2bag/verb/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def add_arguments(self, parser, cli_name): # noqa: D102
)
parser.add_argument(
'-d', '--delay', type=positive_float, default=0.0,
help='Sleep duration before play (each loop), in seconds. Negative durations invalid.')
help='Sleep duration before play (loops are not affected), in seconds.'
'Negative durations invalid.')
parser.add_argument(
'--playback-duration', type=float, default=-1.0,
help='Playback duration, in seconds. Negative durations mark an infinite playback. '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct PlayOptions
// a /clock update to be published. If list is empty, all topics will act as a trigger
std::vector<std::string> clock_trigger_topics = {};

// Sleep before play. Negative durations invalid. Will delay at the beginning of each loop.
// Sleep before play. Negative durations invalid. Loops are not affected.
rclcpp::Duration delay = rclcpp::Duration(0, 0);

// Determines the maximum duration of the playback. Negative durations will make the playback to
Expand Down
19 changes: 13 additions & 6 deletions rosbag2_transport/src/rosbag2_transport/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,19 @@ bool PlayerImpl::play()
playback_thread_ = std::thread(
[&, delay]() {
try {
if (delay > rclcpp::Duration(0, 0)) {
RCLCPP_INFO_STREAM(owner_->get_logger(), "Sleep " << delay.nanoseconds() << " ns");
std::chrono::nanoseconds delay_duration(delay.nanoseconds());
std::this_thread::sleep_for(delay_duration);
}
{
std::lock_guard<std::mutex> lk(reader_mutex_);
clock_->jump(starting_time_);
}
Comment on lines +491 to +494
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have already do clock_->jump(..) just a few lines down. On line 502.
This one is redundant and should be removed IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the jump before the loop because in this commit it needs to be called before starting the clock_publisher_timer to avoid publishing a non-monotonic clock. But yes if I revert to pausing the clock until the delay is over, then it is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelOrlov I think there could be issues with pausing the clock and resuming it after the delay. If the user commands a direct "pause" of the clock (via keyboard control or service) during the delay period, the clock will resume automatically after the delay regardless of the user's intent (#1861 (comment)). Another linked issue is if I remove the clock_->jump call before starting clock_publisher_timer, and the user resumes the clock during the delay period; then when the delay ends and the clock_publisher_timer starts, the clock publisher could publish "future" timestamps before going back to the starting_time_ timestamp with the clock_->jump call inside the loop.

if (clock_publish_timer_ != nullptr) {
clock_publish_timer_->reset();
}
do {
if (delay > rclcpp::Duration(0, 0)) {
RCLCPP_INFO_STREAM(owner_->get_logger(), "Sleep " << delay.nanoseconds() << " ns");
std::chrono::nanoseconds delay_duration(delay.nanoseconds());
std::this_thread::sleep_for(delay_duration);
}
{
std::lock_guard<std::mutex> lk(reader_mutex_);
reader_->seek(starting_time_);
Expand Down Expand Up @@ -1072,7 +1079,7 @@ void PlayerImpl::prepare_publishers()
clock_publish_timer_ = owner_->create_wall_timer(
publish_period, [this]() {
publish_clock_update();
});
}, nullptr, false);
}

if (play_options_.clock_publish_on_topic_publish) {
Expand Down
Loading