計時器#
介紹#
VEX AIR 無人機控制器的計時器會記錄工程開始以來經過的時間。它可以用來測量某項任務所需的時間,也可以重設計時器以進行新的計時操作。
以下是所有可用方法的清單:
動作-控制計時器。
reset– 將計時器重設為零。
Getter – 報告目前計時器值。
time– 傳回經過的時間。
回呼-延遲後觸發函數。
event– 註冊一個在指定毫秒數後呼叫的函數。
建構函式-建立一個計時器來追蹤時間。
Timer- 建立一個新的計時器對象,該物件可以與這些方法一起使用。
行動#
reset#
reset 將計時器設為零。這可用於對同一專案中的其他程式碼段進行計時。
用法:
timer.reset()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display the time the drone takes to climb upwards.
drone.take_off(500)
timer.reset()
drone.climb_for(UP, 500)
controller.screen.print("Time elapsed: ")
controller.screen.print(timer.time(SECONDS))
drone.land()
吸氣劑#
time#
time 傳回自上次計時器重設以來經過的時間,以十進位數表示。計時器會在專案開始時自動重設。
用法:
timer.time(units)
參數 |
描述 |
|---|---|
|
時間單位: |
# Display the time in two units.
while True:
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Time: {} ms".format(timer.time(MSEC)))
controller.screen.next_row()
controller.screen.print("Time: {:.2f} s".format(timer.time(SECONDS)))
wait(50, MSEC)
打回來#
event#
事件event
用法:
timer.event(callback, delay, arg)
參數 |
描述 |
|---|---|
|
計時器事件發生時執行的函數。 |
|
函數呼叫前的延遲,以毫秒為單位。 |
|
可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的事件。 |
# Create a function to play a looping sound.
def on_timer():
controller.sound.play(LOOPING)
# Play the sound after 4 seconds (4000 mS).
timer.event(on_timer, 4000)
建構函數#
Timer#
Timer 創造一個新的計時器。 2 Timer 物件會在創建後立即開始計時,並且可以與所有 timer 方法一起使用。
用法:
Timer()
範圍 |
描述 |
|---|---|
此建構函數沒有參數。 |
# Display 2 timers with a 2 second difference.
wait(2, SECONDS)
stopwatch = Timer()
while True:
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Timer: ")
controller.screen.print(timer.time(SECONDS))
controller.screen.set_cursor(4, 1)
controller.screen.print("Stopwatch: ")
controller.screen.print(stopwatch.time(SECONDS))
wait(0.1, SECONDS)