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

Implement validity checks for rclcpp::Clock #2040

Merged
merged 10 commits into from
Dec 20, 2022

Conversation

methylDragon
Copy link
Contributor

@methylDragon methylDragon commented Nov 1, 2022

This PR implements the analogues of the isValid and waitForValid methods in rclcpp.

I also added the necessary tests.

Note: In this case, I added a special case for treating RCL_CLOCK_UNINITIALIZED as invalid as well, since there isn't an analogous clock type in the ROS 1 API.

Pinging @sloretz for visibility.

Edit: Waiting on:

@methylDragon
Copy link
Contributor Author

methylDragon commented Nov 1, 2022

Latest build

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

rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/include/rclcpp/clock.hpp Outdated Show resolved Hide resolved
rclcpp/test/rclcpp/test_time.cpp Outdated Show resolved Hide resolved
@methylDragon
Copy link
Contributor Author

Changes incorporated!

rclcpp/include/rclcpp/clock.hpp Outdated Show resolved Hide resolved
rclcpp/test/rclcpp/test_time.cpp Outdated Show resolved Hide resolved
@methylDragon methylDragon force-pushed the ch3/clock-wait-for-valid branch 2 times, most recently from 937c213 to 23271a6 Compare November 2, 2022 00:28
Copy link
Contributor

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

@methylDragon methylDragon force-pushed the ch3/clock-wait-for-valid branch 2 times, most recently from eb680b4 to 6a54627 Compare November 2, 2022 03:49
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.

@methylDragon thanks for the contribution.

Could you explain the actual problem that you meet to address with this PR? I am not really following the what needs to be done here, thanks in advance!

rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/include/rclcpp/clock.hpp Outdated Show resolved Hide resolved
rclcpp/include/rclcpp/clock.hpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
@methylDragon
Copy link
Contributor Author

methylDragon commented Nov 2, 2022

@methylDragon thanks for the contribution.

Could you explain the actual problem that you meet to address with this PR? I am not really following the what needs to be done here, thanks in advance!

Hey Tomoya (@fujitatomoya), thanks for the reviews (and the exception docs catches)! This PR is supposed to add rclcpp analogues of the isValid and waitForValid methods (in ROS 1 time), used for checking if a clock's time is "valid"/"initialized" and so codifying that notion (from the design docs.)

I'm upstreaming this change because it came up in one of my porting efforts.

Notably, this notion of validity is separate from the rcl notion, since it also includes the fact that time 0 is also invalid (that is, it applies to the time reflected by the clock.)

@methylDragon
Copy link
Contributor Author

@ros-pull-request-builder retest this please

@fujitatomoya
Copy link
Collaborator

@methylDragon thank you for iterating with me.

If time has not been set it will return zero if nothing has been received. A time value of zero should be considered an error meaning that time is uninitialized.

https://design.ros2.org/articles/clock_and_time.html, says above. thanks for pointing out the design.

Notably, this notion of validity is separate from the rcl notion, since it also includes the fact that time 0 is also invalid (that is, it applies to the time reflected by the clock.)

this becomes my question that why these notions are different? by design, rcl_clock_valid should return false if the time is zero? and then we can call rcl_clock_valid from rclcpp and rclpy?

@clalancette @ivanpauno @wjwwood @sloretz what do you think?

@clalancette
Copy link
Contributor

this becomes my question that why these notions are different?

Yes, this is a good question.

It turns out that ROS has been considering time==0 as invalid since practically forever. It is not ideal, since time==0 is actually valid; it is just the start of time, not invalid time. If we were totally redesigning this today, I think we might designate something like -1 as invalid, with 0 just being the start of time. But if we did that now, we'd risk subtly breaking a lot of simulation applications that made some assumptions about past behavior. So while it is not ideal, I think we should continue to treat time==0 as invalid.

by design, rcl_clock_valid should return false if the time is zero? and then we can call rcl_clock_valid from rclcpp and rclpy?

But this is a good question; it would be nice if this policy were encoded in the rcl layer, and then just inherited by the client libraries. @methylDragon Do you think that is feasible?

rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
@fujitatomoya
Copy link
Collaborator

It turns out that ROS has been considering time==0 as invalid since practically forever. It is not ideal, since time==0 is actually valid; it is just the start of time, not invalid time. If we were totally redesigning this today, I think we might designate something like -1 as invalid, with 0 just being the start of time.

agree on this. I was wondering the same thing, thanks for pointing this out.

@methylDragon
Copy link
Contributor Author

methylDragon commented Nov 3, 2022

@clalancette

But this is a good question; it would be nice if this policy were encoded in the rcl layer, and then just inherited by the client libraries. @methylDragon Do you think that is feasible?

It'll be nice to put this in the rcl layer, though I'm not too sure how to do it (well, more precisely, how to name the methods without causing API/ABI breaks...)

