Robot-Specific Python#

Introduction#

The V5RC 24-25 High Stakes Playground features methods exclusive to the build designed for this Playground, including two motor options, AI Vision Sensor, Optical Sensor, Rotation Sensor, and Game Positioning System (GPS) Sensor.

All standard VEXcode VR methods are available for use in the V5RC 25-26 Push Back Playground.

Below is a list of all available Playground-specific methods:

Motion – Move and track the robot’s motors.

  • Actions

    • spin – Spins the selected motor or motor group indefinitely.

    • spin_for – Spins a motor or group for a specific distance in degrees or turns.

    • spin_to_position – Spins a motor or motor group to a set position.

    • stop – Stops a specific motor or motor group from spinning.

  • Mutators

    • set_position – Sets the encoder value of a motor or motor group.

    • set_velocity – Sets the speed of a motor or motor group as a percentage.

    • set_timeout – Limits how long a motor block waits before giving up if movement is blocked.

  • Getters

    • is_done – Returns a Boolean indicating whether the motor is no longer spinning.

    • is_spinning – Returns a Boolean indicating whether the motor is currently spinning.

    • position – Returns the motor’s current rotational position in degrees or turns.

    • velocity – Returns the motor’s current velocity in % or rpm.

AI Vision - Capture and analyze objects using the AI Vision Sensor.

  • Getters

    • take_snapshot – Returns a tuple of detected objects based on a given signature.

  • Properties

    • width – Width of the detected object in pixels.

    • height – Height of the detected object in pixels.

    • centerX – X position of the object’s center in pixels.

    • centerY – Y position of the object’s center in pixels.

    • originX – X position of the object’s top-left corner in pixels.

    • originY – Y position of the object’s top-left corner in pixels.

    • id – Classification or tag ID of the object.

Sensing - Utilize the robot’s various sensors.

  • Optical

    • is_near_object – Returns whether a detected object is near the Optical Sensor.

    • color – Returns the color detected from the Optical Sensor.

    • brightness – Returns the brightness percentage detected by the sensor.

    • hue – Returns the hue value of the detected color.

    • object_detected – Registers a callback function for when the Optical Sensor detects an object.

    • object_lost – Registers a callback function for when the Optical Sensor loses an object.

  • Rotation

    • set_position – Sets the current position of the Rotation Sensor to a specific value.

    • angle – Returns the current angle of the sensor between 0 and 359.99 degrees.

    • position – Returns the total rotational position in degrees or turns.

  • GPS

    • x_position – Returns the current x coordinate of a GPS Sensor on the field.

    • y_position – Returns the current y coordinate of a GPS Sensor on the field.

    • heading – Returns the heading that the robot is currently facing based on the GPS Sensor’s readings from 0 to 359 degrees.

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

Motion#

Actions#

spin#

spin spins a motor or motor group in the specified direction indefinitely.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.spin(direction) — Arm Motor

pusher_motor

pusher_motor.spin(direction) — Pusher Motor

Parameters

Description

direction

The direction for the motor to spin:

  • FORWARD — Spins the Arm up or the Pusher down the arm.
  • REVERSE — Spins the Arm down or moves the Pusher up the arm.
def main():
    # Lift the Arm up before moving
    arm_motor.spin(FORWARD)
    wait(1, SECONDS)
    arm_motor.stop()

# VR threads — Do not delete
vr_thread(main)

spin_for#

spin_for spins a motor or motor group for a given amount of degrees or turns.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.spin_for(direction, distance, units, wait) — Arm Motor

pusher_motor

pusher_motor.spin_for(direction, distance, units, wait) — Pusher Motor

Parameters

Description

direction

The direction for the motor to spin:

  • FORWARD — Spins the Arm up or the Pusher down the arm.
  • REVERSE — Spins the Arm down or moves the Pusher up the arm.

distance

The distance for the motor to spin as an integer.

units

The unit that represents the distance to rotate:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) — The robot waits until spin_for is complete before executing the next line of code.
  • wait=False — The robot starts the action and moves on to the next line of code right away, without waiting for spin_for to finish.

def main():
    # Lift the Arm up before moving
    arm_motor.spin_for(FORWARD, 400, DEGREES)

# VR threads — Do not delete
vr_thread(main)

spin_to_position#

spin_to spins a motor or motor group to a given position.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.spin_to_position(angle, units, wait=True) — Arm Motor

pusher_motor

pusher_motor.spin_to_position(angle, units, wait=True) — Pusher Motor

Parameters

Description

angle

The specific angle or number of turns that the motor will spin to.

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) — The robot waits until spin_to_position is complete before executing the next line of code.
  • wait=False — The robot starts the action and moves on to the next line of code right away, without waiting for spin_to_position to finish.

