定時器#

介紹#

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

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

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