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

Add Clock.sleep_until #858

Merged
merged 19 commits into from
Dec 15, 2021
Merged

Add Clock.sleep_until #858

merged 19 commits into from
Dec 15, 2021

Conversation

sloretz
Copy link
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
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
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
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
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
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
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.

rclpy/src/rclpy/event.hpp Outdated Show resolved Hide resolved
rclpy/src/rclpy/event.cpp Outdated Show resolved Hide resolved
rclpy/src/rclpy/event.cpp Outdated Show resolved Hide resolved
rclpy/src/rclpy/event.cpp Outdated Show resolved Hide resolved
rclpy/test/test_clock.py Show resolved Hide resolved
rclpy/test/test_clock.py Outdated Show resolved Hide resolved
rclpy/test/test_clock.py Show resolved Hide resolved
@sloretz
Copy link
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
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
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
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
Contributor

@ros-pull-request-builder retest this please

Copy link

@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>

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
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
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!

rclpy/rclpy/clock.py Show resolved Hide resolved
rclpy/src/rclpy/event.cpp Outdated Show resolved Hide resolved
@@ -12,18 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import threading
Copy link
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
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
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
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
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