计时器#

介绍#

VEX IQ(第二代)中的计时器可以跟踪经过的时间,并根据时间间隔执行操作。它可用于测量某项任务所需的时间,或重置时间以进行新的计时操作。

以下是所有方法的列表:

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

  • time — 返回经过的时间。

  • clear — 将计时器重置为零。

  • system — 返回大脑已通电的毫秒数。

  • system_high_res — 返回大脑已通电的微秒数。

  • event — 注册一个在指定毫秒数后调用的函数。

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

  • Timer — 创建一个额外的定时器。

时间#

time 返回自上次计时器重置以来经过的时间,以十进制数表示。计时器会在项目开始时自动重置。

Usage:
brain.timer.time(units)

参数

描述

units

时间单位: MSEC (默认)– 毫秒,或 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 将计时器设置为零。这可用于对同一项目中的其他代码段进行计时。

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 返回 大脑 已通电的毫秒数。

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 返回 brain 通电的微秒数。

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

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

参数

描述

callback

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

delay

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

arg

Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information.

# 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)

构造函数#

构造函数用于手动创建 Timer 对象,这是创建除 brain.timer之外的任何其他计时器所必需的。

对于以下示例,配置的定时器将被命名为 timer_1 ,并且在本 API 文档中所有后续示例中,当引用 Timer 类方法时,都将使用该定时器。

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)