執行緒#

介紹#

執行緒允許 VEX AIR 無人機控制器在同一程式中同時執行多個任務。它們支援多工處理,使 VEX AIR 無人機能夠同時執行獨立的操作。

注意: 您必须首先定义一个函数才能将其与线程一起使用。

建構函式-建立並啟動新執行緒。

  • Thread – 啟動一個新線程,該線程與主程式並行運行指定的函數。

建構函數#

Thread#

Thread 創建並啟動一個線程。建立線程時,您可以為其命名,以便在不再需要時將其停止。

用法:

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)