Skip to content

Commit

Permalink
Fix Kvaser timestamp (#1878)
Browse files Browse the repository at this point in the history
* get timestamp offset after canBusOn

* update comment
  • Loading branch information
zariiii9003 authored Oct 25, 2024
1 parent ff01a8b commit 5be89ec
Showing 1 changed file with 20 additions and 16 deletions.
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

0 comments on commit 5be89ec

Please sign in to comment.