Temporizador#
Introducción#
El temporizador del robot de codificación VEX AIM registra el tiempo transcurrido desde el inicio de un proyecto. Puede usarse para medir duraciones, activar eventos tras un tiempo determinado o reiniciarse para nuevas operaciones de cronometraje. A continuación, se muestra una lista de todos los métodos disponibles:
Acción – Controlar el temporizador.
reset – Resets the timer to zero.
Getter – Devuelve el valor actual del temporizador.
time – Returns the elapsed time since the project started.
Devolución de llamada: activa funciones después de un retraso.
event – Calls a function after a specified number of milliseconds, with optional arguments.
Constructor: crea un temporizador para controlar el tiempo.
Timer - Create a new timer object that can be used with these methods.
Acción#
reset#
reset
pone el temporizador a cero.
Uso:
timer.reset()
Parámetros |
Descripción |
---|---|
Este método no tiene parámetros. |
# 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))
Adquiridor#
time#
time
devuelve el tiempo transcurrido actual del temporizador en las unidades especificadas: un entero para MSEC
o un flotante para SECONDS
.
Uso:
timer.time(units)
Parámetros |
Descripción |
---|---|
|
Las unidades de tiempo son milisegundos |
# 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)
Llamar de vuelta#
event#
event
llama a una función después de una cantidad de tiempo especificada.
Uso:
timer.event(callback, delay, arg)
Parámetros |
Descripción |
---|---|
|
Una función a ejecutar cuando ocurre el evento del temporizador. |
|
El retraso antes de que se llame a la función, en milisegundos. |
|
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)
Constructor#
Timer#
Se puede crear un nuevo temporizador usando el constructor Timer
. Un nuevo temporizador empieza a medir el tiempo inmediatamente después de su creación.
Uso:
Timer()
Parámetro |
Descripción |
---|---|
Este constructor no tiene parámetros. |
## 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)