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

Duration: why no .seconds()? #482

Closed
AndyZe opened this issue May 29, 2018 · 8 comments
Closed

Duration: why no .seconds()? #482

AndyZe opened this issue May 29, 2018 · 8 comments

Comments

@AndyZe
Copy link

AndyZe commented May 29, 2018

Bug report

Required Info:

  • Operating System:
    Ubuntu 16.04
  • Installation type:
    • binary
  • Version or commit hash:
    • rclcpp: 0.4.0-0xenial-20180208-1432
  • Client library (if applicable):
    • rclcpp

Feature description

Feedback from a new ROS2 user: I think the new clock is unnecessarily confusing. Why does the constructor take seconds and nanoseconds, but there's only a method to access nanoseconds?

When I retrieve nanoseconds like so:

rclcpp::Duration delta_t_;
delta_t_.nanoseconds();

Am I getting the total time (seconds+nanoseconds)? Or is it the modulo?

My initial thought was that I should do this to convert to seconds, but I have a feeling this is not correct:
delta_t_.nanoseconds()/1000000000;

@AndyZe AndyZe changed the title Why no .seconds()? Time: why no .seconds()? May 29, 2018
@wjwwood
Copy link
Member

wjwwood commented May 29, 2018

There's not floating point seconds method right now. You could do duration.nanoseconds() / 1e9 to get floating seconds. There's no reason to not have a seconds() -> double method, I think a pr would be accepted.

I will say that the Time interfaces (along with others) are still subject to change in C++. For one thing, the steady clock and system clocks should not be diffable and should produce a compiler error, but I don't think they do right now.

@dirk-thomas
Copy link
Member

Might such a method be subject to precision loss? In that case it might be better to not provide it.

@tfoote
Copy link
Contributor

tfoote commented May 30, 2018

A conversion to double will potentially loose information. If we provide it it should have a explicit/unique signature so it's not accidentally used. Can you just use the datatypes instead of an untyped double?

@AndyZe
Copy link
Author

AndyZe commented May 30, 2018

My use case is multiplying a double to integrate:

error_integral_ += error_.at(0) * delta_t_.nanoseconds()/1e9;

@wjwwood
Copy link
Member

wjwwood commented May 30, 2018

As long as the loss of precision is documented I think it would be ok, but I agree doing that lossy conversion for the user is not ideal.

@AndyZe to answer your question

Why does the constructor take seconds and nanoseconds, but there's only a method to access nanoseconds?

more directly: it takes seconds and nanoseconds because that's the message storage format, and it has an option to return nanoseconds (that's "as nanoseconds" not the nanoseconds component of the time) because that's the underlying storage format.

Personally, I'd like to see the time and duration messages use a single integer for storage rather than the split pair, but that would be a disruptive change and I haven't had time to push for that to happen.

Am I getting the total time (seconds+nanoseconds)? Or is it the modulo?

It's the total time in nanoseconds (as in "nanoseconds since the unix epoch").

I think it's a worth while goal to avoid methods on the time class which return only part of the represented time (i.e. no sec() and nsec() methods). Instead we have a method to convert the time object directly into our message type which is stored as seconds and nanoseconds, but they're together.

@AndyZe
Copy link
Author

AndyZe commented May 30, 2018

I think it makes sense, too. It's ambiguous, currently. What it this going to do?
rclcpp::Time old(2, 2e9);

Is that 4 seconds, or ?

@wjwwood
Copy link
Member

wjwwood commented May 30, 2018

That's correct, it is 4 seconds, see:

Time::Time(int32_t seconds, uint32_t nanoseconds, rcl_clock_type_t clock_type)
: rcl_time_(init_time_point(clock_type))
{
if (seconds < 0) {
throw std::runtime_error("cannot store a negative time point in rclcpp::Time");
}
rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<int64_t>(seconds));
rcl_time_.nanoseconds += nanoseconds;
}

@sloretz sloretz self-assigned this Jul 27, 2018
@sloretz sloretz added the in progress Actively being worked on (Kanban column) label Jul 27, 2018
@mikaelarguedas mikaelarguedas added ready Work is about to start (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Aug 2, 2018
@mikaelarguedas mikaelarguedas changed the title Time: why no .seconds()? Duretion: why no .seconds()? Aug 20, 2018
@mikaelarguedas
Copy link
Member

Addressed in #536

@mikaelarguedas mikaelarguedas removed the ready Work is about to start (Kanban column) label Aug 20, 2018
@AndyZe AndyZe changed the title Duretion: why no .seconds()? Duration: why no .seconds()? Aug 20, 2018
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this issue Aug 5, 2022
…thon (ros2#482)

Signed-off-by: Emerson Knapp <eknapp@amazon.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

No branches or pull requests

6 participants