计时器#
介绍#
VEX IQ(第二代)中的计时器允许您跟踪已用时间并根据时间间隔执行操作。通过多种时间管理方法,您可以在程序中创建精确且定时的操作。
以下是所有方法的列表:
方法——控制并与大脑的计时器互动。
时间 – 返回项目开始以来经过的时间。
clear – 将计时器重置为零。
system – 返回 Brain 已通电的毫秒数。
system_high_res – 返回 Brain 已通电的微秒数。
事件 – 在指定的毫秒数后调用一个函数,并带有可选参数。
构造函数——创建额外的计时器。
计时器 – 创建一个附加计时器。
时间#
time
以指定的单位返回计时器的当前经过时间 - 整数表示 MSEC
,浮点数表示 SECONDS
。
用法:
brain.timer.time(units)
参数 |
描述 |
---|---|
|
时间单位是毫秒“MSEC”(默认)或“SECONDS”。 |
# Display the current time on the timer
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print(brain.timer.time(SECONDS))
wait(50, MSEC)
清除#
clear
将计时器设置为零。
用法:
brain.timer.clear()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Reset the timer when a button is pressed
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print(brain.timer.time(SECONDS))
wait(50, MSEC)
if brain.buttonCheck.pressing():
brain.timer.clear()
系统#
system
返回 Brain 已启动的毫秒数。
用法:
brain.timer.system()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display the time the brain has been on in milliseconds
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print(brain.timer.system())
brain.screen.print(" MSEC")
wait(50, MSEC)
系统高分辨率#
system_high_res
返回 Brain 已启动的微秒数。
用法:
brain.timer.system_high_res()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display the time the brain has been on in microseconds
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print(brain.timer.system_high_res())
wait(50, MSEC)
事件#
event
在指定的时间后调用一个函数。
用法:
brain.timer.event(回调,延迟,参数)
参数 |
描述 |
---|---|
|
当计时器事件发生时执行的函数。 |
|
函数调用前的延迟,以毫秒为单位。 |
|
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Define a function timer_event
def timer_event():
drivetrain.drive_for(FORWARD, 200, MM)
# Drive forward after a 5000 millisecond delay
brain.timer.event(timer_event, 5000)
构造函数#
构造函数用于手动创建“Timer”对象,这对于创建除“brain.timer”之外的任何其他计时器都是必需的。
对于下面的示例,配置的 Timer 将被命名为“timer_1”,并且在整个 API 文档的所有后续示例中引用“Timer”类方法时将使用它。
计时器#
Timer
创建一个新的计时器。Timer
对象会在创建后立即开始计时,并可与所有 brain.timer
方法配合使用。
用法:
Timer()
参数 |
描述 |
---|---|
此构造函数没有参数。 |
wait(2, SECONDS)
timer_1 = Timer()
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Brain: ")
brain.screen.print(brain.timer.time(SECONDS))
brain.screen.next_row()
brain.screen.print("timer_1: ")
brain.screen.print(timer_1.time(SECONDS))
wait(15, MSEC)