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, paused: bool=False, wait_time: float=1.0):
15        """ 初始化计时器 """
16        super().__init__(name)
17        self.time_left: float = 0.0
18        """ 剩余时间 """
19        self.wait_time: float = wait_time
20        """ 等待时间 """
21        self.autostart: float = autostart
22        """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """
23        self.one_shot = one_shot
24        """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止  """
25        self.paused = paused
26        """ 是否暂停, 如果为 True 则停止计时 """
27
28        self._running = False
29        
30        self.timeout = Signal()
31        """ 信号,当计时器计时结束时触发 """
32
33    def _ready(self):
34        if self.autostart:
35            self.start()
36
37    def _timer(self, delta):
38        if self.paused or not self._running:
39            return
40        
41        if self.time_left > 0:
42            self.time_left = max(0, self.time_left - delta)
43            if self.time_left <= 0:
44                self.timeout.emit()
45                if self.one_shot:
46                    self.stop()
47                else:
48                    self.time_left = self.wait_time
49
50    def is_stopped(self) -> bool:
51        """ 判断计时器是否停止 """
52        return not self._running
53
54    def start(self) -> None:
55        """ 启动计时器 """
56        self._running = True
57        self.time_left = self.wait_time
58
59    def stop(self) -> None:
60        """ 停止计时器 """
61        self._running = False
62        self.time_left = 0.0
class Timer(robotengine.node.Node):
13class Timer(Node):
14    """ 计时器类 """
15    def __init__(self, name="Timer", autostart: bool=False, one_shot: bool=False, paused: bool=False, wait_time: float=1.0):
16        """ 初始化计时器 """
17        super().__init__(name)
18        self.time_left: float = 0.0
19        """ 剩余时间 """
20        self.wait_time: float = wait_time
21        """ 等待时间 """
22        self.autostart: float = autostart
23        """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """
24        self.one_shot = one_shot
25        """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止  """
26        self.paused = paused
27        """ 是否暂停, 如果为 True 则停止计时 """
28
29        self._running = False
30        
31        self.timeout = Signal()
32        """ 信号,当计时器计时结束时触发 """
33
34    def _ready(self):
35        if self.autostart:
36            self.start()
37
38    def _timer(self, delta):
39        if self.paused or not self._running:
40            return
41        
42        if self.time_left > 0:
43            self.time_left = max(0, self.time_left - delta)
44            if self.time_left <= 0:
45                self.timeout.emit()
46                if self.one_shot:
47                    self.stop()
48                else:
49                    self.time_left = self.wait_time
50
51    def is_stopped(self) -> bool:
52        """ 判断计时器是否停止 """
53        return not self._running
54
55    def start(self) -> None:
56        """ 启动计时器 """
57        self._running = True
58        self.time_left = self.wait_time
59
60    def stop(self) -> None:
61        """ 停止计时器 """
62        self._running = False
63        self.time_left = 0.0

计时器类

Timer( name='Timer', autostart: bool = False, one_shot: bool = False, paused: bool = False, wait_time: float = 1.0)
15    def __init__(self, name="Timer", autostart: bool=False, one_shot: bool=False, paused: bool=False, wait_time: float=1.0):
16        """ 初始化计时器 """
17        super().__init__(name)
18        self.time_left: float = 0.0
19        """ 剩余时间 """
20        self.wait_time: float = wait_time
21        """ 等待时间 """
22        self.autostart: float = autostart
23        """ 是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动 """
24        self.one_shot = one_shot
25        """ 是否为一次性计时器, 如果为 True 则会在触发一次后停止  """
26        self.paused = paused
27        """ 是否暂停, 如果为 True 则停止计时 """
28
29        self._running = False
30        
31        self.timeout = Signal()
32        """ 信号,当计时器计时结束时触发 """

初始化计时器

time_left: float

剩余时间

wait_time: float

等待时间

autostart: float

是否自动启动, 如果为 True 则会在当前节点 _ready() 时自动启动

one_shot

是否为一次性计时器, 如果为 True 则会在触发一次后停止

paused

是否暂停, 如果为 True 则停止计时

timeout

信号,当计时器计时结束时触发

def is_stopped(self) -> bool:
51    def is_stopped(self) -> bool:
52        """ 判断计时器是否停止 """
53        return not self._running

判断计时器是否停止

def start(self) -> None:
55    def start(self) -> None:
56        """ 启动计时器 """
57        self._running = True
58        self.time_left = self.wait_time

启动计时器

def stop(self) -> None:
60    def stop(self) -> None:
61        """ 停止计时器 """
62        self._running = False
63        self.time_left = 0.0

停止计时器