控制器#
介绍#
VEX AIR 无人机控制器采用 12 个按钮布局和两个操纵杆。这些输入使无人机能够检测按钮按下和操纵杆移动,从而实现交互式和灵敏的控制。
以下是所有可用方法的列表:
Getters – 读取按钮、操纵杆和连接状态。
.pressing
– Checks if a specific button is currently being pressed..position
– Returns the position of the joystick along a specified axis.is_drone_connected
– Returns whether or not the controller is connected to the drone.get_battery_level
– Returns the controller’s battery level.
回调——响应按钮或操纵杆输入变化。
吸气剂#
.pressing#
.pressing
returns if a specific controller button is being pressed. This method must be called on a specific button, such as button5
(see full list of buttons below). This will return either of the following:
True
- 指定的按钮正在被按下。False
- 指定的按钮未被按下。
用法:
八个可用按钮(编号 5 至 12)之一可用于此方法,如下所示:
按钮 |
命令 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
# Take a picture when button 5 is pressed.
drone.take_off(climb_to=500)
while True:
# Fly using the controller sticks
drone.move_with_vectors(
forward=controller.axis4.position(),
rightward=controller.axis3.position(),
upward=controller.axis1.position(),
rotation=controller.axis2.position()
)
# Capture an image from the forward camera when button 5 is pressed.
if controller.button5.pressing():
drone.camera.capture_image(FORWARD_CAMERA)
wait(5, MSEC)
.position#
.position
returns the position of the joystick’s specified axis as a percentage. This will return an integer from –100 to 100.
用法:
此方法可使用四个可用轴中的一个,编号为 1 至 4。
轴 |
命令 |
---|---|
|
|
|
|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
# Climb when the left joystick is moved up.
drone.take_off(climb_to=500)
while True:
if controller.axis1.position() > 0:
drone.climb(direction=UP, velocity=50)
else:
drone.hover()
wait(5, MSEC)
is_drone_connected#
is_drone_connected
returns whether the drone is connected. This will return a Boolean value:
True
- The drone is connected.False
- The drone is not connected.
用法:
controller.is_drone_connected()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Print controller connection status on screen.
while True:
if controller.is_drone_connected():
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Controller connected")
wait(0.5, SECONDS)
get_battery_level#
get_battery_level
returns the Controller’s battery level as a percentage. This returns an integer from 0 to 100.
用法:
controller.get_battery_level()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Show controller's battery level.
if controller.get_battery_level() > 50:
controller.screen.print("Battery level ok")
else:
controller.screen.print("Battery level low")
回调#
.pressed#
.pressed
registers a function to be called when a specific button on the controller is pressed. This method must be called on a specific button, such as button5
(see full list of buttons below).
用法:
此方法可使用八个可用按钮之一,编号为 5 至 12。
按钮 |
命令 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
---|---|
|
先前定义的 函数,当按下指定的按钮时执行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# Take a picture when button 5 is pressed.
def take_picture():
drone.camera.capture_image(FORWARD_CAMERA)
# Register the button press callback.
controller.button5.pressed(take_picture)
# Fly using the controller sticks.
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)
.released#
.released
注册一个函数,当控制器上的特定按钮被释放时调用。此方法必须在特定按钮上调用,例如 button5
(请参阅下面的完整按钮列表)。
用法:
此方法可使用八个可用按钮之一,编号为 5 至 12。
按钮 |
命令 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
---|---|
|
先前定义的 函数,当释放指定按钮时执行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。 |
# Take a picture when button 5 is released.
def take_picture():
drone.camera.capture_image(FORWARD_CAMERA)
# Register the button released callback.
controller.button5.released(take_picture)
# Fly using the controller sticks.
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)
.changed#
.changed
registers a function to be called when the joystick’s position changes.
用法:
此方法可使用四个可用轴中的一个,编号为 1 至 4。
轴 |
命令 |
---|---|
|
|
|
|
|
|
|
|
参数 |
描述 |
---|---|
|
先前定义的 函数,当轴的值发生变化时执行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。 |
# Move forward when the left joystick moves.
def on_axis1_changed():
drone.move_for(direction=0, distance=200, velocity=50, units=MM)
drone.take_off(climb_to=500)
controller.axis1.changed(on_axis1_changed)