定時器#
介紹#
VEX CTE 中的計時器可讓您追蹤經過的時間並根據時間間隔執行操作。它可用於測量某項任務所需的時間,或重設計時器以進行新的計時操作。
注意: 只有在創建了 Brain brain = Brain()用,因為所有計時器功能都是透過 Brain 上的方法驅動的。
本頁面使用 brain 作為範例大腦名稱。請根據需要將其替換為您自己配置的名稱。
以下是所有方法的清單:
timer_reset– 將定時器重設為零。timer_time– 傳回自專案開始以來經過的時間。timer_event– 在指定的毫秒數後呼叫函數,並附有可選參數。
定時器重置#
timer_reset 將計時器重設為零。這可用於對同一專案中的其他程式碼段進行計時。
Usage:
brain.timer_reset()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
if arm.is_done():
brain.timer_reset()
定時器時間#
timer_time 返回計時器目前經過的時間,單位為指定單位 MSEC 為整數, SECONDS為浮點數。
Usage:
brain.timer_time(units)
參數 |
描述 |
|---|---|
|
可選。時間單位:
|
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
if arm.is_done():
brain.timer_reset()
定時器事件#
timer_event 在指定的時間過後呼叫一個函數。
Usage:
timer_event(callback, delay, arg)
參數 |
描述 |
|---|---|
|
當定時器事件發生時要執行的函數。 |
|
函數呼叫前的延遲時間,以毫秒為單位。 |
|
可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的函數。 |
def move_arm():
arm.move_inc(0, 100, 0)
# Move the arm after a 5000 millisecond delay
timer_event(move_arm, 5000)