Threads#

Introduction#

Threads allow the VEX AIR Drone Controller to run multiple tasks simultaneously within the same program. They enable multitasking, letting the VEX AIR Drone perform independent actions at the same time.

Note: You must first define a function to use it with a thread.

Constructor – Create and start new threads.

  • Thread – Starts a new thread that runs the specified function in parallel with the main program.

Constructor#

Thread#

Thread creates and starts a thread. When you create a thread, you can name it to manage it individually in your project.

Usage:

my_thread = Thread(my_function)

Parameters

Description

my_thread

Optional. A name for the new thread.

callback

The name of a previously defined function.

Note: A function must always be defined before it is called.

# Only play sounds when the drone moves laterally
def moving_sounds():
    while True:
        if drone.is_move_active():
            controller.sound.play(LOOPING)
            while controller.sound.is_active():
                wait(50, MSEC)
        wait(5, MSEC)

# Run moving_sounds at the same time
moving_sounds_thread = Thread(moving_sounds)

# Fly the drone using the controller
drone.take_off(climb_to=500)
while True:
    drone.move_with_vectors(
        forward=controller.axis4.position(),
        rightward=controller.axis3.position(),
        upward=controller.axis1.position(),
        rotation=controller.axis2.position()
    )
    wait(5, MSEC)