定时器#

介绍#

VEXcode VR 中的计时器功能允许您跟踪经过的时间并根据时间间隔执行操作。通过多种时间管理方法,您可以在程序中创建精确的定时操作。

以下是所有方法的列表:

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

  • 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.

timer_reset#

timer_reset sets the timer to zero.

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 current elapsed time of the timer as a float.

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#

event calls a function after a specified amount of time.

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)