Robot-Specific Python#
Introduction#
The VIQRC 25-26 Mix & Match Playground features methods exclusive to this Playground, including two motor options, the IQ AI Vision Sensor, the Optical Sensor, and the Touch LED.
All standard VEXcode VR methods are available for use in the VIQRC 25-26 Mix & Match 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 indefinitely.spin_for- Spins a motor for a specific distance in degrees or turns.spin_to_position- Spins a motor to a set position.stop- Stops a specific motor from spinning.
Mutators
set_position- Sets the encoder value of a motor.set_velocity- Sets the speed of a motor as a percentage.set_timeout- Limits how long a motor command 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 percent.
AI Vision - Capture and analyze objects using the IQ 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.
Touch LED
set_color- Sets the Touch LED to a selected color.
Optical
is_near_object- Returns whether a detected object is near the Optical Sensor.color- Returns the color detected by 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.
The examples on this page use the default Playground start position.
Motion#
Actions#
spin#
spin spins a motor indefinitely.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The direction for the motor to spin:
|
def main():
# Raise the lift, drive forward, then open the claw.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
spin_for#
spin_for spins a motor 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 |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The direction for the motor to spin:
|
|
The distance for the motor to spin as an integer. |
|
The unit that represents the distance to rotate:
|
|
Optional.
|
def main():
# Raise the lift, drive forward, then open the claw.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
spin_to_position#
spin_to_position spins a motor to a given position.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The specific angle or number of turns that the motor will spin to. |
|
The unit that represents the angle to rotate to:
|
|
Optional.
|
def main():
# Raise the lift to a known position, drive, then open the claw
lift_motor.spin_to_position(-2, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
stop motor#
stop stops a motor from spinning.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Raise the lift for 2 seconds, then stop
lift_motor.spin(REVERSE)
wait(2, SECONDS)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
Mutators#
set_position#
set_position sets a motor’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 |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The specific integer for the motor’s encoder to be set to. |
|
The unit that represents the angle to rotate to:
|
def main():
# Reset the lift position, then move to a new target.
lift_motor.set_position(100, DEGREES)
lift_motor.spin_to_position(-500, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
set_velocity#
set_velocity sets the speed of a motor.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The speed that the IQ Motor will spin at, ranging from 0 to 100. |
|
The unit that represents the new velocity:
|
def main():
# Set lift speed, then raise it.
lift_motor.set_velocity(100, PERCENT)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
set_timeout#
set_timeout sets a time limit for a motor’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 |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The amount of time the motor will wait before stopping. |
|
The unit to represent the timeout:
|
def main():
# Limit how long the lift waits to reach its target.
lift_motor.set_timeout(2, SECONDS)
lift_motor.spin_for(REVERSE, 5, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
Getters#
is_done#
is_done returns a Boolean indicating whether the specified motor 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 |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Flash the Touch LED while the lift is moving.
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.1, SECONDS)
while not lift_motor.is_done():
touchled.set_color(RED)
wait(0.5, SECONDS)
touchled.set_color(NONE)
wait(0.5, SECONDS)
# VR threads — Do not delete
vr_thread(main)
is_spinning#
is_spinning returns a Boolean indicating whether the specified motor 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 |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Flash the Touch LED while the lift is spinning.
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.1, SECONDS)
while lift_motor.is_spinning():
touchled.set_color(RED)
wait(0.5, SECONDS)
touchled.set_color(NONE)
wait(0.5, SECONDS)
# VR threads — Do not delete
vr_thread(main)
position#
position returns the total distance the specified motor has rotated.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The units that represent the motor’s position:
|
def main():
# Raise the lift until it reaches roughly 600 degrees.
lift_motor.spin(REVERSE)
while not lift_motor.position(DEGREES) <= -600:
wait(2, MSEC)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
velocity#
velocity returns the current rotational speed of the motor.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The unit that represents the motor’s velocity:
|
def main():
# Print the lift velocity while it is moving.
lift_motor.set_velocity(100, PERCENT)
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.5, SECONDS)
brain.screen.print(lift_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
AI Vision#
Getters#
take_snapshot#
take_snapshot filters the data from the IQ 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 |
|---|---|
|
Which signature to get data of. The only available signature is:
|
def main():
# Place the Red Pin on the top-left Blue Pin.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.turn_for(LEFT, 38, DEGREES)
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
while not ai_objects[0].width > 48:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
drivetrain.drive(FORWARD)
drivetrain.drive_for(FORWARD, 160, MM)
claw_motor.spin(FORWARD)
# 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 on the detected object’s position in the IQ AI Vision Sensor’s view at the time that take_snapshot was used. The IQ 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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():
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 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 |
|---|---|---|
Beam |
0 |
|
Blue Pin |
1 |
|
Red Pin |
2 |
|
Orange Pin |
3 |
|
def main():
# Find the nearest Blue Pin in the current snapshot.
lift_motor.spin_for(REVERSE, 600, DEGREES)
object_count = 1
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if len(ai_objects) > 0:
for repeat_count in range(len(ai_objects)):
if ai_objects[repeat_count].id == GameElements.BLUE_PIN:
brain.screen.print(str("Closest Blue Pin is ") + str(object_count))
break
object_count = object_count + 1
# VR threads — Do not delete
vr_thread(main)
Sensing#
Touch LED#
set_color#
set_color sets the color of the Touch LED.
Usage:
touchled.set_color(color)
Parameters |
Description |
|---|---|
|
The color to set the Touch LED to:
|
def main():
# Light the Touch LED while placing a Pin.
touchled.set_color(RED)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
touchled.set_color(NONE)
# VR threads — Do not delete
vr_thread(main)
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:
optical.is_near_object()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Wait until the claw no longer detects an object, then lower the lift.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while optical.is_near_object():
wait(2, MSEC)
lift_motor.spin_to_position(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
color#
color returns the color detected by the Optical Sensor:
Color Returned: |
|---|
|
Usage:
optical.color()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Wait until the Optical Sensor detects red, then lower the lift.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not optical.color() == RED:
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
# 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:
optical.brightness()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Lower the lift once the claw sees a darker object.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not 30 > optical.brightness():
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
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.

Usage:
optical.hue()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Lower the lift once the Optical Sensor reports a hue value.
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not optical.hue() > 0:
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
# 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:
optical.object_detected(callback, arg)
Parameters |
Description |
|---|---|
|
A function that will be called when an object is detected. |
|
Optional. A tuple that is used to pass arguments to the callback function. |
def pin_in_claw():
touchled.set_color(RED)
def main():
# Change the Touch LED color when the claw detects an object.
optical.object_detected(pin_in_claw)
touchled.set_color(GREEN)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
object_lost#
object_lost registers a callback function for when the Optical Sensor loses an object.
Usage:
optical.object_lost(callback, arg)
Parameters |
Description |
|---|---|
|
A function that will be called when an object is lost. |
|
Optional. A tuple that is used to pass arguments to the callback function. |
def lower_lift():
# Lower the lift when the claw no longer holds an object.
lift_motor.spin_to_position(0, DEGREES)
def main():
optical.object_lost(lower_lift)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)