定時器#

介紹#

VEXcode VR 中的計時器功能可讓您追蹤經過的時間並根據時間間隔執行操作。它可用於測量某項任務所需的時間,或重設計時器以進行新的計時操作。

以下是所有方法的清單:

方法-控制並與大腦的計時器互動。

timer_reset#

timer_reset 將計時器重設為零。這可用於對同一專案中的其他程式碼段進行計時。

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 返回自上次重置計時器以來經過的時間,以十進制數表示。計時器會在專案開始時自動重設。

Usage:
brain.timer_time(units)

參數

描述

units

表示時間的單位: 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 在指定的時間過後呼叫一個函數。

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)