Eventos#
Introducción#
Los eventos permiten ejecutar funciones en paralelo mediante objetos de evento. En lugar de llamar a funciones o subprocesos uno tras otro, los eventos permiten registrar funciones y activarlas todas a la vez. Cada función registrada se ejecuta en su propio subproceso, lo que permite que el robot realice varias acciones simultáneamente, como hacer parpadear los TouchLED y conducir.
A continuación se muestra una lista de métodos y constructores disponibles:
Event– Creates a new event object.broadcast– Triggers all registered functions in an event object to run in parallel.broadcast_and_wait– Triggers all registered functions in an event object and waits for them to finish before continuing.event– Registers a function to the event object, optionally with arguments.
Create an Event Object#
The Event constructor is used to create an event object that manages function execution in separate threads.
Usage:
Event()
Parámetro |
descripción |
|---|---|
Este constructor no tiene parámetros. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set the event
move_event = Event()
move_event(dash_lines)
# Draw the dash line while driving forward
move_event.broadcast()
drivetrain.drive_for(FORWARD, 1000, MM)
# VR threads — Do not delete
vr_thread(main)
Broadcast#
broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.
Usage:
my_event.broadcast()
parámetro |
descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set the event
move_event = Event()
move_event(dash_lines)
# Draw the dash line while driving forward
move_event.broadcast()
drivetrain.drive_for(FORWARD, 1000, MM)
# VR threads — Do not delete
vr_thread(main)
Broadcast and wait#
broadcast_and_wait starts an event but waits for all registered functions to finish before continuing with subsequent functions.
Usage:
my_event.broadcast_and_wait()
parámetro |
descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
def move_and_turn():
# Drive forward and turn
drivetrain.drive_for(FORWARD, 150, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
def main():
# Wait for the event to finish after 3 seconds
move_event = Event()
move_event(move_and_turn)
while True:
if brain.timer_time(SECONDS) > 3:
move_event.broadcast_and_wait()
break
wait(5, MSEC)
brain.print("Movement done.")
# VR threads — Do not delete
vr_thread(main)
Register Functions to an Event#
Cuando se registra una función en un evento, se ejecutará en un hilo separado cuando se transmita el evento.
Usage:
event(callback, args)
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
|
Una función que se define previamente para ejecutarse cuando se transmite el evento. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set and call the event and start driving
move_event = Event()
move_event(dash_lines)
move_event.broadcast()
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)