定时器#

介绍#

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

以下是所有方法的列表:

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

  • reset – 将计时器重置为零。

  • time – 返回经过的时间。

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

  • Timer – 创建一个额外的定时器。

重置#

reset 将计时器设置为零。这可用于对同一项目中的其他代码段进行计时。

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 返回自上次计时器重置以来经过的时间,以十进制数表示。计时器会在项目开始时自动重置。

Usage:
timer.time(units)

参数

描述

units

时间单位: MSEC (默认)– 毫秒,或 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 创建一个新的计时器。2 Timer 对象会在创建后立即开始计时,并且可以与所有 timer 方法一起使用。

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)