定时器#

介绍#

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

注意: 只有在创建了 Brain brain = Brain()用,因为所有计时器功能都是通过 Brain 上的方法驱动的。

本页面使用 brain 作为示例大脑名称。您可以根据需要将其替换为您自己配置的名称。

以下是所有方法的列表:

  • timer_reset – 将定时器重置为零。

  • timer_time – 返回自项目开始以来经过的时间。

  • timer_event – 在指定的毫秒数后调用函数,带有可选参数。

定时器重置#

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

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 返回计时器当前经过的时间,单位为指定单位 MSEC 为整数, SECONDS为浮点数。

Usage:
brain.timer_time(units)

参数

描述

units

可选。时间单位:

  • MSEC (默认)– 毫秒,返回整数
  • SECONDS – 返回浮点数

# 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 在指定的时间过后调用一个函数。

Usage:
timer_event(callback, delay, arg)

参数

描述

callback

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

delay

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

arg

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

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

# Move the arm after a 5000 millisecond delay
timer_event(move_arm, 5000)