定时器#

介绍#

VEX 123 中的计时器允许您跟踪经过的时间并根据时间间隔执行操作。它可用于测量某项任务所需的时间,或重置计时器以进行新的计时操作。

以下是所有方法的列表:

方法 – 控制计时器并与之交互。

  • reset – Resets the timer to zero.

  • time – Returns how much time has passed.

构造函数 – 创建额外的计时器。

  • Timer – Creates an additional timer.

重置#

reset sets the timer to zero. This can be used to time additional sections of code within the same project.

Usage:
timer.reset()

参数

描述

此方法没有参数。

# Reset the timer when the Move button is pressed
while True:
    console.clear()
    console.print(timer.time(SECONDS))
    wait(50, MSEC)
    if robot.get_button(MOVE):
        timer.reset()

时间#

time returns the time since the timer was last reset as a decimal. The timer is automatically reset at the start of a project.

Usage:
timer.time(units)

参数

描述

units

The unit that represents the time: MSEC (default) – milliseconds, or SECONDS

# Display the time until the Move button is pressed
while True:
    console.clear()
    console.print(timer.time(SECONDS))
    wait(50, MSEC)
    if robot.get_button(MOVE):
        timer.reset()

构造函数#

Timer#

Timer creates a new timer. A Timer object will immediately begin counting the moment it is created and will work with all timer methods.

Usage:
my_timer = Timer()

参数

描述

此构造函数没有参数。

# Display the time of two timers
wait(2, SECONDS)
timer_1 = Timer()
while True:
    console.clear()
    console.print("Timer: ", timer.time(SECONDS))
    console.new_line()
    console.print("timer_1: ", timer_1.time(SECONDS))
    wait(15, MSEC)