From 1a24da7322fa0bf827606f0472a2d152793c5031 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?M=2E=20Fatih=20C=C4=B1r=C4=B1t?= <mfc@leodrive.ai>
Date: Thu, 30 Nov 2023 18:41:45 +0300
Subject: [PATCH] fix(hw_interfaces): fix the lossy nanoseconds extraction
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: M. Fatih Cırıt <mfc@leodrive.ai>
---
 .../src/nebula_hesai_hw_interfaces/hesai_hw_interface.cpp      | 3 +--
 .../nebula_velodyne_hw_interfaces/velodyne_hw_interface.cpp    | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/nebula_hw_interfaces/src/nebula_hesai_hw_interfaces/hesai_hw_interface.cpp b/nebula_hw_interfaces/src/nebula_hesai_hw_interfaces/hesai_hw_interface.cpp
index 601960347..8bfe0399f 100644
--- a/nebula_hw_interfaces/src/nebula_hesai_hw_interfaces/hesai_hw_interface.cpp
+++ b/nebula_hw_interfaces/src/nebula_hesai_hw_interfaces/hesai_hw_interface.cpp
@@ -189,8 +189,7 @@ void HesaiHwInterface::ReceiveCloudPacketCallback(const std::vector<uint8_t> & b
   auto now_nanosecs =
     std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
   pandar_packet.stamp.sec = static_cast<int>(now_secs);
-  pandar_packet.stamp.nanosec =
-    static_cast<int>((now_nanosecs / 1000000000. - static_cast<double>(now_secs)) * 1000000000);
+  pandar_packet.stamp.nanosec = static_cast<std::uint32_t>(now_nanosecs % 1000000000);
   scan_cloud_ptr_->packets.emplace_back(pandar_packet);
 
   int current_phase = 0;
diff --git a/nebula_hw_interfaces/src/nebula_velodyne_hw_interfaces/velodyne_hw_interface.cpp b/nebula_hw_interfaces/src/nebula_velodyne_hw_interfaces/velodyne_hw_interface.cpp
index b8f86f266..0aa2a4d3f 100644
--- a/nebula_hw_interfaces/src/nebula_velodyne_hw_interfaces/velodyne_hw_interface.cpp
+++ b/nebula_hw_interfaces/src/nebula_velodyne_hw_interfaces/velodyne_hw_interface.cpp
@@ -72,8 +72,7 @@ void VelodyneHwInterface::ReceiveCloudPacketCallback(const std::vector<uint8_t>
     std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
   velodyne_packet.data = packet_data;
   velodyne_packet.stamp.sec = static_cast<int>(now_secs);
-  velodyne_packet.stamp.nanosec =
-    static_cast<int>((now_nanosecs / 1000000000. - static_cast<double>(now_secs)) * 1000000000);
+  velodyne_packet.stamp.nanosec = static_cast<std::uint32_t>(now_nanosecs % 1000000000);
   scan_cloud_ptr_->packets.emplace_back(velodyne_packet);
   processed_packets_++;