Robot-Specific Python#
Introduction#
The Hero Bot, Huey, includes 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 Robot-specific methods:
Drivetrain – Move and turn the robot.
Actions
drive— Moves the robot forward or reverse forever.drive_for— Moves the robot forward or reverse for a specific distance.turn— Turns the robot left or right forever.turn_for— Turns the robot left or right for a specific number of degrees.turn_to_heading— Turns the robot to face a specific heading from -359 to 359 degrees.turn_to_rotation— Turns the robot to a specific rotation.stop— Stops the robot’s movement.
Mutators
set_heading— Changes the robot’s current heading to a new heading.set_rotation— Changes the robot’s current rotation to a new rotation.set_timeout— Sets how much time the robot will try to finish a movement.set_drive_velocity— Tells the robot how fast to drive.set_turn_velocity— Tells the robot how fast to turn.
Getters
heading— Returns the robot’s current heading from 0 to 359.99 degrees.rotation— Returns the robot’s current rotation.is_done— Returns whether the robot is finished moving, as a Boolean value.is_moving— Returns whether the robot is moving, as a Boolean value.velocity— Returns how fast the robot is driving.
Motion - Move and track the robot’s motors.
Actions
spin— Spins a motor forward or reverse forever.spin_for— Spins a motor for a specific distance.spin_to_position— Spins a motor to a specific position.stop— Stops a motor from spinning.
Mutators
set_position— Changes the motor’s current position to a new value.set_velocity— Tells a motor how fast to spin.set_timeout— Sets how much time a motor will try to finish a movement.
Getters
is_done— Returns whether the motor is finished moving, as a Boolean value.is_spinning— Returns whether the motor is spinning, as a Boolean value.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- 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 the Optical Sensor detects an object within range.color– Returns the color detected by the Optical Sensor.brightness– Returns the amount of light reflected from the object.hue– Returns the hue detected by the Optical Sensor.object_detected– Registers a function to be called when the Optical Sensor detects an object.object_lost– Registers a function to be called when the Optical Sensor loses an object.
The examples on this page use the default Playground start position.
Drivetrain#
The drivetrain controls how the VR Robot drives and turns. The drivetrain can move forward or reverse, turn left or right, turn to headings, and track its rotation.
Actions#
drive#
drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.
Usage:
drivetrain.drive(direction)
Parameters |
Description |
|---|---|
|
The direction the robot moves: |
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
drive_for#
drive_for moves the robot forward or reverse for a specific distance. The project will wait until the robot is done moving before the next line of code runs.
Usage:
drivetrain.drive_for(direction, distance, units, wait)
Parameters |
Description |
|---|---|
|
The direction the robot moves: |
|
The distance the robot drives. This can be an |
|
Optional. The distance unit: |
|
Optional. |
def main():
# Drive forward and backward
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)
# VR threads — Do not delete
vr_thread(main)
turn#
turn turns the robot left or right forever. The robot will continue to turn until it is given another action, like driving or stopping.
Usage:
drivetrain.turn(direction)
Parameters |
Description |
|---|---|
|
The direction the robot turns: |
def main():
# Turn right and left, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.turn(LEFT)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
turn_for#
turn_for turns the robot left or right for a specific number of degrees. The turn is relative to the current position of the robot. The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_for(direction, angle, units, wait)
Parameters |
Description |
|---|---|
|
The direction the robot turns: |
|
The number of degrees the robot turns. This can be an |
|
Optional. The rotation unit: |
|
Optional. |
def main():
# Turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES)
# VR threads — Do not delete
vr_thread(main)
turn_to_heading#
A heading is the direction the robot is facing, measured in degrees. turn_to_heading turns the robot to face a specific heading from -359 to 359 degrees. The robot will turn the shortest direction to reach the target heading.
The robot’s starting heading is 0 degrees.
The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_to_heading(angle, units, wait)
Parameters |
Description |
|---|---|
|
The direction the robot should face, in degrees. This can be an |
|
Optional. The rotation unit: |
|
Optional. |
def main():
# Turn to face the cardinal directions
drivetrain.turn_to_heading(90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(270, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
turn_to_rotation#
turn_to_rotation turns the robot to a specific rotation.
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. Rotation can also be set using the set_rotation method.
Rotation values are absolute. This means the direction of the turn depends on the robot’s current rotation. Turning right increases the rotation, and turning left decreases the rotation.
For example, if the robot starts at 0 degrees and you turn to a rotation of 720 degrees, it will turn right twice. If you then turn to a rotation of 360 degrees, it will turn left once, because 360 is less than 720.
The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_to_rotation(angle, units, wait)
Parameters |
Description |
|---|---|
|
The rotation value, in degrees, that the robot will turn to. This can be an |
|
Optional. The rotation unit: |
|
Optional. |
def main():
# Turn left, then spin in a circle
# clockwise and face right
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES)
# VR threads — Do not delete
vr_thread(main)
stop#
stop stops the robot’s movement.
Usage:
drivetrain.stop()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
Mutators#
set_heading#
A heading is the direction the robot is facing, measured in degrees. set_heading changes the robot’s current heading to a new heading value.
For example, if the robot has turned to face right, setting the heading to 0 degrees makes that right-facing position the new 0 degrees. Then the robot can turn to other positions based on that new heading.
Usage:
drivetrain.set_heading(heading, units)
Parameters |
Description |
|---|---|
|
The heading value, in degrees, to set for the robot. This can be an |
|
Optional. The heading unit: |
def main():
# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
set_rotation#
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. set_rotation changes the robot’s current rotation to a new value.
For example, if the robot has made two full turns to the right, its rotation value will be 720 degrees. Setting the rotation to 0 degrees will reset that rotation from 720 to 0 degrees. Then the robot can turn to rotations based on that new value.
Usage:
drivetrain.set_rotation(rotation, units)
Parameters |
Description |
|---|---|
|
The rotation value, in degrees, to set for the robot. This can be an |
|
Optional. The rotation unit: |
def main():
# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
set_timeout#
set_timeout sets how much time the robot will try to finish a movement. If the robot cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the robot from getting stuck on a movement.
Usage:
drivetrain.set_timeout(value, units)
Parameters |
Description |
|---|---|
|
The amount of time the robot can try to finish a movement. This can be a positive |
|
The time unit: |
def main():
# Turn right after driving forward
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
# VR threads — Do not delete
vr_thread(main)
set_drive_velocity#
set_drive_velocity tells the robot how fast to drive. A higher percentage makes the robot drive faster and a lower percentage makes the robot drive slower.
Every project begins with the robot driving at 50% velocity by default.
Note: A higher velocity makes the robot drive faster, but it may be less precise. A lower velocity makes the robot drive slower, but it can be more precise.
Usage:
drivetrain.set_drive_velocity(velocity, units)
Parameters |
Description |
|---|---|
|
The velocity to drive with from 0% to 100%. This can be an |
|
Optional. The velocity unit: |
def main():
# Drive forward at different velocities
# Default velocity
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
# VR threads — Do not delete
vr_thread(main)
set_turn_velocity#
set_turn_velocity tells the robot how fast to turn. A higher percentage makes the robot turn faster and a lower percentage makes the robot turn slower.
Every project begins with the robot turning at 50% velocity by default.
Note: A higher velocity makes the robot turn faster, but it may be less precise. A lower velocity makes the robot turn slower, but it can be more precise.
Usage:
drivetrain.set_turn_velocity(velocity, units)
Parameters |
Description |
|---|---|
|
The velocity to turn with from 0% to 100%. This can be an |
|
Optional. The velocity unit: |
def main():
# Turn at different velocities
# Default velocity
drivetrain.turn_for(RIGHT, 120, DEGREES)
wait(1, SECONDS)
# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 120, DEGREES)
wait(1, SECONDS)
# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 120, DEGREES)
# VR threads — Do not delete
vr_thread(main)
Getters#
heading#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359.99 degrees.
The robot’s starting heading is 0 degrees.
Usage:
drivetrain.heading(units)
Parameters |
Description |
|---|---|
|
Optional. The heading unit: |
def main():
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading(DEGREES))
# VR threads — Do not delete
vr_thread(main)
rotation#
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.
Turning right increases the rotation, and turning left decreases the rotation. For example, making two full turns to the right will return a rotation of 720 degrees.
Usage:
drivetrain.rotation(units)
Parameters |
Description |
|---|---|
|
Optional. The rotation unit: |
def main():
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation(DEGREES))
# VR threads — Do not delete
vr_thread(main)
is_done#
is_done returns whether the robot is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
True— The robot is finished moving.False— The robot is still moving.
This method works together with the following Drivetrain methods that have the wait parameter: drive_for, turn_for, turn_to_heading, and turn_to_rotation.
Usage:
drivetrain.is_done()
Parameters |
Description |
|---|---|
This method has no parameters. |
is_moving#
is_moving returns whether the robot is moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
True— The robot is moving.False— The robot is not moving.
Usage:
drivetrain.is_moving()
Parameters |
Description |
|---|---|
This method has no parameters. |
velocity#
velocity returns how fast the robot is driving, as a percentage from -100% to 100%.
Usage:
drivetrain.velocity(units)
Parameters |
Description |
|---|---|
|
Optional. The velocity unit: |
Example
def main():
# Display the velocity after driving
drivetrain.drive_for(FORWARD, 150, MM)
brain.screen.print("Velocity: ")
brain.screen.print(drivetrain.velocity(PERCENT))
Motion#
Huey uses the claw motor to open and close the claw so Pins and Beams can be picked up or dropped. The lift motor raises and lowers Pins or Beams so they can attach to one another.
Each motor has its own direction behavior. The direction descriptions explain how each direction moves that motor on Huey.
Actions#
spin#
spin spins a motor forward or reverse forever. The motor will continue to spin until it is given another action, like spinning in a different direction or stopping.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The direction the motor spins:
|
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 specific distance. The spin is relative to the current position of the motor. The project will wait until the motor is done spinning before the next line of code runs.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The direction the motor spins:
|
|
The distance the motor spins. |
|
Optional. The distance unit: |
|
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 specific position.
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. At the beginning of a project, the motor position is set to 0 degrees. The motor position can also be set using the set_position method.
Position values are absolute. This means the direction of the spin depends on the motor’s current position.
For example, if the motor starts at 0 degrees and spins to a position of 720 degrees, it will spin forward two turns. If it then spins to a position of 360 degrees, it will spin reverse one turn, because 360 is less than 720.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The position value the motor will spin to. |
|
Optional. The position unit: |
|
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#
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. set_position changes the motor’s current position to a new value.
For example, if a motor has spun to 180 degrees, setting the position to 0 degrees will reset that position from 180 to 0 degrees. Then the motor can spin to positions based on that new value.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The position value to set for the motor. |
|
Optional. The position unit: |
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 tells a motor how fast to spin. A higher percentage makes the motor spin faster and a lower percentage makes the motor spin slower.
Every project begins with each motor spinning at 50% velocity by default.
Note: A higher velocity makes the motor spin faster, but it may be less precise. A lower velocity makes the motor spin slower, but it can be more precise.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The velocity to spin with from 0% to 100%. This can be an |
|
The velocity unit: |
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 how much time a motor will try to finish a movement. If the motor cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor from getting stuck on a movement.
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 can try to finish a movement. This can be a positive |
|
The time unit: |
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 whether the motor is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.
True— The motor is finished moving.False— The motor is still moving.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
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 whether the motor is spinning, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.
True— The motor is spinning.False— The motor is not spinning.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
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#
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. position returns the motor’s current position.
At the beginning of a project, the motor position is set to 0 degrees. If the motor spins one full turn forward, the position will be 360 degrees or 1 turn. If the motor spins the other direction, the position will be negative.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The unit to return the motor position in: |
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 how fast the motor is spinning.
A positive value means the motor is spinning forward. A negative value means the motor is spinning in reverse.
Usage:
One of two available motor objects can be used with this method, as shown below:
motor |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
|
The velocity unit to return: |
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 within range.
True– The Optical Sensor detects an object.False– The Optical Sensor does not detect an object.
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 an instance of a Color class, based on the detected hue value. These can be compared to predefined Color objects to create conditional statements.
Possible colors are:
NONERED- A hue value between 340° - 20°GREEN- A hue value between 80° - 140°BLUE- A hue value between 200° - 240°YELLOW- A hue value between 40° - 60°ORANGE- A hue value between 20° - 40°PURPLE- A hue value between 240° - 280°CYAN- A hue value between 140° - 200°
Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see hue.
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 amount of light reflected from the object as a decimal (float) representing a percent.
A higher percentage means more light is reflected back to the Optical Sensor. A lower percentage means less light is reflected back.
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 value of the detected color as a decimal (float) from 0 to 359.
Hue is a way to describe color using numbers around a color wheel.

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 function to be called whenever the Optical Sensor detects a new object.
Usage:
optical.object_detected(callback, arg)
Parameters |
Description |
|---|---|
|
A previously defined function that executes when the Optical Sensor detects a new object. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information. |
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 function to be called whenever the Optical Sensor loses a detected object.
Usage:
optical.object_lost(callback, arg)
Parameters |
Description |
|---|---|
|
A previously defined function that executes when the Optical Sensor loses a detected object. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information. |
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)