Robot-Specific Python#
Introduction#
The VIQRC 22-23 Slapshot Playground features methods exclusive to this Playground, including two motor options, the Intake Bumper, and the Optical Sensor.
All standard VEXcode VR methods are available for use in the VIQRC 22-23 Slapshot 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.
Events - Run code when a sensor changes state.
Bumper
Optical
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.
Sensing - Utilize the robot’s various sensors.
Bumper
pressing- Returns whether the Intake Bumper is currently pressed.
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.
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():
# Spin the Intake Motor to collect a Disc
intake_motor.spin(FORWARD)
wait(1, SECONDS)
intake_motor.stop()
# 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():
# Collect the preloaded Disc
intake_motor.spin_for(FORWARD, 180, DEGREES)
# 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 Arm to reach over the barrier
arm_motor.spin_to_position(600, DEGREES)
# 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 Arm briefly, then stop it
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 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():
# Set the Intake Motor position, then return to 0 degrees
intake_motor.set_position(180, DEGREES)
intake_motor.spin_to_position(0, DEGREES)
# 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 motor will spin at, ranging from -100 to 100. |
|
The unit that represents the new velocity:
|
def main():
# Set the Intake Motor speed before collecting a Disc
intake_motor.set_velocity(75, PERCENT)
intake_motor.spin_for(FORWARD, 180, DEGREES)
# 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 Arm Motor waits to reach its target
arm_motor.set_timeout(2, 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 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. |
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. |
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():
# Print the current Arm Motor position
brain.screen.print(arm_motor.position(DEGREES))
# 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 current Intake Motor velocity
brain.screen.print(intake_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
Events#
Bumper#
pressed#
pressed registers a callback function for when the Intake Bumper is pressed.
Usage:
intake_bumper.pressed(callback)
Parameters |
Description |
|---|---|
|
A function that will be called when the Intake Bumper is pressed. |
def stop_intake():
intake_motor.stop()
def main():
# Stop the Intake Motor when a Disc reaches the Intake Bumper
intake_bumper.pressed(stop_intake)
intake_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
released#
released registers a callback function for when the Intake Bumper is released.
Usage:
intake_bumper.released(callback)
Parameters |
Description |
|---|---|
|
A function that will be called when the Intake Bumper is released. |
def lower_arm():
arm_motor.spin_to_position(0, DEGREES)
def main():
# Lower the Arm when the Intake Bumper is released
intake_bumper.released(lower_arm)
# VR threads — Do not delete
vr_thread(main)
Optical#
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 |
|---|---|
|
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 stop_intake():
intake_motor.stop()
def main():
# Stop the Intake Motor when the Optical Sensor detects a Disc
front_optical.object_detected(stop_intake)
intake_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:
front_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 resume_intake():
intake_motor.spin(FORWARD)
def main():
# Restart the Intake Motor when the Optical Sensor no longer detects a Disc
front_optical.object_lost(resume_intake)
# VR threads — Do not delete
vr_thread(main)
Sensing#
Bumper#
pressing#
pressing returns a Boolean indicating whether the Intake Bumper is currently pressed.
True- The Intake Bumper is pressed.False- The Intake Bumper is not pressed.
Usage:
intake_bumper.pressing()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Print whether the Intake Bumper is pressed
brain.screen.print(intake_bumper.pressing())
# 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:
front_optical.is_near_object()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Print whether the Front Optical Sensor detects a nearby Disc
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: |
|---|
|
Usage:
front_optical.color()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Print the color detected by the Front Optical Sensor
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. |
def main():
# Print the Front Optical Sensor brightness
brain.screen.print(front_optical.brightness())
# 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:
front_optical.hue()
Parameters |
Description |
|---|---|
This method has no parameters. |
def main():
# Print the hue detected by the Front Optical Sensor
brain.screen.print(front_optical.hue())
# VR threads — Do not delete
vr_thread(main)