计时器#

介绍#

VEX AIR 无人机控制器的计时器会记录项目开始以来经过的时间。它可以用来测量某项任务所需的时间,也可以重置计时器以进行新的计时操作。

以下是所有可用方法的列表:

动作——控制计时器。

  • reset – Resets the timer to zero.

Getter – 报告当前计时器值。

  • 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.

Usage:
timer.reset()

参数

描述

该方法没有参数。

# Display the time the drone takes to climb upwards.
drone.take_off(500)
timer.reset()
drone.climb_for(UP, 500)
controller.screen.print("Time elapsed: ")
controller.screen.print(timer.time(SECONDS))
drone.land()

吸气剂#

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.

Usage:
timer.time(units)

参数

描述

units

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

# Display the time in two units.
while True:
    controller.screen.clear_screen()
    controller.screen.set_cursor(1, 1)
    controller.screen.print("Time: {} ms".format(timer.time(MSEC)))
    controller.screen.next_row()
    controller.screen.print("Time: {:.2f} s".format(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 looping sound.
def on_timer():
    controller.sound.play(LOOPING)

# Play the sound after 4 seconds (4000 mS).
timer.event(on_timer, 4000)

构造函数#

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:
    controller.screen.clear_screen()

    controller.screen.set_cursor(1, 1)
    controller.screen.print("Timer: ")
    controller.screen.print(timer.time(SECONDS))

    controller.screen.set_cursor(4, 1)
    controller.screen.print("Stopwatch: ")
    controller.screen.print(stopwatch.time(SECONDS))

    wait(0.1, SECONDS)