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

fix timestamps #2658

Merged
merged 4 commits into from
Mar 20, 2023
Merged

Conversation

SamerKhshiboun
Copy link
Collaborator

Fix timestamp calculation metadata header to be aligned with metadata json timestamp
Tracked by [DSO-18855]

@SamerKhshiboun SamerKhshiboun marked this pull request as ready for review March 14, 2023 12:13
@SamerKhshiboun SamerKhshiboun requested a review from Nir-Az March 14, 2023 12:13
@Nir-Az Nir-Az requested a review from OhadMeir March 15, 2023 11:04

//convert to ns
uint64_t int_part_ns = static_cast<uint64_t>(int_part_ms) * 1000000;
uint64_t fract_part_ns = static_cast<uint64_t>(fract_part_ms * 10000000);

Choose a reason for hiding this comment

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

Too many zeros.
It would be better to use a constant static constexpr uint64_t milli_to_nano = 1000000.
Can also use C++14 digit separator 1'000'000 :-)


// Convert fract_parts_ns into 6 digits and round it
// to be aligned with librealsense get_timestamp API
fract_part_ns = (fract_part_ns % 10 > 4) ? (fract_part_ns/10 + 1) : (fract_part_ns/10);

Choose a reason for hiding this comment

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

More efficient ( fract_part_ns + 5 ) / 10. No need to use ? :.
Or use round function while converting to uint64_t

if (frame.get_frame_timestamp_domain() == RS2_TIMESTAMP_DOMAIN_HARDWARE_CLOCK)
{
double elapsed_camera_ns = (/*ms*/ frame.get_timestamp() - /*ms*/ _camera_time_base) * 1e6;
double elapsed_camera_ns = millisecondsToNanoseconds(timestamp_ms - _camera_time_base);

Choose a reason for hiding this comment

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

Converting uint64_t to double might loose precision.
double can exactly represent numbers up to 2^51 which is around 26 days in ns. Is it OK? If timestamp is since power-up then probably yes. If since epoch then surely not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this question in more related to Librealsense API, since frame.get_timestamp is returning a double timestamp.
For this PR, I think this discussion can be skipped, but it is really worth taking this issue into account.

// for fract_part_ns, multiplie ns * 10, then divide by 10 while rounding
// to be aligned with librealsense get_timestamp API
uint64_t fract_part_ns = static_cast<uint64_t>(fract_part_ms * milli_to_nano * 10);
fract_part_ns = (fract_part_ns + 5)/10;

Choose a reason for hiding this comment

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

Looking at this now it seems like a redundant division.
Use static_cast<uint64_t>( ( fract_part_ms + 0.5 ) * milli_to_nano ) to eliminate it.

Choose a reason for hiding this comment

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

Or just std::round that does the same. It is more readable.

Choose a reason for hiding this comment

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

After consulting with StackOverflow I suggest you use round as the +0.5 method does not handle all corner cases (Inf, NaN and such).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link

@OhadMeir OhadMeir left a comment

Choose a reason for hiding this comment

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

LGTM

@Nir-Az Nir-Az merged commit 78f0377 into IntelRealSense:ros2-development Mar 20, 2023
@SamerKhshiboun SamerKhshiboun deleted the fix-timestamps branch May 9, 2023 10:49
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