Skip to content

Fix Kvaser timestamp #1878

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

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Changes from all commits
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
36 changes: 20 additions & 16 deletions can/interfaces/kvaser/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,35 +573,39 @@ def __init__(
)
canSetBusOutputControl(self._write_handle, can_driver_mode)

self._is_filtered = False
super().__init__(
channel=channel,
can_filters=can_filters,
**kwargs,
)

# activate channel after CAN filters were applied
log.debug("Go on bus")
if not self.single_handle:
canBusOn(self._read_handle)
canBusOn(self._write_handle)

# timestamp must be set after bus is online, otherwise kvReadTimer may return erroneous values
self._timestamp_offset = self._update_timestamp_offset()

def _update_timestamp_offset(self) -> float:
timer = ctypes.c_uint(0)
try:
if time.get_clock_info("time").resolution > 1e-5:
ts, perfcounter = time_perfcounter_correlation()
kvReadTimer(self._read_handle, ctypes.byref(timer))
current_perfcounter = time.perf_counter()
now = ts + (current_perfcounter - perfcounter)
self._timestamp_offset = now - (timer.value * TIMESTAMP_FACTOR)
return now - (timer.value * TIMESTAMP_FACTOR)
else:
kvReadTimer(self._read_handle, ctypes.byref(timer))
self._timestamp_offset = time.time() - (timer.value * TIMESTAMP_FACTOR)
return time.time() - (timer.value * TIMESTAMP_FACTOR)

except Exception as exc:
# timer is usually close to 0
log.info(str(exc))
self._timestamp_offset = time.time() - (timer.value * TIMESTAMP_FACTOR)

self._is_filtered = False
super().__init__(
channel=channel,
can_filters=can_filters,
**kwargs,
)

# activate channel after CAN filters were applied
log.debug("Go on bus")
if not self.single_handle:
canBusOn(self._read_handle)
canBusOn(self._write_handle)
return time.time() - (timer.value * TIMESTAMP_FACTOR)

def _apply_filters(self, filters):
if filters and len(filters) == 1:
Expand Down
Loading