计时器#
介绍#
VEX AIM 编程机器人的计时器会记录项目从开始到结束的耗时。它可以用来测量持续时间、在设定时间后触发事件,或重置以进行新的计时操作。以下是所有可用方法的列表:
动作——控制计时器。
reset– Resets the timer to zero.
Getter – 返回当前计时器值。
time– Returns the elapsed time since the project started.
回调——延迟后触发函数。
event– Calls a function after a specified number of milliseconds, with optional arguments.
构造函数——创建一个计时器来跟踪时间。
Timer- Create a new timer object that can be used with these methods.
行动#
reset#
reset sets the timer to zero.
用法:
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 returns the current elapsed time of the timer in the specified units — an integer for MSEC or a float for SECONDS.
用法:
timer.time(units)
参数 |
描述 |
|---|---|
|
The time units are milliseconds |
# 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 calls a function after a specified amount of time.
用法:
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#
A new timer can be created using the Timer constructor. A new timer starts measuring time immediately when it is created.
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)