diff --git a/ros/src/kinematic_icp_ros/utils/RosUtils.cpp b/ros/src/kinematic_icp_ros/utils/RosUtils.cpp index 198a1bd..3f840e6 100644 --- a/ros/src/kinematic_icp_ros/utils/RosUtils.cpp +++ b/ros/src/kinematic_icp_ros/utils/RosUtils.cpp @@ -22,6 +22,9 @@ // SOFTWARE. #include "kinematic_icp_ros/utils/RosUtils.hpp" +#include +#include + namespace kinematic_icp_ros::utils { std::optional GetTimestampField(const PointCloud2::ConstSharedPtr msg) { @@ -54,13 +57,24 @@ std::vector NormalizeTimestamps(const std::vector ×tamps) { auto ExtractTimestampsFromMsg(const PointCloud2::ConstSharedPtr msg, const PointField ×tamp_field) { + auto number_of_digits_decimal_part = [](const auto &stamp) { + const uint64_t number_of_seconds = static_cast(std::round(stamp)); + return number_of_seconds > 0 ? std::floor(std::log10(number_of_seconds) + 1) : 1; + }; auto extract_timestamps = - [&msg](sensor_msgs::PointCloud2ConstIterator &&it) -> std::vector { + [&](sensor_msgs::PointCloud2ConstIterator &&it) -> std::vector { const size_t n_points = msg->height * msg->width; std::vector timestamps; timestamps.reserve(n_points); for (size_t i = 0; i < n_points; ++i, ++it) { - timestamps.emplace_back(static_cast(*it)); + double stampd = static_cast(*it); + // If the number of digits is greater than 10 (which is the maximum number of digits + // that can be represented with a 32 bits integer), the stamp is in nanoseconds instead + // of seconds, perform conversion + if (number_of_digits_decimal_part(stampd) > 10) { + stampd *= 1e-9; + } + timestamps.emplace_back(stampd); } return timestamps; };