From c59bc605b08f8d9ce580352c2235fb4d30eb5f10 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Date: Thu, 20 Feb 2025 10:08:42 -0500 Subject: [PATCH 1/3] Fix timestamp offset bug in BLFWriter In the BLF files, the frame timestamp are stored as a delta from the "start time". The "start time" stored in the file is the first frame timestamp rounded to 3 decimals. When we calculate the timestamp delta for each frame, we were previously calculating it using the non-rounded "start time". This new code, use the "start time" as the first frame timestamp truncated to 3 decimals. --- can/io/blf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/io/blf.py b/can/io/blf.py index 7d944a846..995a729a5 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -530,7 +530,7 @@ def _add_object(self, obj_type, data, timestamp=None): if timestamp is None: timestamp = self.stop_timestamp or time.time() if self.start_timestamp is None: - self.start_timestamp = timestamp + self.start_timestamp = int(timestamp * 1000) / 1000 self.stop_timestamp = timestamp timestamp = int((timestamp - self.start_timestamp) * 1e9) header_size = OBJ_HEADER_BASE_STRUCT.size + OBJ_HEADER_V1_STRUCT.size From 2a7a9f37f16cd96e50084d2b25bc9c84dd56a8a0 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Date: Tue, 25 Feb 2025 10:03:28 -0500 Subject: [PATCH 2/3] Update example_data.py Making sure all tests messages contains a timestamp to avoid situation where the first written message have timestamps equal to zero. --- test/data/example_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/example_data.py b/test/data/example_data.py index 592556926..b78420e4e 100644 --- a/test/data/example_data.py +++ b/test/data/example_data.py @@ -160,7 +160,7 @@ def sort_messages(messages): TEST_MESSAGES_ERROR_FRAMES = sort_messages( [ - Message(is_error_frame=True), + Message(is_error_frame=True, timestamp=TEST_TIME), Message(is_error_frame=True, timestamp=TEST_TIME + 0.170), Message(is_error_frame=True, timestamp=TEST_TIME + 17.157), ] From 001e5eb90c1ba33aa3a4a6bffd7b8e92d1aa24f6 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Date: Tue, 25 Feb 2025 10:04:34 -0500 Subject: [PATCH 3/3] Update blf.py Adding comment --- can/io/blf.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/can/io/blf.py b/can/io/blf.py index 995a729a5..deefd4736 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -530,6 +530,9 @@ def _add_object(self, obj_type, data, timestamp=None): if timestamp is None: timestamp = self.stop_timestamp or time.time() if self.start_timestamp is None: + # Save start timestamp using the same precision as the BLF format + # Truncating to milliseconds to avoid rounding errors when calculating + # the timestamp difference self.start_timestamp = int(timestamp * 1000) / 1000 self.stop_timestamp = timestamp timestamp = int((timestamp - self.start_timestamp) * 1e9)