计时器#

介绍#

VEX IQ(第二代)中的计时器允许您跟踪已用时间并根据时间间隔执行操作。通过多种时间管理方法,您可以在程序中创建精确且定时的操作。

以下是所有方法的列表:

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

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

  • clear – Resets the timer to zero.

  • system – Returns the number of milliseconds the Brain has been powered on.

  • system_high_res – Returns the number of microseconds the Brain has been powered on.

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

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

  • Timer – Creates an additional timer.

时间#

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

Usage:
brain.timer.time(units)

参数

描述

units

The time units are milliseconds MSEC (default) or SECONDS.

# Display the current time on the timer
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print(brain.timer.time(SECONDS))
    wait(50, MSEC)

清除#

clear sets the timer to zero.

Usage:
brain.timer.clear()

参数

描述

该方法没有参数。

# Reset the timer when a button is pressed
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print(brain.timer.time(SECONDS))
    wait(50, MSEC)
    if brain.buttonCheck.pressing():
        brain.timer.clear()

系统#

system returns the number of milliseconds the Brain has been powered on.

Usage:
brain.timer.system()

参数

描述

该方法没有参数。

# Display the time the brain has been on in milliseconds
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print(brain.timer.system())
    brain.screen.print(" MSEC")
    wait(50, MSEC)

系统高分辨率#

system_high_res returns the number of microseconds the Brain has been powered on.

Usage:
brain.timer.system_high_res()

参数

描述

该方法没有参数。

# Display the time the brain has been on in microseconds
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print(brain.timer.system_high_res())
    wait(50, MSEC)

事件#

event calls a function after a specified amount of time.

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

参数

描述

callback

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

delay

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

arg

可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](./Functions.md#functions-with-parameters)。

# Define a function timer_event
def timer_event():
    drivetrain.drive_for(FORWARD, 200, MM)

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

构造函数#

Constructors are used to manually create Timer objects, which are necessary for creating any additional timers besides brain.timer.

For the examples below, the configured Timer will be named timer_1 and will be used in all subsequent examples throughout this API documentation when referring to Timer class methods.

Timer#

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

Usage:
Timer()

参数

描述

此构造函数没有参数。

wait(2, SECONDS)
timer_1 = Timer()
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Brain: ")
    brain.screen.print(brain.timer.time(SECONDS))
    brain.screen.next_row()
    brain.screen.print("timer_1: ")
    brain.screen.print(timer_1.time(SECONDS))
    wait(15, MSEC)