def main():
    # Lift the Arm up before moving
    arm_motor.spin_to_position(400, DEGREES)

# VR threads — Do not delete
vr_thread(main)

stop#

stop stops a motor or motor group from spinning.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.stop() — Arm Motor

pusher_motor

pusher_motor.stop() — Pusher Motor

Parameters

Description

This method has no parameters.

def main():
    # Lift the Arm up before moving
    arm_motor.spin(FORWARD)
    wait(1, SECONDS)
    arm_motor.stop()

# VR threads — Do not delete
vr_thread(main)

Mutators#

set_position#

set_position sets a motor’s or motor group’s encoder position to the given position value.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.set_position(position, units) — Arm Motor

pusher_motor

pusher_motor.set_position(position, units) — Pusher Motor

Parameters

Description

position

The specific integer for the motor’s encoder to be set to.

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS
def main():
    # Make the raised Arm as the new 0 degrees position
    arm_motor.spin_to_position(400, DEGREES)
    arm_motor.set_position(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

set_velocity#

set_velocity sets the speed of a motor or motor group.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.set_velocity(velocity, units) — Arm Motor

pusher_motor

pusher_motor.set_velocity(velocity, units) — Pusher Motor

Parameters

Description

velocity

The speed that the V5 Motor will spin at, ranging from 0 to 100.

units

The unit that represents the new velocity:

  • PERCENT
def main():
    # Lift the Arm up before moving
    arm_motor.set_velocity(100, PERCENT)
    arm_motor.spin_to_position(400, DEGREES)

# VR threads — Do not delete
vr_thread(main)

set_timeout#

set_timeout sets a time limit for a motor’s or motor group’s movement commands. This prevents Motion commands that do not reach their intended position from preventing subsequent commands from running.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.set_timeout(value, units) — Arm Motor

pusher_motor

pusher_motor.set_timeout(value, units) — Pusher Motor

Parameters

Description

value

The amount of time the motor will wait before stopping.

units

The unit to represent the timeout:

  • SECONDS
  • MSEC — milliseconds
def main():
    # Lift the Arm up before moving
    arm_motor.set_timeout(1, SECONDS)
    arm_motor.spin_for(FORWARD, 3, TURNS)

# VR threads — Do not delete
vr_thread(main)

Getters#

is_done#

is_done returns a Boolean indicating whether the specified motor or motor group is not spinning.

  • True – The specified motor is not spinning.

  • False – The specified motor is spinning.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.is_done() — Arm Motor

pusher_motor

pusher_motor.is_done() — Pusher Motor

Parameters

Description

This method has no parameters.

is_spinning#

is_spinning returns a Boolean indicating whether the specified motor or motor group is spinning.

  • True – The specified motor is spinning.

  • False – The specified motor is not spinning.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.is_spinning() — Arm Motor

pusher_motor

pusher_motor.is_spinning() — Pusher Motor

Parameters

Description

This method has no parameters.

position#

position returns the total distance the specified motor or motor group has rotated.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.position(units) — Arm Motor

pusher_motor

pusher_motor.position(units) — Pusher Motor

Parameters

Description

units

The units that represent the motor’s position:

  • DEGREES
  • TURNS

velocity#

velocity returns the current rotational speed of the motor or motor group.

Usage:
One of two available motor objects can be used with this method, as shown below:

motor

Command

arm_motor

arm_motor.velocity(units) — Arm Motor

pusher_motor

pusher_motor.velocity(units) — Pusher Motor

Parameters

Description

units

The unit that represent the motor’s position:

  • PERCENT

AI Vision#

Getters#

take_snapshot#

take_snapshot filters the data from the AI Vision Sensor frame to return 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

Which signature to get data of. The only available signature is:

  • AiVision.ALL_AIOBJS - Detects Mobile Goals, Red Rings, and Blue Rings

def main():
    # Lift the Arm to not block the AI Vision Sensor's field of view
    arm_motor.spin_for(FORWARD, 350, DEGREES)
    while True:
        brain.screen.clear_screen()

        # Display the data of the largest detected Game Element
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

        if len(ai_objects) is not 0:
            brain.screen.print("Center X: ", ai_objects[0].centerX)
            brain.screen.next_row()

            brain.screen.print("Center Y: ", ai_objects[0].centerY)
            brain.screen.next_row()

        else:
            brain.screen.print("No object detected.")
                
        wait(0.2, SECONDS)

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

Some property values are based off of the detected object’s position in the AI Vision Sensor’s view at the time that take_snapshot was used. The AI Vision Sensor has a resolution of 320 by 240 pixels.

.width#

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

def main():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the width of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].width)

