计时器#

介绍#

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 将计时器设置为零。

用法:

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 以指定的单位返回计时器的当前经过时间 - 整数表示 MSEC,浮点数表示 SECONDS

用法:

timer.time(units)

参数

描述

units

时间单位是毫秒MSEC(默认)或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 在指定的时间后调用一个函数。

用法:

timer.event(callback, delay, arg)

参数

描述

callback

当计时器事件发生时执行的函数。

delay

函数调用前的延迟,以毫秒为单位。

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# 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()

范围

描述

此构造函数没有参数。

## 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)