定時器#
介紹#
VEX AIM 編程機器人的計時器會記錄工程開始以來經過的時間。它可以用來測量某項任務所需的時間,也可以重設計時器以進行新的計時操作。
以下是所有方法的清單:
方法 – 控制定時器。
建構函式 – 建立一個計時器來追蹤時間。
Timer– 建立一個新的計時器對象,該物件可以與這些方法一起使用。
方法#
reset#
reset 將計時器設為零。這可用於對同一專案中的其他程式碼段進行計時。
用法:
timer.reset()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display how long a turn takes
robot.move_for(100, 0)
timer.reset()
robot.turn_for(RIGHT, 90)
robot.screen.print("Time elapsed:")
robot.screen.next_row()
robot.screen.print("%d seconds" % timer.time(SECONDS))
time#
time 傳回自上次計時器重設以來經過的時間,以十進位數表示。計時器會在專案開始時自動重設。
用法:
timer.time(units)
參數 |
描述 |
|---|---|
|
時間單位: |
# Print the time in seconds
# and in milliseconds on the next row.
while timer.time(SECONDS) <= 5:
robot.screen.clear_screen()
robot.screen.set_cursor(1, 1)
robot.screen.print("Time: %d" % timer.time(MSEC))
robot.screen.next_row()
robot.screen.print("Time: %.2f" % timer.time(SECONDS))
wait(50, MSEC)
event#
事件event
用法:
timer.event(callback, delay, arg)
參數 |
描述 |
|---|---|
|
當定時器事件發生時要執行的函數。 |
|
函數呼叫前的延遲時間,以毫秒為單位。 |
|
可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的事件。 |
# Create a function to play a sparkle sound
def timer_callback():
robot.sound.play(SPARKLE)
# Call the function after 2000 milliseconds (2 seconds)
timer.event(timer_callback, 2000)
建構函數#
Timer#
Timer 創造一個新的計時器。 2 Timer 物件會在創建後立即開始計時,並且可以與所有 timer 方法一起使用。
用法:
Timer()
範圍 |
描述 |
|---|---|
此建構函數沒有參數。 |
## Display 2 timers with a 2 second difference
wait(2, SECONDS)
stopwatch = Timer()
while True:
robot.screen.clear_screen()
robot.screen.set_cursor(1, 1)
robot.screen.print("Timer:")
robot.screen.set_cursor(2, 1)
robot.screen.print(timer.time(SECONDS))
robot.screen.set_cursor(4, 1)
robot.screen.print("Stopwatch:")
robot.screen.set_cursor(5, 1)
robot.screen.print(stopwatch.time(SECONDS))
wait(0.1, SECONDS)