定時器#

介紹#

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

以下是所有方法的清單:

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

  • reset – 將計時器重設為零。

  • time – 傳回經過的時間。

  • event – 註冊一個在指定毫秒數後呼叫的函數。

建構函式 – 建立額外的計時器。

  • Timer – 建立一個額外的定時器。

重置#

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

用法:
timer.reset()

參數

描述

此方法沒有參數。

# Build Used: Super Code Base 2.0
def main():
    # Reset the timer when the bumper is pressed
    while True:
        console.clear()
        console.print(timer.time(SECONDS))
        wait(50, MSEC)
        if bumper.is_pressed():
            timer.reset()

# Start threads — Do not delete
start_thread(main)

時間#

time 傳回自上次計時器重設以來經過的時間,以十進位數表示。計時器會在專案開始時自動重設。

用法:
timer.time(units)

參數

描述

units

時間單位: MSEC (預設)– 毫秒,或 SECONDS

# Build Used: Super Code Base 2.0
def main():
    # Display the time until the bumper is pressed
    while True:
        console.clear()
        console.print(timer.time(SECONDS))
        wait(50, MSEC)
        if bumper.is_pressed():
            timer.reset()

# Start threads — Do not delete
start_thread(main)

事件#

事件event

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

參數

描述

callback

當定時器事件發生時要執行的函數。

delay

函數呼叫前的延遲時間,以毫秒為單位。

arg

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱帶參數的函數。

# Build Used: Super Code Base 2.0
def timer_event():
    drivetrain.drive_for(FORWARD, 200, MM)

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

# Start threads — Do not delete
start_thread(main)

建構函數#

Timer#

Timer 創造一個新的計時器。 2 Timer 物件會在創建後立即開始計時,並且可以與所有 timer 方法一起使用。

用法:
my_timer = Timer()

參數

描述

此建構函數沒有參數。

def main():
    # Display the time of two timers
    wait(2, SECONDS)
    timer_1 = Timer()
    while True:
        console.clear()
        console.print("Timer: ", timer.time(SECONDS))
        console.new_line()
        console.print("timer_1: ", timer_1.time(SECONDS))
        wait(15, MSEC)

# Start threads — Do not delete
start_thread(main)