robotengine.timer
Timer 是 robotengine 中异步操作的基础。
Timer 继承自 Node 节点,可以在节点树中进行管理。
1""" 2Timer 是 robotengine 中异步操作的基础。 3 4Timer 继承自 Node 节点,可以在节点树中进行管理。 5 6""" 7from robotengine.node import Node 8import threading 9import time 10from robotengine.signal import Signal 11 12class Timer(Node): 13 """ 计时器类 """ 14 def __init__(self, name="Timer", autostart: bool=False, one_shot: bool=False, wait_time: float=1.0): 15 """ 16 初始化计时器 17 18 :param name: 节点名称 19 :param autostart: 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 20 :param one_shot: 是否为一次性计时器, 如果为 True 则会在触发一次后停止 21 :param wait_time: 等待时间 22 """ 23 super().__init__(name) 24 self.time_left: float = 0.0 25 """ 剩余时间 """ 26 self.wait_time: float = wait_time 27 """ 等待时间 """ 28 self.autostart: float = autostart 29 """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """ 30 self.one_shot = one_shot 31 """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止 """ 32 self.paused = False 33 """ 是否暂停, 如果为 True 则停止计时 """ 34 35 self._running = False 36 37 self.timeout = Signal() 38 """ 信号,当计时器计时结束时触发 """ 39 40 def _ready(self): 41 if self.autostart: 42 self.start() 43 44 def _timer(self, delta): 45 if self.paused or not self._running: 46 return 47 48 if self.time_left > 0: 49 self.time_left = max(0, self.time_left - delta) 50 if self.time_left <= 0: 51 self.timeout.emit() 52 if self.one_shot: 53 self.stop() 54 else: 55 self.time_left = self.wait_time 56 57 def is_stopped(self) -> bool: 58 """ 59 判断计时器是否停止 60 """ 61 return not self._running 62 63 def start(self) -> None: 64 """ 65 启动计时器 66 """ 67 self._running = True 68 self.time_left = self.wait_time 69 70 def stop(self) -> None: 71 """ 72 停止计时器 73 """ 74 self._running = False 75 self.time_left = 0.0
13class Timer(Node): 14 """ 计时器类 """ 15 def __init__(self, name="Timer", autostart: bool=False, one_shot: bool=False, wait_time: float=1.0): 16 """ 17 初始化计时器 18 19 :param name: 节点名称 20 :param autostart: 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 21 :param one_shot: 是否为一次性计时器, 如果为 True 则会在触发一次后停止 22 :param wait_time: 等待时间 23 """ 24 super().__init__(name) 25 self.time_left: float = 0.0 26 """ 剩余时间 """ 27 self.wait_time: float = wait_time 28 """ 等待时间 """ 29 self.autostart: float = autostart 30 """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """ 31 self.one_shot = one_shot 32 """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止 """ 33 self.paused = False 34 """ 是否暂停, 如果为 True 则停止计时 """ 35 36 self._running = False 37 38 self.timeout = Signal() 39 """ 信号,当计时器计时结束时触发 """ 40 41 def _ready(self): 42 if self.autostart: 43 self.start() 44 45 def _timer(self, delta): 46 if self.paused or not self._running: 47 return 48 49 if self.time_left > 0: 50 self.time_left = max(0, self.time_left - delta) 51 if self.time_left <= 0: 52 self.timeout.emit() 53 if self.one_shot: 54 self.stop() 55 else: 56 self.time_left = self.wait_time 57 58 def is_stopped(self) -> bool: 59 """ 60 判断计时器是否停止 61 """ 62 return not self._running 63 64 def start(self) -> None: 65 """ 66 启动计时器 67 """ 68 self._running = True 69 self.time_left = self.wait_time 70 71 def stop(self) -> None: 72 """ 73 停止计时器 74 """ 75 self._running = False 76 self.time_left = 0.0
计时器类
Timer( name='Timer', autostart: bool = False, one_shot: bool = False, wait_time: float = 1.0)
15 def __init__(self, name="Timer", autostart: bool=False, one_shot: bool=False, wait_time: float=1.0): 16 """ 17 初始化计时器 18 19 :param name: 节点名称 20 :param autostart: 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 21 :param one_shot: 是否为一次性计时器, 如果为 True 则会在触发一次后停止 22 :param wait_time: 等待时间 23 """ 24 super().__init__(name) 25 self.time_left: float = 0.0 26 """ 剩余时间 """ 27 self.wait_time: float = wait_time 28 """ 等待时间 """ 29 self.autostart: float = autostart 30 """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """ 31 self.one_shot = one_shot 32 """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止 """ 33 self.paused = False 34 """ 是否暂停, 如果为 True 则停止计时 """ 35 36 self._running = False 37 38 self.timeout = Signal() 39 """ 信号,当计时器计时结束时触发 """
初始化计时器
:param name: 节点名称
:param autostart: 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动
:param one_shot: 是否为一次性计时器, 如果为 True 则会在触发一次后停止
:param wait_time: 等待时间
def
start(self) -> None:
64 def start(self) -> None: 65 """ 66 启动计时器 67 """ 68 self._running = True 69 self.time_left = self.wait_time
启动计时器