计时器#
介绍#
The Timer in VEX IQ (2nd Gen) allows you to track elapsed time and perform actions based on time intervals. With various methods for time management, you can create precise and timed operations within your program.
Below is a list of all methods:
Methods – Control and interact with the Brain’s timer.
时间 – 返回项目开始以来经过的时间。
clear – Resets the timer to zero.
system – Returns the number of milliseconds the Brain has been powered on.
system_high_res – Returns the number of microseconds the Brain has been powered on.
事件 – 在指定的毫秒数后调用一个函数,并带有可选参数。
Constructor – Create additional timers.
Timer – Creates an additional timer.
时间#
time
以指定的单位返回计时器的当前经过时间 - 整数表示 MSEC
,浮点数表示 SECONDS
。
Usage:
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#
clear
sets the timer to zero.
Usage:
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#
system
returns the number of milliseconds the Brain has been powered on.
Usage:
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#
system_high_res
returns the number of microseconds the Brain has been powered on.
Usage:
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
在指定的时间后调用一个函数。
Usage:
brain.timer.event(callback, delay, arg)
参数 |
描述 |
---|---|
|
当计时器事件发生时执行的函数。 |
|
函数调用前的延迟,以毫秒为单位。 |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
# 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)
Constructors#
Constructors are used to manually create Timer
objects, which are necessary for creating any additional timers besides brain.timer
.
For the examples below, the configured Timer will be named timer_1
and will be used in all subsequent examples throughout this API documentation when referring to Timer
class methods.
计时器#
Timer
creates a new timer. A Timer
object will immediately begin counting the moment it is created and will work with all brain.timer
methods.
用法:
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)