线程#

介绍#

线程允许 VEX AIR 无人机控制器在同一程序中同时运行多个任务。它们支持多任务处理,使 VEX AIR 无人机能够同时执行独立的操作。

**注意:**您必须[首先定义一个函数](Functions.md)才能将其与线程一起使用。

构造函数——创建并启动新线程。

  • 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 manage it individually in your project.

用法:

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)