Macro#

Introduction#

VEX AIM Python macros are prebuilt code blocks that can be dragged into a Python project. Each macro is a group of Python commands that work together to make the robot complete a larger behavior.

These are not single methods that are called from code. Instead, each macro is inserted as a ready-made code sequence that can be used as-is or edited after it is added to a project.

On this page, each Python code block shows the actual code sequence that is inserted when that macro is used.

Below is a list of all macros:

Emotions — Make the robot act expressively.

AI Vision (Sports Ball) — Turn toward or collect sports balls.

AI Vision (Orange Barrel) — Turn toward or collect orange barrels.

AI Vision (Blue Barrel) — Turn toward or collect blue barrels.

AI Vision (AIM Robot) — Turn toward or move toward other AIM Robots.

AI Vision (AprilTag) — Turn toward or move toward AprilTags.

Emotions#

act happy#

This macro makes the robot act happy by showing happy faces, playing a happy sound, changing the LEDs, and moving back and forth. Use it when the robot should respond in a cheerful or positive way.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro makes the robot act sad by showing sad faces, playing a sad sound, fading the LEDs, and moving slowly in different directions. Use it when the robot should show a disappointed or low-energy reaction.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro makes the robot act silly by spinning, changing faces, and flashing different LED colors. Use it when the robot should make a playful or surprising reaction.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro makes the robot act angry by showing angry faces, playing an angry sound, turning the LEDs red, and moving sharply. Use it when the robot should show a frustrated or warning reaction.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro makes the robot act excited by showing an excited face, playing an excited sound, moving quickly side to side, and flashing orange LEDs. Use it when the robot should celebrate or react with high energy.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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 Vision (Sports Ball)#

turn right until sports ball#

This macro turns the robot to the right until the AI Vision Sensor detects a sports ball, then turns toward the detected object. Use it when the robot needs to search for a sports ball before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro turns the robot to the left until the AI Vision Sensor detects a sports ball, then turns toward the detected object. Use it when the robot needs to search for a sports ball before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro moves the robot toward a detected sports ball until the robot has collected it. Use this after the robot can see a sports ball, or after using a turn-until-sports-ball macro to find one.

Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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 Vision (Orange Barrel)#

turn right until orange barrel#

This macro turns the robot to the right until the AI Vision Sensor detects an orange barrel, then turns toward the detected object. Use it when the robot needs to search for an orange barrel before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro turns the robot to the left until the AI Vision Sensor detects an orange barrel, then turns toward the detected object. Use it when the robot needs to search for an orange barrel before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro moves the robot toward a detected orange barrel until the robot has collected it. Use this after the robot can see an orange barrel, or after using a turn-until-orange-barrel macro to find one.

Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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 Vision (Blue Barrel)#

turn right until blue barrel#

This macro turns the robot to the right until the AI Vision Sensor detects a blue barrel, then turns toward the detected object. Use it when the robot needs to search for a blue barrel before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro turns the robot to the left until the AI Vision Sensor detects a blue barrel, then turns toward the detected object. Use it when the robot needs to search for a blue barrel before moving toward it or collecting it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro moves the robot toward a detected blue barrel until the robot has collected it. Use this after the robot can see a blue barrel, or after using a turn-until-blue-barrel macro to find one.

Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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 Vision (AIM Robot)#

turn right until AIM robot#

This macro turns the robot to the right until the AI Vision Sensor detects another VEX AIM Coding Robot, then turns toward it. Use it when the robot needs to search for another robot before moving toward it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro turns the robot to the left until the AI Vision Sensor detects another VEX AIM Coding Robot, then turns toward it. Use it when the robot needs to search for another robot before moving toward it.

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro moves the robot toward another VEX AIM Coding Robot until it is close enough to stop. Use this after the robot can see another robot, or after using a turn-until-AIM-robot macro to find one.

Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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)

AI Vision (AprilTag)#

turn right until AprilTag#

This macro turns the robot to the right until the AI Vision Sensor detects AprilTag ID 0, then turns toward the detected AprilTag. Use it when the robot needs to search for an AprilTag before moving toward it. To change which AprilTag the robot will turn until, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro turns the robot to the left until the AI Vision Sensor detects AprilTag ID 0, then turns toward the detected AprilTag. Use it when the robot needs to search for an AprilTag before moving toward it. To change which AprilTag the robot will turn until, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)

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

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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#

This macro moves the robot toward AprilTag ID 0 until it is close enough to stop. Use this after the robot can see an AprilTag, or after using a turn-until-AprilTag macro to find one. To change which AprilTag the robot will move to, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)

Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.

The code below shows the Python sequence that is added to your project when this macro is dragged from the Toolbox. You can use the sequence as-is or edit it after adding it.

# 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)