Repository navigation
Add (and use) durationFromSec and timeFromSec - #16
Conversation
|
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"); |
There was a problem hiding this comment.
if you're wondering if this was already ns, upstream it is also 10,000(s): https://github.com/ros/geometry2/blob/439e2350ca9b2f0581a3631f0929acc2021db73e/test_tf2/test/test_message_filter.cpp#L272
|
@tfoote to give feedback on this |
|
I originally had it so that I went ahead and changed the storage of @clalancette could you please try this and let me know if you run into issues with this change? You may need to change some |
|
@dhood I'll give this a try today. |
|
@clalancette no worries. CI in the meantime: |
| { | ||
| uint32_t sec, nsec; | ||
| sec = (uint32_t)floor(t); | ||
| nsec = (uint32_t)std::round((t-sec) * 1e9); |
There was a problem hiding this comment.
continued from #20 (comment)
@wjwwood wrote:
Have you tried this function with
t_sec< 0? I think you might wantstd::truncrather thanstd::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
There was a problem hiding this comment.
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.
|
the switch to integer storage in b4307d5 broke the durationToSec implementation (gets truncated to whole number). working on it. |
|
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 |
|
Here's where I'm at with the updated implementation of Given a chrono duration, from its 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.. |
|
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). |
|
still waiting for review corresponding update to cartographer in ros2/cartographer_ros#3 |
wjwwood
left a comment
There was a problem hiding this comment.
lgtm, except for casting style being used in two places (that I saw)
| { | ||
| int32_t sec, nsec; | ||
| sec = (int32_t)floor(t_sec); | ||
| nsec = (int32_t)std::nearbyint((t_sec-sec) * 1e9); |
There was a problem hiding this comment.
Please use C++ style casts.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
ok, will do! (was copying from ROS 1 code not ROS 2 code)
There was a problem hiding this comment.
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
| int64_t count = input.count(); | ||
| int32_t sec, nsec; | ||
| nsec = (int32_t)(count % 1000000000l); | ||
| sec = (int32_t)((count - nsec) / 1000000000l); |
| { | ||
| int32_t sec, nsec; | ||
| sec = static_cast<int32_t>(floor(t_sec)); | ||
| nsec = static_cast<int32_t>(std::nearbyint((t_sec-sec) * 1e9)); |
There was a problem hiding this comment.
Do we need to explicitly set the rounding mode for std::nearbyint?
There was a problem hiding this comment.
Also, nit: spaces around the minus sign.
* 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]>
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::Durationunderneathtf2::TimePoint, sodoubles are used everywhere and notlong ints fromstd::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
doubleis being used underneathtf2::Durationinstead of the more standardstd::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.