Robot-Specific Python#

Introduction#

The Hero Bot, Flop, includes an IntakeMotor, an ArmMotor, and an IQ AI Vision Sensor.

All standard VEXcode VR Python commands are available for use in the IQ 26-27 Level Up Playground.

Below is a list of all available Robot-specific Python commands:

Motion – Move and track the robot’s motors.

  • Actions

    • spin – Spins a motor in one direction forever.

    • spin_for – Spins a motor for a specific number of degrees or turns.

    • spin_to_position – Spins a motor to a specific position.

    • stop – Stops a motor from spinning.

  • Settings

    • set_velocity – Sets how fast a motor will spin.

    • set_timeout – Sets how long a motor will try to finish a movement.

    • set_position – Changes the motor’s current position to a new value.

  • Values

    • is_done – Returns whether the motor is finished moving.

    • is_spinning – Returns whether the motor is spinning.

    • position – Returns the motor’s current position.

    • velocity – Returns how fast the motor is spinning.

AI Vision – Capture and analyze objects using the IQ AI Vision Sensor.

  • Getters

    • take_snapshot – Filters the sensor’s current frame to a specific signature and returns a tuple of detected objects.

  • Properties

    • .width – Returns the width of the detected object in pixels.

    • .height – Returns the height of the detected object in pixels.

    • .centerX – Returns the x-coordinate of the center of the detected object.

    • .centerY – Returns the y-coordinate of the center of the detected object.

    • .originX – Returns the x-coordinate of the top-left corner of the detected object’s bounding box.

    • .originY – Returns the y-coordinate of the top-left corner of the detected object’s bounding box.

    • .id – Returns the ID of the detected AI Classification.

The examples on this page use the default Playground start position.

Motion#

Flop has two robot-specific motors: intake_motor and arm_motor. Both use standard motor commands.

Direction constants:

  • intake_motor: FORWARD = intake (collects); REVERSE = outtake (ejects)

  • arm_motor: FORWARD = up (raises); REVERSE = down (lowers)

spin#

spin spins a motor in the given direction forever. The motor will continue to spin until it is given another command.

Usage:
motor.spin(direction)

Parameters

Description

direction

The direction to spin: FORWARD or REVERSE.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

spin_for#

spin_for spins a motor for a specific distance. The project waits until the motor is done before the next command runs unless wait=False is passed.

Usage:
motor.spin_for(direction, amount, unit)

Parameters

Description

direction

The direction to spin: FORWARD or REVERSE.

amount

The distance to spin. This can be an integer or a decimal.

unit

The unit of measurement: DEGREES or TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

spin_to_position#

spin_to_position spins a motor to a specific position. The project waits until the motor is done before the next command runs unless wait=False is passed.

Usage:
motor.spin_to_position(position, unit)

Parameters

Description

position

The position to spin to. This can be an integer or a decimal.

unit

The unit of measurement: DEGREES or TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_to_position(1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

stop#

stop stops a motor from spinning.

Usage:
motor.stop()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(1, SECONDS)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

set_velocity#

set_velocity sets how fast a motor will spin as a percentage from 0 to 100.

Usage:
motor.set_velocity(velocity, unit)

Parameters

Description

velocity

The velocity of the motor as a percentage from 0 to 100.

unit

The unit of measurement: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.set_velocity(100, PERCENT)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

set_timeout#

set_timeout sets how many seconds a motor will try to finish a movement before stopping.

Usage:
motor.set_timeout(timeout, unit)

Parameters

Description

timeout

The number of seconds the motor will try to finish a movement.

unit

The unit of measurement: SECONDS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_timeout(2, SECONDS)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

set_position#

set_position changes the motor’s current position to a new value, resetting its encoder.

Usage:
motor.set_position(position, unit)

Parameters

Description

position

The new position value.

unit

The unit of measurement: DEGREES or TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

is_done#

is_done returns a Boolean indicating whether the motor has finished its movement.

  • True – The motor has finished moving.

  • False – The motor is still moving.

Usage:
motor.is_done()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
    while not arm_motor.is_done():
        wait(5, MSEC)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

is_spinning#

is_spinning returns a Boolean indicating whether the motor is currently spinning.

  • True – The motor is spinning.

  • False – The motor is not spinning.

Usage:
motor.is_spinning()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    while intake_motor.is_spinning():
        wait(5, MSEC)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

position#

position returns the motor’s current position.

Usage:
motor.position(unit)

Parameters

Description

unit

The unit of measurement: DEGREES or TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    while arm_motor.position(DEGREES) < 360:
        arm_motor.spin(FORWARD)
        wait(2, MSEC)
    arm_motor.stop()
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

velocity#

velocity returns how fast the motor is currently spinning as a percentage from -100 to 100.

Usage:
motor.velocity(unit)

Parameters

Description

unit

The unit of measurement: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(0.2, SECONDS)
    brain.screen.print(intake_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

AI Vision#

take_snapshot#

take_snapshot filters data from the IQ AI Vision Sensor frame to a single signature — a saved description of something the sensor can recognize, such as a game element on the field — and returns a tuple.

The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.

Usage:
ai_vision.take_snapshot(signature)

Parameters

Description

signature

Filters the dataset to only include data of the given signature. Available signatures are:

  • AiVision.ALL_AIOBJS - Detects all Bean Bags.
  • AiVision.ALL_TAGS - Detects AprilTags, found on the sides of the Goals.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Turn to face a red Bean Bag
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn(LEFT)
                elif ai_object.centerX > 180:
                    drivetrain.turn(RIGHT)
                else:
                    drivetrain.stop()
                    return
                break

# VR threads — Do not delete
vr_thread(main)

Properties#

There are seven properties that are included with each object stored in a tuple after take_snapshot is used.

All property values except .id describe the detected object’s position and size in the IQ AI Vision Sensor’s view at the moment take_snapshot was used. These values are measured in pixels, based on the sensor’s 320 by 240 pixel resolution.

.width#

.width returns the width of the detected object in pixels, which is an integer between 1 and 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.width > 200:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.height#

.height returns the height of the detected object in pixels, which is an integer between 1 and 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.height > 110:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.centerX#

.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the Bean Bag centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.centerY#

.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.centerY)
            break

# VR threads — Do not delete
vr_thread(main)

.originX#

.originX returns the x-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the left edge centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.originX < 100:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.originY#

.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag's top edge
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.originY)
            break

# VR threads — Do not delete
vr_thread(main)

.id#

.id returns the ID of the detected AI Classification as an integer.

AI Classification

id

BlueBeanBag

0

RedBeanBag

1

YellowBeanBag

2

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Pick up the Bean Bag if it's red
    drivetrain.set_drive_velocity(30, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            intake_motor.spin(FORWARD)
            drivetrain.drive_for(FORWARD, 150, MM)
            break

# VR threads — Do not delete
vr_thread(main)


def main():
    # Stop when you see the Red Goal
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.turn(RIGHT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
        for ai_object in ai_objects:
            if ai_object.id == 1:
                drivetrain.stop()
                brain.screen.print("Found the Red Goal!")
                return

# VR threads — Do not delete
vr_thread(main)