unitree_sdk2_python/unitree_sdk2py/utils/hz_sample.py

25 lines
643 B
Python
Raw Normal View History

2024-04-26 21:26:50 +08:00
import time
from threading import Lock
from .thread import RecurrentThread
class HZSample:
def __init__(self, interval: float = 1.0):
self.__count = 0
self.__inter = interval if interval > 0.0 else 1.0
self.__lock = Lock()
self.__thread = RecurrentThread(self.__inter, target=self.TimerFunc)
def Start(self):
self.__thread.Start()
def Sample(self):
with self.__lock:
self.__count += 1
def TimerFunc(self):
count = 0
with self.__lock:
count = self.__count
self.__count = 0
print("HZ: {}".format(count/self.__inter))