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

Merged
merged 5 commits into from
Dec 7, 2024

Conversation

nicolaloi
Copy link
Contributor

@nicolaloi nicolaloi commented Nov 21, 2024

clock_publish_timer is now initialized with autostart = false, so it doesn't immediately start publishing the /clock topic. It is "activated" later with clock_publish_timer->reset() at the beginning of the playback thread, ensuring that the (optional) user-defined delay period has passed.

Additionally, the clock_ now starts in a paused state if a delay time is specified by the user. It resumes after the delay period has ended.

@nicolaloi nicolaloi requested a review from a team as a code owner November 21, 2024 23:12
@nicolaloi nicolaloi requested review from MichaelOrlov and hidmic and removed request for a team November 21, 2024 23:12
Copy link
Contributor

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm with green CI.

@MichaelOrlov could you have 2nd review?

Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@nicolaloi Thanks for the PR. Overall looks good among one undefined behavior / race introducing with the

            if (!play_options_.start_paused) {
              clock_->resume();
            }

in Play method.

rosbag2_transport/src/rosbag2_transport/player.cpp Outdated Show resolved Hide resolved
@nicolaloi
Copy link
Contributor Author

nicolaloi commented Nov 23, 2024

Ah yes, the clock will resume automatically at the next loop regardless of user requests.

Then to not overcomplicate things, I would suggest removing the "pause clock until delay is over" feature from this PR, and keeping the "publish clock after the delay is over" feature only, which is the actual fix for issue #1858. Pausing the clock is not required to fix this specific issue since anyway the clock_publish_timer_ was starting to publish after the clock was resumed.

I was pausing the clock as a comprehensive "inner" change for the Player class when in delayed status, but it is not required for the "outer" behavior of the timed published clock. If this "pause clock until delay is over" feature is desired, it can be a separate PR.

@nicolaloi nicolaloi changed the title publish clock after the delay is over and pause clock until delay is over publish clock after the delay is over Nov 23, 2024
@nicolaloi nicolaloi force-pushed the nicolaloi/fix-clock-delay branch from 5576c77 to 0323e46 Compare November 23, 2024 11:41
Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
@nicolaloi nicolaloi force-pushed the nicolaloi/fix-clock-delay branch from 0323e46 to 5f7da84 Compare November 23, 2024 12:49
@nicolaloi
Copy link
Contributor Author

I was pausing the clock as a comprehensive "inner" change for the Player class when in delayed status, but it is not required for the "outer" behavior of the timed published clock. If this "pause clock until delay is over" feature is desired, it can be a separate PR.

Removing

if (!play_options_.start_paused) {
       clock_->resume();
}

to keep the "pause clock until delay is over" feature only, but if it is not okay I can reintegrate and try to fix this "pause clock until delay is over" feature in this PR too.

Extra change: now before sleeping for the delay period, the clock_publish_timer_->cancel() is called. This does not affect the first standard loop (since clock_publish_timer_ has autostart=false), however for the subsequent (optional) loops this prevents the clock from being published until the player exits again the subsequent delayed periods (to be consistent with the first delay period).

@fujitatomoya
Copy link
Contributor

keeping the "publish clock after the delay is over" feature only, which is the actual fix for issue #1858

i prefer this approach, one thing at a time. and easier to backport to downstream distros.

and the question here is that clock needs to be paused in delay duration? i am not quite sure about what state delay is...
in the implementation, it is playing state in PlayerImpl::play during duration period, but we cannot pause the sleep duration at all.

Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@nicolaloi Thanks for addressing the comments from the review.
It looks good to me in the current form. However, I would move the delay out of the playback loop. IMO, there is no value in making a delay in the loop after we have finished the playback round.
Delay needs for the purporse to be able to wait for other nodes (subscribers and other dependend nodes in the pipeline) to start and be discoverable. When we have run the first playback round all other nodes still up un running. Thereofre, there is no need to make a delay on a consequent rounds of replay.

@nicolaloi
Copy link
Contributor Author

nicolaloi commented Nov 24, 2024

Okay, I will incorporate the change to the delay loop behavior in this PR.

Need to invoke clock_->jump(starting_time_) before the loop, before starting the clock_publish_timer, but I am not sure if locking the mutex during the call is needed also in this pre-loop case.

@nicolaloi nicolaloi changed the title publish clock after the delay is over publish clock after delay is over and disable delay on next loops Nov 24, 2024
Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
@nicolaloi nicolaloi force-pushed the nicolaloi/fix-clock-delay branch from 64bd7a4 to cecd120 Compare November 24, 2024 23:14
Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@nicolaloi Thanks for the updates.
I think now it would be safe to init clock in pause mode in constructor and resume it after the delay. i.e.

 clock_ = std::make_unique<rosbag2_cpp::TimeControllerClock>(
      starting_time_, std::chrono::steady_clock::now,
      std::chrono::milliseconds{100},
      play_options_.start_paused || play_options_.delay > rclcpp::Duration(0, 0));

Also may I ask you in future do a separate commit after a round of review instead of push force the branch with the the same changed commit?
We are squashing all commits before merging to the target branch after review anyway automatically. However, the separate commits allow to keep history of changes during review.

rosbag2_transport/src/rosbag2_transport/player.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@nicolaloi Could you please also rebase your branch on top of the latest rolling branch?
We made some changes. Now we support multiple readers.

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@nicolaloi Thanks for the PR and for addressing the review comments. LGTM now.

@MichaelOrlov MichaelOrlov changed the title publish clock after delay is over and disable delay on next loops Publish clock after delay is over and disable delay on next loops Dec 6, 2024
@MichaelOrlov
Copy link
Contributor

Pulls: #1861
Gist: https://gist.githubusercontent.com/MichaelOrlov/b0a96eb6a82f324d25e2f267ed68985d/raw/a84f0c1d7aca1838d0241fbdfc44c78d16a91c18/ros2.repos
BUILD args: --packages-above-and-dependencies ros2bag rosbag2_transport
TEST args: --packages-above ros2bag rosbag2_transport
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/14914

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@MichaelOrlov
Copy link
Contributor

Re-run Windows CI job since previous failure is unrelated

ERROR: Failed building wheel for PyQt5-sip
  • Windows Build Status

@MichaelOrlov MichaelOrlov merged commit 15b8260 into ros2:rolling Dec 7, 2024
12 checks passed
@MichaelOrlov
Copy link
Contributor

https://github.com/Mergifyio backport jazzy

Copy link

mergify bot commented Dec 7, 2024

backport jazzy

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 7, 2024
)

* publish clock after the delay is over

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* Disable delay period in subsequent loops (ros2 bag play)

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* Reset clock publisher timer outisde playback loop

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* review

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

---------

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
(cherry picked from commit 15b8260)
fujitatomoya pushed a commit that referenced this pull request Dec 8, 2024
) (#1878)

* publish clock after the delay is over

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* Disable delay period in subsequent loops (ros2 bag play)

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* Reset clock publisher timer outisde playback loop

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

* review

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>

---------

Signed-off-by: Nicola Loi <nicolaloi@outlook.com>
(cherry picked from commit 15b8260)

Co-authored-by: Nicola Loi <nicolaloi@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rosbag2 play --clock option ignores delay
3 participants