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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions realsense2_camera/include/base_realsense_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ namespace realsense2_camera
void enable_devices();
void setupFilters();
bool setBaseTime(double frame_time, rs2_timestamp_domain time_domain);
uint64_t millisecondsToNanoseconds(double timestamp_ms);
rclcpp::Time frameSystemTimeSec(rs2::frame frame);
cv::Mat& fix_depth_scale(const cv::Mat& from_image, cv::Mat& to_image);
void clip_depth(rs2::depth_frame depth_frame, float clipping_dist);
Expand Down
24 changes: 21 additions & 3 deletions realsense2_camera/src/base_realsense_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,17 +708,35 @@ bool BaseRealSenseNode::setBaseTime(double frame_time, rs2_timestamp_domain time
return false;
}

uint64_t BaseRealSenseNode::millisecondsToNanoseconds(double timestamp_ms)
{
// modf breaks input into an integral and fractional part
double int_part_ms, fract_part_ms;
fract_part_ms = modf(timestamp_ms, &int_part_ms);

//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


return int_part_ns + fract_part_ns;
}

rclcpp::Time BaseRealSenseNode::frameSystemTimeSec(rs2::frame frame)
{
double timestamp_ms = frame.get_timestamp();
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.


/*
Fixing deprecated-declarations compilation warning.
Duration(rcl_duration_value_t) is deprecated in favor of
static Duration::from_nanoseconds(rcl_duration_value_t)
starting from GALAXY.
starting from GALACTIC.
*/
#if defined(FOXY) || defined(ELOQUENT) || defined(DASHING)
auto duration = rclcpp::Duration(elapsed_camera_ns);
Expand All @@ -730,7 +748,7 @@ rclcpp::Time BaseRealSenseNode::frameSystemTimeSec(rs2::frame frame)
}
else
{
return rclcpp::Time(frame.get_timestamp() * 1e6);
return rclcpp::Time(millisecondsToNanoseconds(timestamp_ms));
}
}

Expand Down