线程#

简介#

线程允许 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)