#

简介#

VEX AIM Python 宏是预先构建的代码块,可以拖放到 Python 项目中。每个宏都是一组 Python 命令,它们协同工作,使机器人完成更复杂的动作。

这些并非可从代码中调用的单个方法。相反,每个宏都作为现成的代码序列插入,可以直接使用,也可以添加到项目后进行编辑。

在此页面上,每个 Python 代码块都显示了使用该宏时插入的实际代码序列。

以下是所有宏的列表:

情感——让机器人表现出丰富的情感。

  • [act happy] (#act-happy) —让机器人表现得很开心。

  • [act sad] (#act-sad) —让机器人变得悲伤。

  • [act silly] (#act-silly) —让机器人装傻。

  • [act angry] (#act-angry) —让机器人动作愤怒。

  • [act excited] (#act-excited) —让机器人表现得很兴奋。

AI视觉(运动球类)——转向或收集运动球类。

  • [turn right until sports ball] (#turn-right-until-sports-ball) —向右转,直到检测到运动球。

  • turn left until sports ball — 向左转直到检测到运动球。

  • [get sports ball] (#get-sports-ball) —移动机器人以收集运动球。

AI视觉(橙色桶)——转向或收集橙色桶。

  • turn right until orange barrel — 向右转直到检测到橙色桶。

  • [turn left until orange barrel] (#turn-left-until-orange-barrel) —向左转直到检测到橙色桶。

  • get orange barrel — 移动机器人去收集橙色桶。

AI视觉(蓝色桶)——转向或收集蓝色桶。

AI视觉(AIM机器人)——转向或向其他AIM机器人移动。

  • [turn right until AIM robot] (#turn-right-until-aim-robot) —向右转,直到检测到另一个AIM机器人。

  • [turn left until AIM robot] (#turn-left-until-aim-robot) —向左转,直到检测到另一个AIM机器人。

  • [move to AIM robot] (#move-to-aim-robot) —向另一个AIM机器人移动。

AI视觉(AprilTag)——转向或靠近AprilTags。

  • [turn right until AprilTag] (#turn-right-until-apriltag) —向右转,直到检测到AprilTag。

  • [turn left until AprilTag] (#turn-left-until-apriltag) —向左转,直到检测到AprilTag。

  • [move to AprilTag] (#move-to-apriltag) —移向AprilTag。

情绪#

act happy#

这个宏可以让机器人表现得开心,包括显示笑脸、播放欢快的声音、变换 LED 灯颜色以及来回移动。当机器人需要以快乐或积极的方式回应时,可以使用它。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# act happy
robot.screen.show_emoji(HAPPY)
wait(.5,SECONDS)
robot.sound.play(ACT_HAPPY)
wait(115, MSEC)

robot.screen.show_emoji(HAPPY, LOOK_RIGHT)
robot.led.on(ALL_LEDS, YELLOW)
robot.turn_for(RIGHT, 4, 100)
robot.led.on(ALL_LEDS, WHITE)
robot.screen.show_emoji(HAPPY, LOOK_LEFT)
robot.turn_for(RIGHT, -8, 100)
robot.led.on(ALL_LEDS, YELLOW)
robot.screen.show_emoji(HAPPY, LOOK_RIGHT)
robot.turn_for(RIGHT, 4, 100)

wait(100,MSEC)
for i in range(3):
    robot.screen.show_emoji(QUIET)
    robot.led.on(ALL_LEDS, WHITE)
    robot.move_for(1,-90)
    robot.screen.show_emoji(HAPPY)
    robot.led.on(ALL_LEDS, YELLOW)
    robot.move_for(1,90)

robot.stop_all_movement()
robot.led.off(ALL_LEDS)
wait(.5,SECONDS)

act sad#

这个宏可以让机器人表现出悲伤的表情,包括显示悲伤的表情、播放悲伤的声音、LED灯逐渐变暗以及缓慢地朝不同方向移动。当机器人需要表现出失望或低落的情绪时,可以使用它。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# act sad
robot.screen.show_emoji(SAD)
wait(.5,SECONDS)
robot.sound.play(ACT_SAD)
wait(115, MSEC)

robot.led.on(ALL_LEDS, Color(0, 0, 250))
robot.screen.show_emoji(SAD, LOOK_RIGHT)
robot.move_for(11, 135, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 100))
robot.screen.show_emoji(SAD, LOOK_FORWARD)
robot.move_for(11, 315, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 50))
robot.screen.show_emoji(SAD, LOOK_LEFT)
robot.move_for(11, 225, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 10))
robot.screen.show_emoji(SAD, LOOK_FORWARD)
robot.move_for(11, 45, 70)
wait(.5,SECONDS)
robot.led.off(ALL_LEDS)

act silly#

这个宏可以让机器人做出滑稽的动作,比如旋转、变换表情和闪烁不同颜色的LED灯。当机器人需要做出俏皮或出人意料的反应时,可以使用它。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# act silly
robot.screen.show_emoji(SILLY)
wait(.5,SECONDS)
robot.sound.play(ACT_SILLY)
wait(115,MSEC)

robot.turn_for(RIGHT, 360, 100, wait=False)
face_list = [SILLY, HAPPY, EXCITED, PROUD, THRILLED, LAUGHING]
colors = [BLUE, CYAN, GREEN, ORANGE, PURPLE, RED, WHITE, YELLOW]

while robot.is_turn_active():
    robot.screen.show_emoji(SILLY)
    robot.led.on(ALL_LEDS, random.choice(colors))
    wait(110,MSEC)
    robot.screen.show_emoji(random.choice(face_list))
    wait(110,MSEC)

robot.screen.show_emoji(SILLY)
robot.led.off(ALL_LEDS)
wait(.5,SECONDS)

act angry#

这个宏可以让机器人做出生气的反应,包括显示愤怒的表情、播放愤怒的声音、将LED灯变成红色以及快速移动。当机器人需要表现出沮丧或警告的反应时,可以使用它。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# act angry
robot.screen.show_emoji(ANGRY)
wait(.5,SECONDS)
robot.sound.play(ACT_ANGRY)
wait(115,MSEC)

robot.led.on(ALL_LEDS, RED)
robot.move_for(25, 0, 50)
wait(50,MSEC)

for i in range(5):
    robot.screen.show_emoji(ANNOYED)
    robot.move_for(5, 180, 50)
    robot.screen.show_emoji(ANGRY)
    wait(50,MSEC)

wait(.5,SECONDS)
robot.led.off(ALL_LEDS)

act excited#

这个宏可以让机器人表现得兴奋,包括露出兴奋的表情、播放兴奋的声音、快速左右移动以及闪烁橙色LED灯。当机器人需要庆祝或做出充满活力的反应时,可以使用它。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# act excited
robot.screen.show_emoji(EXCITED)
wait(.5,SECONDS)
robot.sound.play(ACT_EXCITED)
wait(115, MSEC)

for angle in [3,-3,2,-2,2,-2,2,-2,1,-1]:
    if angle > 0:
        robot.screen.show_emoji(EXCITED, LOOK_RIGHT)
        robot.led.on(LED4, ORANGE)
        robot.led.on(LED5, ORANGE)
        robot.led.on(LED6, ORANGE)
        robot.led.off(LED1)
    else:
        robot.screen.show_emoji(EXCITED, LOOK_LEFT)
        robot.led.on(LED1, ORANGE)
        robot.led.on(LED2, ORANGE)
        robot.led.on(LED3, ORANGE)
        robot.led.off(LED6)
    robot.turn_for(RIGHT, angle)
    robot.led.off(ALL_LEDS)

robot.screen.show_emoji(EXCITED, LOOK_FORWARD)
wait(.5,SECONDS)

AI视觉(运动球)#

turn right until sports ball#

此宏命令会让机器人向右转弯,直到AI视觉传感器检测到运动球,然后机器人转向检测到的物体。当机器人需要在靠近或捡起运动球之前搜索它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn right until sports ball is detected
robot.turn(RIGHT)
while True:
    vision_data = robot.vision.get_data(SPORTS_BALL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

turn left until sports ball#

此宏命令会让机器人向左转弯,直到AI视觉传感器检测到运动球,然后机器人转向检测到的物体。当机器人需要在靠近或捡起运动球之前搜索它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn left until sports ball is detected
robot.turn(LEFT)
while True:
    vision_data = robot.vision.get_data(SPORTS_BALL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

get sports ball#

此宏命令会让机器人向检测到的运动球移动,直到机器人捡起该球为止。请在机器人能够看到运动球之后,或在使用“转向直到找到运动球”宏找到运动球之后使用此宏命令。

注意: 如果机器人难以检测物体,请尝试使用 set_move_velocityset_turn_velocity 降低机器人的移动和转向速度。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Get sports ball
while True:
    vision_data = robot.vision.get_data(SPORTS_BALL)

    if vision_data[0].exists:
        if robot.has_sports_ball():
            robot.stop_all_movement()
            break
        else:
            robot.move_at(vision_data[0].bearing)
    else:
        robot.move_at(0)
    wait(20, MSEC)

AI视觉(橙色桶)#

turn right until orange barrel#

此宏命令会让机器人向右转弯,直到AI视觉传感器检测到橙色桶,然后机器人转向检测到的物体。当机器人需要在靠近或收集橙色桶之前搜索它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn right until orange barrel is detected
robot.turn(RIGHT)
while True:
    vision_data = robot.vision.get_data(ORANGE_BARREL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

turn left until orange barrel#

此宏命令会让机器人向左转弯,直到AI视觉传感器检测到橙色桶,然后机器人转向检测到的物体。当机器人需要先搜索橙色桶,然后再靠近或收集它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn left until orange barrel is detected
robot.turn(LEFT)
while True:
    vision_data = robot.vision.get_data(ORANGE_BARREL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

get orange barrel#

此宏命令会让机器人朝着检测到的橙色桶移动,直到机器人拾取到它为止。请在机器人能够看到橙色桶之后,或者在使用“转向直到找到橙色桶”宏找到橙色桶之后使用此宏。

注意: 如果机器人难以检测物体,请尝试使用 set_move_velocityset_turn_velocity 降低机器人的移动和转向速度。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Get orange barrel
while True:
    vision_data = robot.vision.get_data(ORANGE_BARREL)

    if vision_data[0].exists:
        if robot.has_orange_barrel():
            robot.stop_all_movement()
            break
        else:
            robot.move_at(vision_data[0].bearing)
    else:
        robot.move_at(0)
    wait(20, MSEC)

AI视觉(蓝桶)#

turn right until blue barrel#

此宏命令会让机器人向右转弯,直到AI视觉传感器检测到蓝色桶,然后机器人转向检测到的物体。当机器人需要先搜索蓝色桶,然后再靠近或收集它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn right until blue barrel is detected
robot.turn(RIGHT)
while True:
    vision_data = robot.vision.get_data(BLUE_BARREL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

turn left until blue barrel#

此宏命令会让机器人向左转弯,直到AI视觉传感器检测到蓝色桶,然后机器人转向检测到的物体。当机器人需要先搜索蓝色桶,然后再靠近或收集它时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn left until blue barrel is detected
robot.turn(LEFT)
while True:
    vision_data = robot.vision.get_data(BLUE_BARREL)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

get blue barrel#

此宏命令会让机器人朝着检测到的蓝色桶移动,直到收集到桶为止。请在机器人能够看到蓝色桶之后,或在使用“转向直到找到蓝色桶”宏找到蓝色桶之后使用此宏。

注意: 如果机器人难以检测物体,请尝试使用 set_move_velocityset_turn_velocity 降低机器人的移动和转向速度。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Get blue barrel
while True:
    vision_data = robot.vision.get_data(BLUE_BARREL)

    if vision_data[0].exists:
        if robot.has_blue_barrel():
            robot.stop_all_movement()
            break
        else:
            robot.move_at(vision_data[0].bearing)
    else:
        robot.move_at(0)
    wait(20, MSEC)

AI视觉(AIM机器人)#

turn right until AIM robot#

此宏命令会使机器人向右旋转,直到AI视觉传感器检测到另一个VEX AIM编码机器人,然后机器人转向该机器人。当机器人需要先搜索其他机器人才能靠近时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn right until AIM robot is detected
robot.turn(RIGHT)
while True:
    vision_data = robot.vision.get_data(AIM_ROBOT)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

turn left until AIM robot#

此宏命令会使机器人向左转弯,直到AI视觉传感器检测到另一个VEX AIM编码机器人,然后机器人转向该机器人。当机器人需要先搜索其他机器人才能靠近时,可以使用此宏命令。

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn left until AIM robot is detected
robot.turn(LEFT)
while True:
    vision_data = robot.vision.get_data(AIM_ROBOT)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

move to AIM robot#

此宏命令使机器人向另一台 VEX AIM 编码机器人移动,直到距离足够近可以停止。请在机器人能够看到另一台机器人之后,或在使用“转向直到找到 AIM 机器人”宏找到目标机器人之后使用此宏命令。

注意: 如果机器人难以检测物体,请尝试使用 set_move_velocityset_turn_velocity 降低机器人的移动和转向速度。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Move to AIM robot
while True:
    vision_data = robot.vision.get_data(AIM_ROBOT)

    if vision_data[0].exists:
        if vision_data[0].width >= 140:
            robot.stop_all_movement()
            break
        else:
            robot.move_at(vision_data[0].bearing)
    else:
        robot.move_at(0)
    wait(20, MSEC)

人工智能视觉(AprilTag)#

turn right until AprilTag#

此宏让机器人向右转,直到 AI 视觉传感器检测到 AprilTag ID 0,然后转向检测到的 AprilTag。当机器人需要先搜索 AprilTag 再向它移动时使用。要更改机器人转向的目标 AprilTag,请将 0 中的 TAG0 替换为 037 之间的任意数字(AprilTag 5 到 37 可与 [AIM Printables]中的打印 AprilTag 配合使用(https://kb.vex.com/hc/en-us/articles/4410295384980-VEX-Printables#aim-printables-header-9) 。)

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn right until AprilTag ID 0 is detected
robot.turn(RIGHT)
while True:
    vision_data = robot.vision.get_data(TAG0)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

turn left until AprilTag#

此宏让机器人向左转,直到 AI 视觉传感器检测到 AprilTag ID 0,然后转向检测到的 AprilTag。当机器人需要先搜索 AprilTag 再向它移动时使用。要更改机器人转向的目标 AprilTag,请将 0 中的 TAG0 替换为 037 之间的任意数字(AprilTag 5 到 37 可与 [AIM Printables] 中的打印 AprilTag 配合使用(https://kb.vex.com/hc/en-us/articles/4410295384980-VEX-Printables#aim-printables-header-9)。)

Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Turn left until AprilTag ID 0 is detected
robot.turn(LEFT)
while True:
    vision_data = robot.vision.get_data(TAG0)

    if vision_data[0].exists:
        # Turn to the object by adding your current heading and the vision bearing offset
        robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
        break
    wait(20, MSEC)

move to AprilTag#

此宏让机器人向右转,直到 AI 视觉传感器检测到 AprilTag ID 0,然后转向检测到的 AprilTag。当机器人需要先搜索 AprilTag 再向它移动时使用。要更改机器人转向的目标 AprilTag,请将 0 中的 TAG0 替换为 037 之间的任意数字(AprilTag 5 到 37 可与 [AIM Printables]中的打印 AprilTag 配合使用(https://kb.vex.com/hc/en-us/articles/4410295384980-VEX-Printables#aim-printables-header-9) 。)

注意: 如果机器人难以检测物体,请尝试使用 set_move_velocityset_turn_velocity 降低机器人的移动和转向速度。

以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。

# Move to AprilTag ID 0
while True:
    vision_data = robot.vision.get_data(TAG0)

    if vision_data[0].exists:
        if vision_data[0].width >= 60:
            robot.stop_all_movement()
            break
        else:
            robot.move_at(vision_data[0].bearing)
    else:
        robot.move_at(0)
    wait(20, MSEC)