# 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():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the height of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].height)

# 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():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the centerX of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].centerX)

# 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():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the centerY of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].centerY)

# 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():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the originX of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].originX)

# 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():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display the originY of the largest Game Element
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:
        brain.screen.print(ai_objects[0].originY)

# VR threads — Do not delete
vr_thread(main)

.id#

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

AI Classification

id

Signature

MobileGoal

0

GameElements.MOBILE_GOAL

RedRing

1

GameElements.RED_RING

BlueRing

2

GameElements.BLUE_RING

def main():
    # Lift the Arm to not block the AI Vision Sensor's view
    arm_motor.spin_for(FORWARD, 350, DEGREES)

    # Display if a Mobile Goal is detected
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    if len(ai_objects) is not 0:

        if ai_objects[0].id == GameElements.MOBILE_GOAL:
            brain.screen.print("This is a Mobile Goal.")
        else:
            brain.screen.print("This is not a Mobile Goal.")

# VR threads — Do not delete
vr_thread(main)

Sensing#

Optical#

is_near_object#

is_near_object returns a Boolean indicating whether or not the Optical Sensor detects an object close to the sensor.

  • True – The object is close to the Optical Sensor.

  • False – The object is not close to the Optical Sensor.

Usage:
front_optical.is_near_object()

Parameters

Description

This method has no parameters.

def main():
    # Display if the Optical Sensor is detecting an object
    brain.screen.print(front_optical.is_near_object())

# VR threads — Do not delete
vr_thread(main)

color#

color returns the color detected by the Optical Sensor.

Color Returned:

  • NONE – No color detected.
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • CYAN

Usage:
front_optical.color()

Parameters

Description

This method has no parameters.

def main():
    # Display the color of the ring on the Arm
    brain.screen.print(front_optical.color())

# VR threads — Do not delete
vr_thread(main)

brightness#

brightness returns the brightness value detected by the Optical Sensor as a percent from 0% to 100%.

Usage:
front_optical.brightness()

Parameters

Description

This method has no parameters.

hue#

hue returns the hue detected by the Optical Sensor.

Hue values range from 0 to 359 degrees, corresponding to positions on the color wheel shown below.

A circular color wheel displaying a full spectrum of hues labeled with degree values around the perimeter, increasing in 30-degree increments from 0° at the top to 360°.

Usage:
front_optical.hue()

Parameters

Description

This method has no parameters.

def main():
    # Display the hue of the ring on the Arm.
    brain.screen.print("Hue: ", optical.hue())

# VR threads — Do not delete
vr_thread(main)

object_detected#

object_detected registers a callback function for when the Optical Sensor detects an object.

Usage:
front_optical.object_detected(callback, arg)

Parameters

Description

callback

A function that will be called when an object is detected.

arg

Optional. A tuple that is used to pass arguments to the callback function.

object_lost#

object_lost registers a callback function for when the Optical Sensor loses a previously detected object.

Usage:
front_optical.object_lost(callback, arg)

Parameters

Description

callback

A function that will be called when a detected object is lost.

arg

Optional. A tuple that is used to pass arguments to the callback function.

Rotation#

set_position#

set_position sets the current position of the Rotation Sensor to a value in degrees.

Usage:
pusher_rotation.set_position(position, units)

Parameters

Description

position

The position to set the Rotation Sensor to.

units

Optional. The unit of measurement:

  • DEGREES (default)
  • TURNS

angle#

angle returns the current angle of the sensor as a float.

Usage:
pusher_rotation.angle(units)

Parameters

Description

units

Optional. The unit of measurement:

  • DEGREES (default) – 0 to 359.99
  • TURNS

position#

position returns the total rotational position.

Usage:
pusher_rotation.position(units)

Parameters

Description

units

Optional. The unit of measurement:

  • DEGREES (default) – 0 to 359 as an integer
  • TURNS – As a float

GPS#

x_position#

x_position returns the current x coordinate of a GPS (Game Positioning System™) Sensor on the field.

Usage:
gps.x_position(units)

Parameters

Description

units

The unit of the offset value:

  • INCHES
  • MM

y_position#

y_position returns the current y coordinate of a GPS (Game Positioning System™) Sensor on the field.

Usage:
gps.y_position(units)

Parameters

Description

units

The unit of the offset value:

  • INCHES
  • MM

heading#

heading returns the heading that the robot is currently facing based on the GPS (Game Positioning System™) Sensor’s readings from 0 to 359 degrees.

Usage:
gps.heading()

Parameters

Description

This method has no parameters.