We need a distinct way to check if the members of a clock are initialized (currently what rcl_clock_valid() is doing), since that gets used by the client libraries (e.g. in the ros_time_active() method, which then feeds into the clock setting methods (well, actually that seems to be the only place...)). Checking for validity in the rcl_clock_valid() sense is useful because it lets us check if a clock can even become valid (in the non-zero time sense) or not.

So if I were to add it into the rcl layer, I need to add a new method. The problem is:

  • I either need to change the current rcl_clock_valid()'s name and then have this new method be the rcl_clock_valid(), necessitating changes across every client library that uses the method. But I'm pretty sure this will break ABI/API, since the names will change.
  • Or I need to add a new method, but then it'd be confusing because rcl_clock_valid() would not be encoding the notion of validity in the design docs... Is there a better method name for the new method I can use?

If we can't resolve the naming issue, I think it'd be more intuitive to place it in the client libraries? Or maybe I'm missing something :V

Alternatively, do I even need to care about breaking ABI/API if I'm implementing this for rolling, or especially given that the scope of downstream use is so small :o

@methylDragon
Copy link
Contributor Author

methylDragon commented Nov 8, 2022

I just had an idea, I will create an rcl_time_valid() method in rcl to encode the notion of non-zero time validity, and use it alongside rcl_clock_valid() here.

Implemented here: ros2/rcl#1018

I will update this PR to use that new implementation in the rcl layer.

Edit: Updated. Builds and tests pass locally with that rcl PR's changes.

*/
RCLCPP_PUBLIC
bool
wait_for_valid(Context::SharedPtr context = contexts::get_global_default_context());
Copy link
Member

Choose a reason for hiding this comment

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

what's the use case of wait_for_valid()?
It seems like a strange feature to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It'll block till the first valid time. In most cases this will be waiting for sim to start (and time to march forward)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ivanpauno friendly ping on this comment.

Signed-off-by: methylDragon <methylDragon@gmail.com>
@methylDragon
Copy link
Contributor Author

methylDragon commented Dec 5, 2022

This PR has been updated to use this PR instead:

It doesn't make sense for a non-zero time point to be invalid, so the names and implementations have been changed to make this PR about checking for whether a clock has started or not

@methylDragon
Copy link
Contributor Author

@ros-pull-request-builder retest this please

@fujitatomoya
Copy link
Collaborator

@methylDragon thanks for iterating with patience, so this has been update and ready to review, right?

@methylDragon
Copy link
Contributor Author

@methylDragon thanks for iterating with patience, so this has been update and ready to review, right?

Hey @fujitatomoya , this is ready, yep! Though it'll take awhile for rcl to get released so CI works...

Signed-off-by: methylDragon <methylDragon@gmail.com>
rclcpp/src/rclcpp/clock.cpp Outdated Show resolved Hide resolved
rclcpp/include/rclcpp/clock.hpp Show resolved Hide resolved
Signed-off-by: methylDragon <methylDragon@gmail.com>
@methylDragon
Copy link
Contributor Author

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

@emersonknapp
Copy link
Collaborator

@methylDragon Can we backport this to Humble? I believe "new non-virtual methods on a class" is an ABI-stable change, so it seems to me like the answer is yes. The dependency ros2/rcl#1021 only introduces new free functions, which I believe is also ABI stable

@fujitatomoya
Copy link
Collaborator

"new non-virtual methods on a class" is an ABI-stable change

yeah, correct. i believe backport should be fine. besides, this is something we add, there is no user application behavior change.

@emersonknapp
Copy link
Collaborator

@Mergifyio backport humble

@mergify
Copy link

mergify bot commented Jun 12, 2023

backport humble

❌ Command disallowed due to command restrictions in the Mergify configuration.

  • sender-permission>=write

@methylDragon
Copy link
Contributor Author

@Mergifyio backport humble

@mergify
Copy link

mergify bot commented Jun 12, 2023

backport humble

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jun 12, 2023
fujitatomoya pushed a commit that referenced this pull request Jun 14, 2023
(cherry picked from commit c091fe1)

Co-authored-by: methylDragon <methylDragon@gmail.com>
tkimura4 pushed a commit to tier4/rclcpp that referenced this pull request Aug 17, 2023
(cherry picked from commit c091fe1)

Co-authored-by: methylDragon <methylDragon@gmail.com>
tkimura4 added a commit to tier4/rclcpp that referenced this pull request Aug 17, 2023
…rt 0.15.7 (#3)

* Implement validity checks for rclcpp::Clock (ros2#2040) (ros2#2210)

(cherry picked from commit c091fe1)

Co-authored-by: methylDragon <methylDragon@gmail.com>

* rclcpp: check rcl version [humble]

Signed-off-by: tomoya.kimura <tomoya.kimura@tier4.jp>

---------

Signed-off-by: tomoya.kimura <tomoya.kimura@tier4.jp>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: methylDragon <methylDragon@gmail.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.

None yet

6 participants