计时器#

介绍#

The VEX AIM Coding Robot’s timer keeps track of elapsed time from the start of a project. It can be used to measure how long something takes or reset for new timing operations.

以下是所有方法的列表:

方法 – 控制定时器。

  • reset – Resets the timer to zero.

  • time – Returns how much time has passed.

  • event – Registers a function to be called after a specified number of milliseconds.

构造函数 – 创建一个计时器来跟踪时间。

  • Timer – Create a new timer object that can be used with these methods.

方法#

reset#

reset sets the timer to zero. This can be used to time additional sections of code within the same project.

用法:

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 time since the timer was last reset as a decimal. The timer is automatically reset at the start of a project.

用法:

timer.time(units)

参数

描述

units

The unit that represents the time: MSEC (default) – milliseconds, or SECONDS

# 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 passes.

用法:

timer.event(callback, delay, arg)

参数

描述

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 creates a new timer. A Timer object will immediately begin counting the moment it is created and will work with all timer methods.

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)