Skip to content

Add (and use) durationFromSec and timeFromSec - #16

Merged
dhood merged 16 commits into
ros2from
update_timepoint
Jun 13, 2017
Merged

dhood merged 16 commits into
ros2from
update_timepoint

Conversation

@dhood

@dhood dhood commented Apr 20, 2017 •

Copy link
Copy Markdown
Member

connects to ros2/navigation#5

I wanted to use tf2::timeFromSec and tf2::durationFromSec but they weren't implemented, so I added them in c7bac5b (the code is copied from this ros1 code)

In order to do implement those, (in particular this line) I made this change to use tf2::Duration underneath tf2::TimePoint, so doubles are used everywhere and not long ints from std::chrono::nanoseconds (thank you @Karsten1987 !!)

In doing this I noticed that with the exception of a few places (e.g. db4574a#diff-473dd6a6d7f5086c80694ac302fd9f8eL137), we are accidentally initialising nanosecond durations instead of seconds. @clalancette idk if this is the underlying source of any of your TF issues.

There is still a question about why double is being used underneath tf2::Duration instead of the more standard std::chrono::nanoseconds. I think it was for 'nice' constructors e.g. tf2::Duration(1.5), but that seems to have just led to mistakenly thinking it represented seconds. We could switch it or make a constructor that takes seconds.

@dhood dhood added the in review Waiting for review (Kanban column) label Apr 20, 2017
@dhood dhood self-assigned this Apr 20, 2017
@dhood

dhood commented Apr 20, 2017 •

Copy link
Copy Markdown
Member Author

note that for the executables to build properly this needs #14

builtin_interfaces::msg::Time stamp(1);
bc.setTransform(createTransform(Quaternion(0,0,0,1), Vector3(1,2,3), stamp, "frame1", "frame2"), "me");
bc.setTransform(createTransform(Quaternion(0,0,0,1), Vector3(1,2,3), stamp + tf2::Duration(10000), "frame1", "frame2"), "me");
bc.setTransform(createTransform(Quaternion(0,0,0,1), Vector3(1,2,3), stamp + tf2::durationFromSec(10000), "frame1", "frame2"), "me");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dhood
dhood force-pushed the update_timepoint branch from db4574a to c98f460 Compare April 24, 2017 19:11
@dhood
dhood force-pushed the update_timepoint branch from c98f460 to 0090e41 Compare April 25, 2017 19:49
@dhood

dhood commented Apr 25, 2017 •

Copy link
Copy Markdown
Member Author

osx Build Status
windows Build Status
linux Build Status
aarch64 Build Status

@mikaelarguedas

Copy link
Copy Markdown
Member

@tfoote to give feedback on this

@dhood
dhood force-pushed the update_timepoint branch from 8e63ed7 to b4307d5 Compare April 27, 2017 21:17
@dhood

dhood commented Apr 27, 2017

Copy link
Copy Markdown
Member Author

I originally had it so that timeFromSec would give a time point offset from 'now'. I don't know what motivated that, but ros::Time(t) in ROS 1 doesn't do that offset from 'now', so I have replicated that in c622d9c.

I went ahead and changed the storage of tf2::TimePoint to be integer and not double (b4307d5). As @wjwwood pointed out, we shouldn't need double precision for measuring nanoseconds. I mentioned above this can lead to accidentally initialising Durations with ns when the user thinks the constructor takes seconds. Additionally, there are places where time comparisons will not work well with doubles e.g. this comparison of timestamps here.

@clalancette could you please try this and let me know if you run into issues with this change? You may need to change some tf2::Duration(t) constructors to tf2::durationFromSec(t).

@clalancette

Copy link
Copy Markdown
Contributor

@dhood I'll give this a try today.

@dhood

dhood commented May 1, 2017

Copy link
Copy Markdown
Member Author

@clalancette no worries.

CI in the meantime:

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

Comment thread tf2/include/tf2/time.h Outdated
{
uint32_t sec, nsec;
sec = (uint32_t)floor(t);
nsec = (uint32_t)std::round((t-sec) * 1e9);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

continued from #20 (comment)

@wjwwood wrote:

Have you tried this function with t_sec < 0? I think you might want std::trunc rather than std::floor, and that makes me think that the nanoseconds logic might not be perfect either.
Maybe you could use this function's "possible implementation":
http://en.cppreference.com/w/cpp/chrono/duration/round
It is not available until C++17, but it's possible implementation might work in more cases than this.

I copied this code from ROS 1's implementation: https://github.com/ros/roscpp_core/blob/057f7fa5c36df67757c6cb91c319faa67cefd6e6/rostime/include/ros/time.h#L153..L158 but switched from using boost::math::round to std::round. the tests there only seem to test positive times https://github.com/ros/roscpp_core/blob/057f7fa5c36df67757c6cb91c319faa67cefd6e6/rostime/test/time.cpp#L145 - is that something we should change?

I'll add similar tests 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.

I guess round actually makes sense here. It would still be nice to check the negative behavior in a test. My concern is that the result of std::round((t-sec) * 1e9) might be negative, which we are then just casting to an unsigned integer.

@dhood

dhood commented May 2, 2017 •

Copy link
Copy Markdown
Member Author

the switch to integer storage in b4307d5 broke the durationToSec implementation (gets truncated to whole number). working on it.

@dhood dhood added in progress Actively being worked on (Kanban column) and removed in review Waiting for review (Kanban column) labels May 2, 2017
@dhood

dhood commented May 11, 2017 •

Copy link
Copy Markdown
Member Author

I'm trying to update the durationToSec implementation to work with integer storage of tf2::Duration, modified in b4307d5

I'm stuck trying to get this line to not turn an nsec integer value of, e.g. 639909744 into an nsec_double value of 0.63990974400000000255062104770332.

@mikaelarguedas

Copy link
Copy Markdown
Member

Not sure there is a good way to work around rounding errors (at least in C). I guess the easiest would be to make sure that the "diff" is lower that your "precision" aka 1e-9. Also I think there are few systems that can be timestamping precisely at that scale

@dhood

dhood commented May 12, 2017 •

Copy link
Copy Markdown
Member Author

Here's where I'm at with the updated implementation of durationToSec.

Given a chrono duration, from its count in ns, the seconds and nanoseconds parts can be calculated individually quite accurately as doubles. It is combining them with addition that introduces the floating point error. Example:

3: Count (ns): 1494614995898323272
3: sec_double: 1494614995.0000000000000000
3: nsec_double: 0.8983232720000001
3: Returning double of: 1494614995.8983232975006104
3: Difference to expected (ns): 26

This seems like reasonable behaviour, people shouldn't expect the double representation of a timepoint to be exact. However, a tolerance of 1e-9 is too strict: the value can be off by up to ~150ns.

it doesn't seem to work properly for negatives so I'm working on that now..

@dhood

dhood commented Jun 5, 2017

Copy link
Copy Markdown
Member Author

Back in review.

I have added tests for the new functions, which now include testing negative durations: https://github.com/ros2/geometry2/blob/978b8083bf2e64ed3b96288af344fc929445d338/tf2/test/simple_tf2_core.cpp#L159..L212 (using pre-defined values here because I understood from this comment in the ros1 code that random values aren't ideal).

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status (unrelated flaky test)

@dhood dhood added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Jun 5, 2017
@dhood

dhood commented Jun 7, 2017 •

Copy link
Copy Markdown
Member Author

@ros2/team this is in review (wasn't showing on waffle board)

To check that the new tests aren't flaky, these jobs have test args of --only tf2 --retest-until-fail 20:

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

@dhood

dhood commented Jun 12, 2017

Copy link
Copy Markdown
Member Author

still waiting for review

corresponding update to cartographer in ros2/cartographer_ros#3
anything else that breaks from the change from double to integer storage was probably intending to use seconds not nanoseconds (e.g. all of these updated tests) and so wouldn't be working as expected anyway.

@wjwwood wjwwood left a comment

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.

lgtm, except for casting style being used in two places (that I saw)

Comment thread tf2/include/tf2/time.h Outdated
{
int32_t sec, nsec;
sec = (int32_t)floor(t_sec);
nsec = (int32_t)std::nearbyint((t_sec-sec) * 1e9);

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.

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.

Btw, this isn't in our style guide, but it is in the Google Style guide which ours is based on: https://google.github.io/styleguide/cppguide.html#Casting

When writing our style guide I tried to avoid copying the entire Google Style guide, so I don't know if this can be made more visible or not.

I also do not think cpplint.py checks this (it may not be possible to do so easily).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ok, will do! (was copying from ROS 1 code not ROS 2 code)

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.

I also do not think cpplint.py checks this (it may not be possible to do so easily).

IIRC cpplint does check this, but I don't think we run linter on this package

Comment thread tf2/include/tf2/time.h Outdated
int64_t count = input.count();
int32_t sec, nsec;
nsec = (int32_t)(count % 1000000000l);
sec = (int32_t)((count - nsec) / 1000000000l);

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.

Please use C++ style casts.

@dhood

dhood commented Jun 13, 2017

Copy link
Copy Markdown
Member Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Comment thread tf2/include/tf2/time.h Outdated
{
int32_t sec, nsec;
sec = static_cast<int32_t>(floor(t_sec));
nsec = static_cast<int32_t>(std::nearbyint((t_sec-sec) * 1e9));

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.

Do we need to explicitly set the rounding mode for std::nearbyint?

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.

Also, nit: spaces around the minus sign.

@dhood

dhood commented Jun 13, 2017

Copy link
Copy Markdown
Member Author

new CI after review

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

@dhood
dhood merged commit 859bd3d into ros2 Jun 13, 2017
@dhood
dhood deleted the update_timepoint branch June 13, 2017 21:41
@dhood dhood removed the in review Waiting for review (Kanban column) label Jun 13, 2017
srmainwaring pushed a commit to srmainwaring/geometry2 that referenced this pull request Sep 18, 2023
* Fixes policy CMP0135 warning for CMake >= 3.24

Signed-off-by: Crola1702 <[email protected]>
(cherry picked from commit 806f79a81f95290615ed8ca657653344ce9699db)

Co-authored-by: Cristóbal Arroyo <[email protected]>
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.

4 participants