Νήματα#

Εισαγωγή#

Τα νήματα επιτρέπουν στον ελεγκτή VEX AIR Drone να εκτελεί πολλαπλές εργασίες ταυτόχρονα μέσα στο ίδιο πρόγραμμα. Επιτρέπουν την εκτέλεση πολλαπλών εργασιών, επιτρέποντας στο VEX AIR Drone να εκτελεί ανεξάρτητες ενέργειες ταυτόχρονα.

Σημείωση: Πρέπει πρώτα να ορίσετε μια συνάρτηση για να τη χρησιμοποιήσετε με ένα νήμα.

Κατασκευαστής – Δημιουργήστε και ξεκινήστε νέα νήματα.

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

Κατασκευαστής#

Thread#

Thread creates and starts a thread. When you create a thread, you can name it to stop it when it is no longer needed.

Χρήση:

my_thread = Thread(my_function)

Παράμετροι

Περιγραφή

my_thread

Προαιρετικό. Ένα όνομα για το νέο νήμα.

callback

Το όνομα μιας προηγουμένως ορισμένης συνάρτησης.

Σημείωση: Μια συνάρτηση πρέπει πάντα να ορίζεται πριν την κλήση της.

# 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)