Eventos#
Introducción#
Events allow you to run functions in parallel. Instead of calling functions sequentially, you can register multiple functions to an event and trigger them all at once. Each registered function runs in its own thread, enabling your robot to perform multiple tasks simultaneously—like displaying values on the screen while driving.
Para crear y luego utilizar un Evento, siga estos pasos en orden:
Create the event with
Event.Register a function to trigger when the
Eventis broadcast usingevent.Broadcast an
Event:broadcast– Triggers all registered functions in an event object to run concurrently.broadcast_and_wait– Triggers all registered functions in an event object and waits for them to finish before continuing.
Crear un evento#
Event creates an event object that manages function execution in separate threads.
Usage:
Event()
Parámetro |
Descripción |
|---|---|
Este constructor no tiene parámetros. |
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
# *** Creating the move_event Event ***
move_event = Event()
# Register move_square to move_event
move_event(move_square)
# Call all functions registered to move_event
move_event.broadcast()
Registrar funciones en un evento#
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. |
|
Opcional. Una tupla que contiene los argumentos que se pasan a la función de devolución de llamada. Consulte Uso de funciones con parámetros para obtener más información. |
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
# Creating the move_event Event
move_event = Event()
# *** Register move_square to move_event ***
move_event(move_square)
# Call all functions registered to move_event
move_event.broadcast()
Transmitir un evento#
broadcast#
broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.
Usage:
event.broadcast()
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
# Build Used: Super Code Base 2.0
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
move_event = Event()
move_event(move_square)
# Broadcasting the move_event Event
move_event.broadcast()
optical_sensor.set_light_power(100, PERCENT)
broadcast_and_wait#
broadcast_and_wait starts an event but waits for all registered functions to finish before continuing with subsequent functions.
Usage:
event.broadcast_and_wait()
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
# Set up and call the move_square event
move_event = Event()
move_event(move_square)
# Broadcast the move_event Event and wait
move_event.broadcast_and_wait()
optical_sensor.set_light_power(100, PERCENT)