定时器#

介绍#

VEX V5 中的计时器允许您跟踪经过的时间并根据时间间隔执行操作。通过多种时间管理方法,您可以在程序中创建精确的定时操作。

This page uses timer as the example Timer name. Replace it with your own configured name as needed.

以下是所有方法的列表:

方法——控制并与大脑的计时器进行交互。

  • clear – Resets the timer to zero.

  • time – Returns the elapsed time since the project started.

  • event – Calls a function after a specified number of milliseconds, with optional arguments.

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

  • Timer – Creates an additional timer.

清除#

clear sets the timer to zero.

Usage:
timer.clear()

参数

描述

此方法没有参数。

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

时间#

time returns the current elapsed time of the timer in the specified units — an integer for MSEC or a float for SECONDS.

Usage:
timer.time(units)

参数

描述

units

Optional. The unit to represent the time:

  • MSEC (default) – Milliseconds, returns an integer
  • SECONDS – Returns a float

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

事件#

event calls a function after a specified amount of time.

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

参数

描述

callback

一个先前定义的函数,在指定的时间后执行。

delay

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

arg

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

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

# Drive forward after a 5000 millisecond delay
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()