Skip to content

Add Clock.sleep_until#858

Merged
sloretz merged 19 commits intomasterfrom
rclpy__sleep_until
Dec 15, 2021
Merged

Add Clock.sleep_until#858
sloretz merged 19 commits intomasterfrom
rclpy__sleep_until

Conversation

@sloretz
Copy link
Copy Markdown
Contributor

@sloretz sloretz commented Dec 9, 2021

Fixes #617

This works, and adds a method Clock.sleep_until() to sleep until a particular time on a clock is reached, respecting ROS time.

I chose to implement the time jump callbacks in Python because it's easier and reuses code, but it does mean the internal API Event::wait_until_ros() requires the caller to setup the required time jump callback or it will wait forever. Since this is an internal API, I think it's fine to document it and leave it as is. I couldn't think of a cleaner solution and user's won't be affected one way or the other.

Related to ros2/rclcpp#1730

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
@sloretz sloretz changed the title sleep_until works, but need to split other fixes out Add Clock.sleep_until Dec 9, 2021
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 9, 2021

PR job requires ros2/rcl#955 released to pass

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
@sloretz sloretz marked this pull request as ready for review December 13, 2021 20:09
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 13, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 13, 2021

CI (Re-run)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 13, 2021

CI Re-run

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@clalancette
Copy link
Copy Markdown
Contributor

PR job requires ros2/rcl#955 released to pass

I've merged ros/rosdistro#31477, which should allow the Rpr job to pass (once the downstream packages are done compiling. which will take a while).

Copy link
Copy Markdown
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

I chose to implement the time jump callbacks in Python because it's easier and reuses code

Just as a question: what is the alternative? To implement it on the C++ side?

Besides that, I've left some thoughts and comments inline. Nothing major, really, other than the possible problems of flaky tests.

@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 14, 2021

I chose to implement the time jump callbacks in Python because it's easier and reuses code

Just as a question: what is the alternative? To implement it on the C++ side?

Yeah, the alternative would be to implement it in C++ by calling rcl_clock_add_jump_callback() directly. The advantage is the internal rclpy::Event::wait_until_ros() API would encapsulate all the ROS time stuff; the disadvantage is Python is easier to write. Since it's an internal API I went with the easier option.

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
clock._set_ros_time_is_active(ros_time_enabled)

# wait for thread to exit
time.sleep(0.2)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we don't daemonize the thread, then we could do a join on the thread here instead, which at least gets rid of one sleep in the tests. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

join()ing the threads to avoid the second sleep in a8ddedf

@clalancette
Copy link
Copy Markdown
Contributor

Yeah, the alternative would be to implement it in C++ by calling rcl_clock_add_jump_callback() directly. The advantage is the internal rclpy::Event::wait_until_ros() API would encapsulate all the ROS time stuff; the disadvantage is Python is easier to write. Since it's an internal API I went with the easier option.

Sounds reasonable to me. I think the solution you've chosen is fine.

@clalancette
Copy link
Copy Markdown
Contributor

@ros-pull-request-builder retest this please

Copy link
Copy Markdown

@j-rivero j-rivero left a comment

Choose a reason for hiding this comment

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

I can not find any relevant issue or discrepancy in the code. Just a Nit about doxygen. I re-run CI trying to trigger the possible race condition in tests.

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

public:
/// Wait until a time specified by a system or steady clock.
/// \param clock the clock to use for time synchronization with until
template<typename ClockType>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Do we need to document the until parameter here and in the next method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Documenting the 'until' parameter in de582e1

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Copy link
Copy Markdown
Member

@ivanpauno ivanpauno left a comment

Choose a reason for hiding this comment

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

I have some minor comments, otherwise lgtm!

# See the License for the specific language governing permissions and
# limitations under the License.

import threading
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some of my comments in the tests cases of #864 also apply here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Of my comments in the other PR, I think that the only one relevant for the moment is using thread.join() instead of a sleep() call to wait the thread to exit.
The other comments can be ignored.

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Copy link
Copy Markdown
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

I agree with @ivanpauno's comment about renaming Event -> ClockEvent. I also have expressed concern about the possibility of flaky tests here, but I actually don't see a reasonable way to fix it given what this is trying to test. So once the rename is done, I'm happy to approve.

Signed-off-by: Shane Loretz <sloretz@openrobotics.org>
Copy link
Copy Markdown
Collaborator

@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

@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 15, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz merged commit 9b7e50b into master Dec 15, 2021
@delete-merged-branch delete-merged-branch bot deleted the rclpy__sleep_until branch December 15, 2021 21:00
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.

Add sleep function that respects ROS time

5 participants