控制器#
介绍#
单摇杆控制器采用 4 键布局,并配备一个摇杆,摇杆既可用作模拟输入,又可用作可按压按钮。这些输入使机器人能够检测按钮按下和摇杆移动,从而实现交互式、灵敏的控制。以下列出了所有可用方法:
Getters – 读取按钮、操纵杆和连接状态。
.pressing– Returns whether the specified button is being pressed..position– Returns the position of the joystick’s specified axis.is_connected– Returns whether the controller is connected.get_battery_level– Returns the controller’s battery level as a percentage.
回调——响应按钮或操纵杆输入变化。
吸气剂#
.pressing#
.pressing returns an integer indicating whether a specific button on the controller is currently being pressed. This method must be called on a specific button object, such as button_up (see full list of button objects below).
1- The specified button is being pressed.0- The specified button is not being pressed.
用法:
五个可用按钮对象之一可与此方法一起使用,如下所示:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Move forwards while the Up button is being pressed
while True:
if controller.button_up.pressing():
robot.move_at(0)
else:
robot.stop_all_movement()
.position#
.position returns the position of the joystick’s specified axis as an integer from –100 to 100, representing a percentage.
用法:
此方法可使用两个可用轴之一,如下所示:
轴 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
该方法没有参数。 |
while True:
if controller.axis1.position() > 0:
# Move forward when the joystick is moved up
robot.move_at(0)
else:
# Stop moving when the joystick is centered
robot.stop_all_movement()
is_connected#
is_connected returns a Boolean indicating whether the Controller is connected.
True- The Controller is connected.False- The Controller is not connected.
用法:
controller.is_connected()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Start moving forward.
# When the Controller disconnects, the robot stops.
robot.move_at(0)
while True:
if not controller.is_connected():
robot.stop_all_movement()
wait(100, MSEC)
get_battery_level#
get_battery_level returns the Controller’s battery level as an integer from 0 to 100, representing a percentage.
用法:
controller.get_battery_level()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Display the Controller's battery level.
robot.screen.print(controller.get_battery_level())
回调#
.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 object, such as button_up – (see full list of button objects below).
用法:
五个可用按钮对象之一可与此方法一起使用,如下所示:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
先前定义的在按下指定按钮时执行的 函数。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Kick hard when Up button is pressed
def kick_object():
robot.kicker.kick(HARD)
controller.button_up.pressed(kick_object)
.released#
.released registers a function to be called when a specific button on the controller is released. This method must be called on a specific button object, such as button_up – (see full list of button objects below).
用法:
五个可用按钮对象之一可与此方法一起使用,如下所示:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
先前定义的在释放指定按钮时执行的 函数。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Clear the text after the Up button is released
def clear_screen():
robot.screen.clear_screen(BLUE)
controller.button_up.released(clear_screen)
robot.screen.print("Press Up, then")
robot.screen.next_row()
robot.screen.print("Release Up!")
.changed#
.changed registers a function to be called when the joystick’s position changes.
用法:
此方法可以使用两个可用轴之一,如下所示:
轴 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Function to display an emoji when the joystick is moved
def move_joystick():
robot.screen.show_emoji(CONFUSED)
# Run the function when the joystick is moved up or down
controller.axis1.changed(move_joystick)