定时器#

介绍#

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

This page uses brain.timer as Brain Timer. You can also construct a Timer object to create a new timer and replace it with your own configured name as needed.

以下是所有方法的列表:

方法——控制并与大脑的计时器互动。

  • clear – Resets the timer to zero.

  • time – Returns how much time has passed.

  • event – Registers a function to be called after a specified number of milliseconds.

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

  • Timer – Creates an additional timer.

清除#

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

Usage:
brain.timer.clear()

参数

描述

此方法没有参数。

# Reset the timer when the Bumper Switch is pressed
while True:
    print("\033[2J")
    print(brain.timer.time(SECONDS))
    wait(50, MSEC)
    if bumper_switch.pressing():
        brain.timer.clear()

时间#

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:
brain.timer.time(units)

参数

描述

units

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

# Display the time until the Bumper Switch is pressed
while True:
    print("\033[2J")
    print(brain.timer.time(SECONDS))
    wait(50, MSEC)
    if bumper_switch.pressing():
        brain.timer.clear()

事件#

event calls a function after a specified amount of time passes.

Usage:
brain.timer.event(callback, delay, arg)

参数

描述

callback

当定时器事件发生时要执行的函数。

delay

函数调用前的延迟时间,以毫秒为单位。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数

def timer_event():
    drivetrain.drive_for(FORWARD, 200, MM)

# Drive forward after a 5000 millisecond delay
brain.timer.event(timer_event, 5000)

构造函数#

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:
Timer

参数

描述

此构造函数没有参数。

# Create a Timer
timer = Timer()