计时器#
介绍#
VEX AIR 无人机控制器的计时器会跟踪项目从开始到结束的耗时。它可以用来测量持续时间、在设定时间后触发事件,或重置以进行新的计时操作。
以下是所有可用方法的列表:
动作——控制计时器。
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.
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 current elapsed time of the timer in the specified units — an integer for MSEC
or a float for SECONDS
.
Usage:
timer.time(units)
参数 |
描述 |
---|---|
|
The units that represent the time:
|
# 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.
用法:
timer.event(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#
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:
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)