定时器#

介绍#

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

Note: The Timer is only available when a Brain has been constructed with brain = Brain(), as all Timer functionality is driven through methods on the Brain instance.

This page uses brain as the example Brain name. Replace it with your own configured name as needed.

以下是所有方法的列表:

  • timer_reset – Resets the timer to zero.

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

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

定时器重置#

clear sets the timer to zero.

Usage:
brain.timer_reset()

参数

描述

此方法没有参数。

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    print("\033[2J")
    print(brain.timer_time(SECONDS))
    wait(50, MSEC)
    if arm.is_done():
        brain.timer_reset()

定时器时间#

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

Optional. The unit to represent the time:

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

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    print("\033[2J")
    print(brain.timer_time(SECONDS))
    wait(50, MSEC)
    if arm.is_done():
        brain.timer_reset()

定时器事件#

timer_event calls a function after a specified amount of time.

Usage:
timer_event(callback, delay, arg)

参数

描述

callback

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

delay

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

arg

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

def timer_event():
    arm.move_inc(0, 100, 0)

# Drive forward after a 5000 millisecond delay
timer_event(timer_event, 5000)