定时器#

介绍#

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

以下是所有方法的列表:

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

  • timer_reset – Resets the timer to zero.

  • timer_time – Returns how much time has passed.

  • timer_event – Registers a function to be called after a specified number of milliseconds.

timer_reset#

timer_reset sets the timer to zero. This can be used to time additional sections of code within the same project.

Usage:
brain.timer_reset()

参数

描述

此方法没有参数。

def main():
    # Reset the timer every 3 seconds
    while True:
        brain.clear()
        brain.print(brain.timer.time(SECONDS))
        if brain.timer_time(SECONDS) > 3: 
            brain.timer_reset()
        wait(5, MSEC)

# VR threads — Do not delete
vr_thread(main)

timer_time#

timer_time returns the time since the timer was last reset as a decimal. The timer is automatically reset at the start of a project.

Usage:
brain.timer_time(units)

参数

描述

units

The unit that represents the time: SECONDS

def main():
    # Display the current time on the timer
    while True:
        brain.clear()
        brain.print(brain.timer_time(SECONDS))
        wait(50, MSEC)

# VR threads — Do not delete
vr_thread(main)

timer_event#

timer_event calls a function after a specified amount of time passes.

Usage:
brain.timer_event(callback, delay)

参数

描述

callback

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

delay

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

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

def main():
    # Drive forward after a 5000 millisecond delay
    brain.timer_event(timer_event, 5000)

# VR threads — Do not delete
vr_thread(main)