定時器#

介紹#

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

以下是所有方法的清單:

方法 – 控制計時器並與之互動。

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

  • time – 傳回經過的時間。

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

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

重置#

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

Usage:
timer.reset()

參數

描述

此方法沒有參數。

# Reset the timer when the Move button is pressed
while True:
    console.clear()
    console.print(timer.time(SECONDS))
    wait(50, MSEC)
    if robot.get_button(MOVE):
        timer.reset()

時間#

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

Usage:
timer.time(units)

參數

描述

units

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

# Display the time until the Move button is pressed
while True:
    console.clear()
    console.print(timer.time(SECONDS))
    wait(50, MSEC)
    if robot.get_button(MOVE):
        timer.reset()

建構函數#

Timer#

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

Usage:
my_timer = Timer()

參數

描述

此建構函數沒有參數。

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