タイマー#
導入#
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 は新しいタイマーを作成します。 Timer オブジェクトは作成された瞬間にカウントを開始し、すべての timer メソッドで動作します。
Usage:
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)