Robot Specific Python#
Introduction#
The VIQRC 25-26 Mix & Match Playground features methods exclusive to the build designed for this Playground, including two motor options, Optical Sensor, and 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 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.
Sensing - Utilize the robot’s various sensors.
Touch LED
set_color – Sets the TouchLED to a selected color.
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.
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():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
spin_to_position#
spin_to
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():
# Place a Pin atop another Pin
lift_motor.spin_to_position(-2, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
stop#
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():
# Place a Pin atop another Pin
lift_motor.spin(REVERSE)
wait(2, SECONDS)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
lift_motor.set_position(100, DEGREES)
lift_motor.spin_to_position(-500, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
lift_motor.set_velocity(100, PERCENT)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
lift_motor.set_timeout(2, SECONDS)
lift_motor.spin_for(REVERSE, 5, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
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)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
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)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another Pin
lift_motor.spin(REVERSE)
while not -600 > claw_motor.position(DEGREES):
wait(2, MSEC)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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 represent the motor’s position:
|
def main():
# Place a Pin atop another Pin
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))
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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():
# Place a Pin atop another 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)
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():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not not optical.is_near_object():
wait(2, MSEC)
lift_motor.spin_to_position(0, DEGREES)
color#
color
returns the color detected by the Optical Sensor:
Color Returned: |
---|
|
Usage:
optical.color()
Parameters |
Description |
---|---|
This method has no parameters. |
def main():
# Place a Pin atop another Pin
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)
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():
# Place a Pin atop another Pin
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)
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():
# Place a Pin atop another Pin
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)
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_open_claw():
# Change color when claw isn't holding anything
touchled.set_color(RED)
def main():
# Place a Pin atop another Pin
optical.object_detected(pin_in_open_claw)
touchled.set_color(GREEN)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
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 lift when Pin not in claw
lift_motor.spin_to_position(0, DEGREES)
def main():
# Place a Pin atop another Pin
optical.object_lost(lower_lift)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)