Skip to content

Commit d736826

Browse files
Improve PDO periodic transmit accuracy
1 parent a10d23c commit d736826

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

canopen/pdo.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import time
23
import threading
34
import math
@@ -14,6 +15,17 @@
1415
RTR_NOT_ALLOWED = 1 << 30
1516

1617

18+
if hasattr(time, "perf_counter"):
19+
# Choose time.perf_counter if available
20+
timer = time.perf_counter
21+
elif sys.platform == "win32":
22+
# On Windows, the best timer is time.clock
23+
timer = time.clock
24+
else:
25+
# On most other platforms the best timer is time.time
26+
timer = time.time
27+
28+
1729
logger = logging.getLogger(__name__)
1830

1931

@@ -363,13 +375,14 @@ def wait_for_reception(self, timeout=10):
363375

364376
def _periodic_transmit(self):
365377
while not self.stop_event.is_set():
366-
start = time.time()
378+
start = timer()
367379
try:
368380
self.transmit()
369381
except CanError as error:
370382
print(str(error))
371-
time_left = self.period - (time.time() - start)
372-
time.sleep(max(time_left, 0.0))
383+
time_left = self.period - (timer() - start)
384+
if time_left > 0:
385+
time.sleep(time_left)
373386

374387

375388
class Variable(common.Variable):

canopen/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = "0.4.0.dev5"
2+
__version__ = "0.4.0.dev6"

0 commit comments

Comments
 (0)