Table of Contents

Sensors

This page documents the VEX V5 sensor classes available in the DishPy SDK. Each class provides an interface to a specific VEX sensor, with methods for reading data and controlling sensor behavior.


GPS Sensor

vex.Gps

Gps class - a class for working with the gps sensor
Arguments:
port : The smartport this device is attached to
origin_x (optional) : The X location of the GPS with respect to origin of the robot.
origin_y (optional) : The Y location of the GPS with respect to origin of the robot.\
  note. both X and Y must be supplied
units (optional) : The units that X and Y location are specified in, default is MM
Returns:
An instance of the Gps class
Examples:
gps1 = Gps(Ports.PORT1)

acceleration(axis)

read the acceleration for one axis of the gps
Arguments:
axis : The axis to read
Returns:
A value for the acceleration of the axis in units of gravity.
Examples:
# get the acceleration for the Z axis of the gps\
zaccel = gps1.acceleration(ZAXIS)

calibrate()

not used on the GPS sensor

changed(callback, arg=())

Register a function to be called when the value of the gps heading changes

This is not particularly useful as gps heading is not stable and will cause many events.

Arguments:
callback : A function that will be called when the value of the gps heading changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("heading changed")

gps1.changed(foo)

get_turn_type()

get the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
None
Returns:
The current TurnType, LEFT or RIGHT

gyro_rate(axis, units=VelocityUnits.DPS)

read the gyro rate for one axis of the gps
Arguments:
axis : The axis to read
units (optional) : The units to return the gyro rate in, default is DPS
Returns:
A value for the gyro rate of the axis in the units specified.
Examples:
# get the gyro rate for the Z axis of the gps\
zrate = gps1.gyro_rate(ZAXIS)

heading(units=RotationUnits.DEG)

read the current heading of the gps

heading will be returned in the range 0 - 359.99 degrees

Arguments:
units (optional) : The units to return the heading in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current heading for the gps\
value = gps1.heading()

installed(*args)

Check for device connection
Arguments:
None
Returns:
True or False

is_calibrating()

not used on the GPS sensor

orientation(axis, units=RotationUnits.DEG)

read the orientation for one axis of the gps
Arguments:
axis : The axis to read
units (optional) : The units to return the orientation in, default is DEGREES
Returns:
A value for the axis orientation in the units specified.
Examples:
# get the pitch value for the gps\
pitch = gps1.orientation(OrientationType.PITCH)

quality()

read the current quality of the gps data

A quality of 100 indicates the gps can see the gps field strip and is returning good readings\ The value for quality will reduce as the confidence in x and y location lowers.

Arguments:
None
Returns:
A value of quality in the range 0 to 100
Examples:
# get the current location and heading quality for the gps\
q = gps1.quality()

reset_heading()

Reset the gps heading to 0
Arguments:
None
Returns:
None

reset_rotation()

Reset the gps rotation to 0
Arguments:
None
Returns:
None

rotation(units=RotationUnits.DEG)

read the current rotation of the gps

rotation is not limited, it can be both positive and negative and shows the absolute angle of the gps.

Arguments:
units (optional) : The units to return the rotation in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current rotation for the gps\
value = gps1.rotation()

set_heading(value, units=RotationUnits.DEG)

set the gps heading to a new value

The new value for heading should be in the range 0 - 359.99 degrees.

Arguments:
value : The new value to use for heading.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of heading to 180 degrees\
gps1.set_heading(180)

set_location(x, y, units=DistanceUnits.MM, angle=0, units_r=RotationUnits.DEG)

set the initial location of the robot

This gives a hint as to the location of the robot/gps sensor when it is first initialized.\ This can be used if in the initial position the gps cannot see the gps field strip.

Arguments:
x : The initial X coordinate.
y : The initial Y coordinate.\
  note. both X and Y must be supplied
units (optional) : The units that X and Y coordinates are specified in, default is MM
angle (optional) : The initial heading of the robot.
units_r (optional) : The units that angle is specified in, default is DEGREES
Returns:
None
Examples:
# set the initial location of the gps\
gps1.set_location(1000, -1000, MM, 90, DEGREES)

set_origin(x=0, y=0, units=DistanceUnits.MM)

set the origin of the gps sensor

An alternate way of setting sensor origin if not provided in the Gps class constructor.

Arguments:
x : The X location of the GPS with respect to origin of the robot.
y : The Y location of the GPS with respect to origin of the robot.\
  note. both X and Y must be supplied
units (optional) : The units that X and Y location are specified in, default is MM
Returns:
None
Examples:
# set the origin of the gps\
gps1.set_origin(6, -6, INCHES)

set_rotation(value, units=RotationUnits.DEG)

set the gps rotation to a new value
Arguments:
value : The new value to use for rotation.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of rotation to 180 degrees\
gps1.set_rotation(180)

set_sensor_rotation(value, units=RotationUnits.DEG)

set the sensor rotation of the gps sensor with respect to the robot.

This allows heading and rotation methods to return angles relative to the robot rather than the gps.

Arguments:
value : The angle of the GPS with respect to the robot.
units (optional) : The units that value is specified in, default is DEGREES
Returns:
None
Examples:
# set the sensor rotation of the gps\
gps1.set_sensor_rotation(180, DEGREES)

set_turn_type(turntype)

set the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
turntype : TurnType.LEFT or TurnType.RIGHT
Returns:
None

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

x_position(units=DistanceUnits.MM)

read the current x coordinate of the gps
Arguments:
units (optional) : The units to return the position in, default is MM
Returns:
A value for the x coordinate in the units specified.
Examples:
# get the current x coordinate for the gps\
posx = gps1.x_position()

y_position(units=DistanceUnits.MM)

read the current y coordinate of the gps
Arguments:
units (optional) : The units to return the position in, default is MM
Returns:
A value for the y coordinate in the units specified.
Examples:
# get the current y coordinate for the gps\
posy = gps1.y_position()

Inertial Sensor

vex.Inertial

Inertial class - a class for working with the inertial sensor
Arguments:
port : The smartport this device is attached to
Returns:
An instance of the Inertial class
Examples:
imu1 = Inertial(Ports.PORT1)

acceleration(axis)

read the acceleration for one axis of the inertial sensor
Arguments:
axis : The axis to read
Returns:
A value for the acceleration of the axis in units of gravity.
Examples:
# get the acceleration for the Z axis of the inertial sensor\
zaccel = imu1.acceleration(ZAXIS)

calibrate()

Start calibration of the inertial sensor

Calibration should done when the inertial sensor is not moving.

Arguments:
None
Returns:
None
Examples:
# start calibration\
imu1.calibrate()\
# wait for completion\
while imu1.is_calibrating():\
    sleep(50, MSEC)

changed(callback, arg=())

Register a function to be called when the value of the inertial sensor heading changes
Arguments:
callback : A function that will be called when the value of the inertial sensor heading changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("heading changed")

imu1.changed(foo)

collision(callback, arg=())

Register a function to be called when the inertial sensor detects a collision
Arguments:
callback : A function that will be called when the inertial sensor detects a collision
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("collision")

imu1.collision(foo)

get_turn_type()

get the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
None
Returns:
The current TurnType, LEFT or RIGHT

gyro_rate(axis, units=VelocityUnits.DPS)

read the gyro rate for one axis of the inertial sensor
Arguments:
axis : The axis to read
units (optional) : The units to return the gyro rate in, default is DPS
Returns:
A value for the gyro rate of the axis in the units specified.
Examples:
# get the gyro rate for the Z axis of the inertial sensor\
zrate = imu1.gyro_rate(ZAXIS)

heading(units=RotationUnits.DEG)

read the current heading of the inertial sensor

heading will be returned in the range 0 - 359.99 degrees

Arguments:
units (optional) : The units to return the heading in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current heading for the inertial sensor\
value = imu1.heading()

installed(*args)

Check for device connection
Arguments:
None
Returns:
True or False

is_calibrating()

check the calibration status of the inertial sensor

Calibration should done when the inertial sensor is not moving.

Arguments:
None
Returns:
True when the inertial sensor is calibrating
Examples:
# start calibration\
imu1.calibrate()\
# wait for completion\
while imu1.is_calibrating():\
    sleep(50, MSEC)

orientation(axis, units=RotationUnits.DEG)

read the orientation for one axis of the inertial sensor
Arguments:
axis : The axis to read
units (optional) : The units to return the orientation in, default is DEGREES
Returns:
A value for the axis orientation in the units specified.
Examples:
# get the pitch value for the inertial sensor\
pitch = imu1.orientation(OrientationType.PITCH)

reset_heading()

Reset the inertial sensor heading to 0
Arguments:
None
Returns:
None

reset_rotation()

Reset the inertial sensor rotation to 0
Arguments:
None
Returns:
None

rotation(units=RotationUnits.DEG)

read the current rotation of the inertial sensor

rotation is not limited, it can be both positive and negative and shows the absolute angle of the gps.

Arguments:
units (optional) : The units to return the rotation in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current rotation for the inertial sensor\
value = imu1.rotation()

set_heading(value, units=RotationUnits.DEG)

set the inertial sensor heading to a new value

The new value for heading should be in the range 0 - 359.99 degrees.

Arguments:
value : The new value to use for heading.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of heading to 180 degrees\
imu1.set_heading(180)

set_rotation(value, units=RotationUnits.DEG)

set the inertial sensor rotation to a new value
Arguments:
value : The new value to use for rotation.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of rotation to 180 degrees\
imu1.set_rotation(180)

set_turn_type(turntype)

set the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
turntype : TurnType.LEFT or TurnType.RIGHT
Returns:
None

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

Rotation Sensor

vex.Rotation

Rotation class - a class for working with the rotation sensor
Arguments:
port : The smartport this device is attached to
reverse (optional) : set to reverse the angle and position returned by the sensor.
Returns:
An instance of the Rotation class
Examples:
rot1 = Rotation(Ports.PORT1)\
rot2 = Rotation(Ports.PORT2, True)

angle(units=RotationUnits.DEG)

The current angle of the rotation sensor
Arguments:
units (optional) : A valid RotationUnits type, the default is DEGREES
Returns:
A value in the range that is specified by the units.
Examples:
# get rotation sensor angle            angle = rot1.angle()

changed(callback, arg=())

Register a function to be called when the value of the rotation sensor changes
Arguments:
callback : A function that will be called when the value of the rotation sensor changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("rotation changed")

rot1.changed(foo)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

position(units=RotationUnits.DEG)

Returns the position of the rotation sensor

The position is an absolute value that continues to increase or decrease as the\ sensor is rotated.

Arguments:
units (optional) : The units for the returned position, the default is DEGREES
Returns:
The rotation sensor in provided units

reset_position()

Reset the rotation sensor position to 0
Arguments:
None
Returns:
None

set_position(value, units=RotationUnits.DEG)

Set the current position of the rotation sensor

The position returned by the position() function is set to this value.

The position is an absolute value that continues to increase or decrease as the\ sensor is rotated.

Arguments:
value : The new position
units : The units for the provided position, the default is DEGREES
Returns:
None

set_reversed(value)

Set the reversed flag for the sensor

Usually this would be done in the constructor.

Arguments:
value : 1, 0, True or False
Returns:
None
Examples:
# set reversed flag True\
rot1.set_reversed(True)

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

velocity(units=VelocityUnits.RPM)

Returns the velocity of the rotation sensor
Arguments:
units (optional) : The units for the returned velocity, the default is RPM
Returns:
The rotation sensor velocity in provided units

Optical Sensor

vex.Optical

Optical class - a class for working with the optical sensor
Arguments:
port : The smartport this device is attached to
Returns:
An instance of the Optical class
Examples:
opt1 = Optical(Ports.PORT1)

brightness(readraw=False)

read the brightness value from the optical sensor
Arguments:
readraw (optional) : return raw brightness value if True rather than percentage.
Returns:
brightness as a float in the range 0 - 100%
Examples:
brightness = opt1.brightness()

color()

read the color from the optical sensor
Arguments:
None
Returns:
color as an instance of the Color class
Examples:
c = opt1.color()

gesture_disable()

Disable gesture mode
Arguments:
None
Returns:
None
Examples:
opt1.gesture_disable()

gesture_down(callback, arg=())

Register a function to be called when a gesture down event is detected

gesture must be enabled for events to fire.

Arguments:
callback : A function that will be called when a gesture down event is detected
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("down detected")

opt1.gesture_down(foo)

gesture_enable()

Enable gesture mode
Arguments:
None
Returns:
None
Examples:
opt1.gesture_enable()

gesture_left(callback, arg=())

Register a function to be called when a gesture left event is detected

gesture must be enabled for events to fire.

Arguments:
callback : A function that will be called when a gesture left event is detected
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("left detected")

opt1.gesture_left(foo)

gesture_right(callback, arg=())

Register a function to be called when a gesture right event is detected

gesture must be enabled for events to fire.

Arguments:
callback : A function that will be called when a gesture right event is detected
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("right detected")

opt1.gesture_right(foo)

gesture_up(callback, arg=())

Register a function to be called when a gesture up event is detected

gesture must be enabled for events to fire.

Arguments:
callback : A function that will be called when a gesture up event is detected
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("up detected")

opt1.gesture_up(foo)

get_gesture(newobject=False)

get gesture data
Arguments:
newobject (optional) : create a new Gesture object to return data in
Returns:
An object with the last gesture data
Examples:
opt1.gesture_disable()

hue()

read the hue value from the optical sensor
Arguments:
None
Returns:
hue as a float in the range 0 - 359.99 degrees
Examples:
hue = opt1.hue()

installed()

Check for device connection
Arguments:
None
Returns:
True or False

integration_time(value=-1)

set optical sensor led to the requested power
Arguments:
value (optional) : integration time in mS (5 to 700)
Returns:
The current integration time
Examples:
opt1.set_light_power(50)

is_near_object()

check to see if the optical proximity sensor detects an object
Arguments:
None
Returns:
True if near an object
Examples:
if opt1.is_near_object():
    print('near object')

object_detect_threshold(value)

set the threshold for object detection
Arguments:
value : Number in the range 0 to 255.  A value of 0 will just return current value.
Returns:
current value
Examples:
opt1.object_detect_threshold(100)

object_detected(callback, arg=())

Register a function to be called when an object detected event occurs
Arguments:
callback : A function that will be called when an object detected event occurs
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("object detected")

opt1.object_detected(foo)

object_lost(callback, arg=())

Register a function to be called when an object lost event occurs
Arguments:
callback : A function that will be called when an object lost event occurs
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("object lost")

opt1.object_lost(foo)

rgb(raw=False)

get the optical sensor rgb value
Arguments:
raw (optional) : return raw or processed values
Returns:
A tuple with red, green, blue and brightness
Examples:
value=opt1.rgb()

set_light(*args)

set optical sensor led on or of
Arguments:
value : LedStateType.ON, LedStateType.OFF or power of led, 0 to 100%
Returns:
None
Examples:
# turn on led with previous intensity\
opt1.set_light(LedStateType.ON)

# turn on led with new intensity\
opt1.set_light(65)

set_light_power(value)

set optical sensor led to the requested power
Arguments:
value : power of led, 0 to 100%
Returns:
None
Examples:
opt1.set_light_power(50)

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

Distance Sensor

vex.Distance

Distance class - a class for working with the distance sensor
Arguments:
port : The smartport this device is attached to
Returns:
An instance of the Distance class
Examples:
dist1 = Distance(Ports.PORT1)

changed(callback, arg=())

Register a function to be called when the distance value changes
Arguments:
callback : A function that will be called when the distance value changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("distance changed")

dist1.changed(foo)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

is_object_detected()

Returns if an object is detected
Arguments:
None
Returns:
True or False

object_distance(units=DistanceUnits.MM)

The current distance the sensor is reading.

The distance will return a large positive number if no object is detected.

Arguments:
units (optional): The distance units to return the distance value in.  default is MM.
Returns:
A value for distance in the specified units.
Examples:
# get distance in mm\
value = dist1.object_distance()

# get distance in inches\
value = dist1.object_distance(INCHES)

object_rawsize()

Get the raw value of object size the sensor is detecting.

Raw size will be a number ranging from 0 to about 400\ Larger and more reflective objects will return larger values.

Arguments:
None
Returns:
A value for object size that is a number.\
Examples:
# get object raw size\
size = dist1.object_rawsize()

object_size()

Get an estimation of the object size the sensor is detecting.
Arguments:
None
Returns:
A value for object size.\
The value will be of type ObjectSizeType
Examples:
# get object size\
size = dist1.object_size()

object_velocity()

Returns the object velocity

velocity is calculated from change of distance over time

Arguments:
None
Returns:
The velocity in m/s

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

Electromagnet

vex.Electromagnet

Electromagnet class - a class for working with the electromagnet
Arguments:
port : The smartport this device is attached to
Returns:
An instance of the Electromagnet class
Examples:
em1 = Electromagnet(Ports.PORT1)

drop(duration=1000, units=MSEC, power=50)

energize the electromagnet to drop objects
Arguments:
duration (optional) : the duration to energize the magnet for, default is 1 second
units (optional) : the units for duration, default is MSEC
power (optional) : the power used when energizing.
Returns:
None
Examples:
# drop with default values\
em1.drop()

# drop with custom values\
em1.drop(250, MSEC, 90)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

pickup(duration=1000, units=MSEC, power=50)

energize the electromagnet to pickup objects
Arguments:
duration (optional) : the duration to energize the magnet for, default is 1 second
units (optional) : the units for duration, default is MSEC
power (optional) : the power used when energizing.
Returns:
None
Examples:
# pickup with default values\
em1.pickup()

# pickup with custom values\
em1.pickup(250, MSEC, 90)

set_power(value)

set the default power to use for drop and pickup methods
Arguments:
value : power in range 0 to 100
Returns:
None
Examples:
# set default power to 80\
em1.set_power(80)

temperature(*args)

Returns the temperature of the electromagnet
Arguments:
units (optional) : The units for the returned temperature, the default is CELSIUS
Returns:
The electromagnet temperature in provided units

timestamp()

Request the timestamp of last received message from the sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

Accelerometer

vex.Accelerometer

Accelerometer class - create a new accelerometer

For full functionality, three Accelerometer instances would need to be created, one for each axis.

Arguments:
port : The 3wire port to use for the accelerometer
sensitivity (optional) : set high sensitivity mode (+/- 2G), use True or 1
Returns:
An instance of the Accelerometer class
Examples:
accx = Accelerometer(brain.three_wire_port.a)\
accy = Accelerometer(brain.three_wire_port.b)\
accz = Accelerometer(brain.three_wire_port.c)

acceleration()

The current value of the accelerometer scaled to units of gravity
Arguments:
None
Returns:
A value in the range +/- 6 or +/-2G if high sensitivity mode is set
Examples:
# get accelerometer in range+/- 6G
value = accz.acceleration()

changed(callback, arg=())

Register a function to be called when the value of the accelerometer changes
Arguments:
callback : A function that will be called when the value of the accelerometer changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("accelerometer changed")

accz.changed(foo)

value(units=AnalogUnits.TWELVEBIT)

The current value of the accelerometer
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get accelerometer in range 0 - 4095\
value = accz.value()

# get accelerometer in range 0 - 1023\
value = accz.value(AnalogUnits.TENBIT)

Analog Input

vex.AnalogIn

AnalogIn class - create a new analog input
Arguments:
port : The 3wire port to use for the analog input
Returns:
An instance of the AnalogIn class
Examples:
ana1 = AnalogIn(brain.three_wire_port.a)

changed(callback, arg=())

Register a function to be called when the value of the analog input changes
Arguments:
callback : A function that will be called when the value of the analog input changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("analog input changed")

ana1.changed(foo)

value(units=AnalogUnits.TWELVEBIT)

The current value of the analog input
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get analog input in range 0 - 4095\
value = ana1.value()

# get analog input in range 0 - 1023\
value = ana1.value(AnalogUnits.TENBIT)

Encoder

vex.Encoder

Encoder class - create a new encoder sensor

An encoder uses two adjacent 3wire ports.\ valid port pairs are a/b, c/d, e/f and g/h

Arguments:
port : The 3wire port to use for the encoder sensor
Returns:
An instance of the Encoder class
Examples:
enc1 = Encoder(brain.three_wire_port.a)

position(units=RotationUnits.DEG)

The current position of the encoder
Arguments:
units (optional) : The rotation units to return the position value in, default is DEGREES.
Returns:
A value for encoder position in the specified units.
Examples:
# get encoder position\
value = enc1.position()

reset_position()

Reset the encoder position to 0
Arguments:
None
Returns:
None

set_position(value, units=RotationUnits.DEG)

set the encoder position to a new value
Arguments:
value : The new value to use for position.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of position to 180 degrees\
enc1.set_position(180)

value()

The current value of the encoder in raw counts

One full turn of the encoder is 360 counts.

Arguments:
None
Returns:
A value for encoder counts.
Examples:
# get encoder raw counts\
value = enc1.value()

velocity(units=VelocityUnits.RPM)

The current velocity of the encoder
Arguments:
units (optional) : The velocity units to return the value in, default is RPM.
Returns:
A value for encoder velocity in the specified units.
Examples:
# get encoder velocity in rpm\
value = enc1.velocity()

Sonar

vex.Sonar

Sonar class - create a new sonar (ultrasonic) sensor

A sonar uses two adjacent 3wire ports.\ valid port pairs are a/b, c/d, e/f and g/h\ connect the wire labeled "output" to the lower 3wire port, eg. a

Arguments:
port : The 3wire port to use for the sonar sensor
Returns:
An instance of the Sonar class
Examples:
sonar1 = Sonar(brain.three_wire_port.a)

distance(units)

The current distance the sonar is detecting an object at.

The sonar will return a large positive number if no object is detected in range.

Arguments:
units : The distance units to return the position value in.
Returns:
A value for sonar distance in the specified units.
Examples:
# get sonar distance in mm\
value = sonar1.distance(MM)

found_object()

Check for an object in the range 0 - 1000mm

The sonar will return True if an object is detected closer than 1000mm.

Arguments:
None
Returns:
True of an object is detected.
Examples:
# is an object closer than 1000mm\
if sonar1.found_object():\
    print("object found")

value(units=AnalogUnits.TWELVEBIT)

The current value of the sonar

This method has no practical use, see distance.

Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get sonar raw value\
value = sonar1.value()

PWM

vex.Pwm

Pwm class - create a new pwm output

The pwm class will create raw RC style pwm waveform.\ A pwm output of 0% corresponds to pulse width of 1.5mS every 16mS\ A pwm output of 100% corresponds to pulse width of 2mS\ A pwm output of -100% corresponds to pulse width of 1mS

Arguments:
port : The 3wire port to use for the pwm output
Returns:
An instance of the Pwm class
Examples:
pwm1 = Pwm(brain.three_wire_port.a)

state(value, units=PercentUnits.PERCENT)

Set the current PWM value in percent.
Arguments:
value : The new value for pwm output, -100 to +100 percent.
units (optional) : units must be specified in PERCENT
Returns:
None
Examples:
# set pwm1 output to 50%\
pwm1.state(50)

value()

Read the current PWM value in percent.
Arguments:
None
Returns:
A value in the range -100 to +100 percent.
Examples:
# get pwm1 current value\
value = pwm1.value()

Servo

vex.Servo

Servo class - create a new servo output

The Servo class will create raw RC style pwm waveform.\ An output of 0 corresponds to pulse width of 1.5mS every 16mS\ An output of 50 degrees corresponds to pulse width of 2mS\ An output of -50 degrees corresponds to pulse width of 1mS

Arguments:
port : The 3wire port to use for the servo output
Returns:
An instance of the Servo class
Examples:
servo1 = Servo(brain.three_wire_port.a)

set_position(value, units=RotationUnits.DEG)

Set the servo position
Arguments:
value : The new value for the servo using the supplied units.
units (optional) : The rotation units, default is PERCENT
Returns:
None
Examples:
# set servo output to 10 degrees\
servo1.set_position(10, DEGREES)

value()

Read the current raw servo pwm value.

This is the raw internal pwm value\ A servo position of 0 will return 127\ A maximum positive servo position will return 255

Arguments:
None
Returns:
A value in the range 0 to 255.
Examples:
# get servo1 current value\
value = servo1.value()

Potentiometer

vex.Potentiometer

Potentiometer class - create a new potentiometer
Arguments:
port : The 3wire port to use for the potentiometer
Returns:
An instance of the Potentiometer class
Examples:
pot1 = Potentiometer(brain.three_wire_port.a)

angle(units=RotationUnits.DEG)

The current angle of the potentiometer
Arguments:
units (optional) : A valid RotationUnits type or PERCENT, the default is DEGREES
Returns:
A value in the range that is specified by the units.
Examples:
# get potentiometer in range 0 - 250 degrees\
angle = pot1.angle()

# get potentiometer in range 0 - 100%\
angle = pot1.angle(PERCENT)

changed(callback, arg=())

Register a function to be called when the value of the potentiometer changes
Arguments:
callback : A function that will be called when the value of the potentiometer changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("pot changed")

pot1.changed(foo)

value(units=AnalogUnits.TWELVEBIT)

The current value of the potentiometer
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get potentiometer in range 0 - 4095\
value = pot1.value()

# get potentiometer in range 0 - 1023\
value = pot1.value(AnalogUnits.TENBIT)

Potentiometer V2

vex.PotentiometerV2

PotentiometerV2 class - create a new potentiometer
Arguments:
port : The 3wire port to use for the potentiometer
Returns:
An instance of the PotentiometerV2 class
Examples:
pot1 = PotentiometerV2(brain.three_wire_port.a)

angle(units=RotationUnits.DEG)

The current angle of the potentiometer
Arguments:
units (optional) : A valid RotationUnits type or PERCENT, the default is DEGREES
Returns:
A value in the range that is specified by the units.
Examples:
# get potentiometer in range 0 - 250 degrees\
angle = pot1.angle()

# get potentiometer in range 0 - 100%\
angle = pot1.angle(PERCENT)

changed(callback, arg=())

Register a function to be called when the value of the potentiometer changes
Arguments:
callback : A function that will be called when the value of the potentiometer changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("pot changed")

pot1.changed(foo)

value(units=AnalogUnits.TWELVEBIT)

The current value of the potentiometer
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get potentiometer in range 0 - 4095\
value = pot1.value()

# get potentiometer in range 0 - 1023\
value = pot1.value(AnalogUnits.TENBIT)

Line Sensor

vex.Line

Line class - create a new line sensor
Arguments:
port : The 3wire port to use for the line sensor
Returns:
An instance of the Line class
Examples:
line1 = Line(brain.three_wire_port.a)

changed(callback, arg=())

Register a function to be called when the value of the line sensor changes
Arguments:
callback : A function that will be called when the value of the line sensor changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("line sensor changed")

line1.changed(foo)

reflectivity(units=PercentUnits.PERCENT)

The current reflectivity of the line sensor

The reflectivity of the line sensor is an estimation based on the raw value of the sensor.\ A reflectivity of 0% is a raw value of approximated 3000 or greater\ A reflectivity of 100% is a raw value of 0

Arguments:
units (optional) : The only valid value is PERCENT
Returns:
A value in the range 0 to 100%
Examples:
# get line sensor reflectivity in range of 0 -100%\
value = line1.reflectivity()

value(units=AnalogUnits.TWELVEBIT)

The current value of the line sensor
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get line sensor in range 0 - 4095\
value = line1.value()

# get line sensor in range 0 - 1023\
value = line1.value(AnalogUnits.TENBIT)

Light Sensor

vex.Light

Light class - create a new light sensor
Arguments:
port : The 3wire port to use for the light sensor
Returns:
An instance of the Light class
Examples:
light1 = Light(brain.three_wire_port.a)

brightness(units=PercentUnits.PERCENT)

The current brightness of light falling on the light sensor

The brightness of the light sensor is an estimation based on the raw value of the sensor.\ A brightness of 0% is a raw value of approximated 900 or greater\ A brightness of 100% is a raw value of 0

Arguments:
units (optional) : The only valid value is PERCENT
Returns:
A value in the range 0 to 100%
Examples:
# get light sensor brightness in range of 0 -100%\
value = light1.brightness()

changed(callback, arg=())

Register a function to be called when the value of the light sensor changes
Arguments:
callback : A function that will be called when the value of the light sensor changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("light sensor changed")

light1.changed(foo)

value(units=AnalogUnits.TWELVEBIT)

The current value of the light sensor
Arguments:
units (optional) : A valid AnalogUnits type or PERCENT, the default is 12 bit analog
Returns:
A value in the range that is specified by the units.
Examples:
# get light sensor in range 0 - 4095\
value = light1.value()

# get light sensor in range 0 - 1023\
value = light1.value(AnalogUnits.TENBIT)

Gyro

vex.Gyro

Gyro class - create a new gyro sensor
Arguments:
port : The 3wire port to use for the gyro sensor
Returns:
An instance of the Gyro class
Examples:
gyro1 = Gyro(brain.three_wire_port.a)

calibrate()

Start calibration of the gyro

Calibration should done when the gyro is not moving.

Arguments:
None
Returns:
None
Examples:
# start calibration\
gyro1.calibrate()\
# wait for completion\
while gyro1.is_calibrating():\
    sleep(50, MSEC)

changed(callback, arg=())

Register a function to be called when the value of the gyro heading changes
Arguments:
callback : A function that will be called when the value of the gyro heading changes
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("gyro changed")

gyro1.changed(foo)

get_turn_type()

get the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
None
Returns:
The current TurnType, LEFT or RIGHT

heading(units=RotationUnits.DEG)

read the current heading of the gyro

heading will be returned in the range 0 - 359.99 degrees

Arguments:
units (optional) : The units to return the heading in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current heading for the gyro\
value = gyro1.heading()

is_calibrating()

check the calibration status of the gyro

Calibration should done when the gyro is not moving.

Arguments:
None
Returns:
True when the gyro is calibrating
Examples:
# start calibration\
gyro1.calibrate()\
# wait for completion\
while gyro1.is_calibrating():\
    sleep(50, MSEC)

reset_heading()

Reset the gyro heading to 0
Arguments:
None
Returns:
None

reset_rotation()

Reset the gyro rotation to 0
Arguments:
None
Returns:
None

rotation(units=RotationUnits.DEG)

read the current rotation of the gyro

rotation is not limited, it can be both positive and negative and shows the absolute angle of the gyro.

Arguments:
units (optional) : The units to return the rotation in, default is DEGREES
Returns:
A value for heading in the range that is specified by the units.
Examples:
# get the current rotation for the gyro\
value = gyro1.rotation()

set_heading(value, units=RotationUnits.DEG)

set the gyro heading to a new value

The new value for heading should be in the range 0 - 359.99 degrees.

Arguments:
value : The new value to use for heading.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of heading to 180 degrees\
gyro1.set_heading(180)

set_rotation(value, units=RotationUnits.DEG)

set the gyro rotation to a new value
Arguments:
value : The new value to use for rotation.
units (optional) : The rotation units type for value, the default is DEGREES
Returns:
None
Examples:
# set the value of rotation to 180 degrees\
gyro1.set_rotation(180)

set_turn_type(turntype)

set the direction that returns positive values for heading

An advanced function that is not generally used.

Arguments:
turntype : TurnType.LEFT or TurnType.RIGHT
Returns:
None

value(units=DEGREES)

The current value of the gyro

This method is generally not used, see heading() and rotation()

Arguments:
units (optional) : A valid RotationUnits type or PERCENT, the default is DEGREES
Returns:
A value in the range that is specified by the units.
Examples:
# get gyro value in range 0 - 360 degrees\
value = gyro1.value()

Limit Switch

vex.Limit

Limit class - create a new limit switch
Arguments:
port : The 3wire port the limit switch is connected to
Returns:
An instance of the Limit class
Examples:
limit1 = Limit(brain.three_wire_port.a)

pressed(callback, arg=())

Register a function to be called when the limit switch is pressed
Arguments:
callback : A function that will be called when the limit switch is pressed
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("switch pressed")

limit1.pressed(foo)

pressing()

Returns whether the limit switch is currently being pressed
Arguments:
None
Returns:
True or False

released(callback, arg=())

Register a function to be called when the limit switch is released
Arguments:
callback : A function that will be called when the limit switch is released
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("switch released")

limit1.released(foo)

value()

The current value of the limit switch
Arguments:
None
Returns:
1 or 0

Bumper

vex.Bumper

Bumper class - create a new bumper switch
Arguments:
port : The 3wire port the bumper switch is connected to
Returns:
An instance of the Bumper class
Examples:
bumper1 = Bumper(brain.three_wire_port.a)

pressed(callback, arg=())

Register a function to be called when the bumper switch is pressed
Arguments:
callback : A function that will be called when the bumper switch is pressed
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("switch pressed")

bumper1.pressed(foo)

pressing()

Returns whether the bumper switch is currently being pressed
Arguments:
None
Returns:
True or False

released(callback, arg=())

Register a function to be called when the bumper switch is released
Arguments:
callback : A function that will be called when the bumper switch is released
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("switch released")

bumper1.released(foo)

value()

The current value of the bumper switch
Arguments:
None
Returns:
1 or 0

Digital Input

vex.DigitalIn

DigitalIn class - create a new digital input
Arguments:
port : The 3wire port to use for the digital input
Returns:
An instance of the DigitalIn class
Examples:
dig1 = DigitalIn(brain.three_wire_port.a)

high(callback, arg=())

Register a function to be called when the digital input goes to the logic high state
Arguments:
callback : A function that will be called when the digital input goes to the logic high state
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("input high")

dig1.high(foo)

low(callback, arg=())

Register a function to be called when the digital input goes to the logic low state
Arguments:
callback : A function that will be called when the digital input goes to the logic low state
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("input low")

dig1.low(foo)

value()

The current value of the digital input
Arguments:
None
Returns:
1 or 0

Digital Output

vex.DigitalOut

DigitalOut class - create a new digital output
Arguments:
port : The 3wire port to use for the digital output
Returns:
An instance of the DigitalOut class
Examples:
dig1 = DigitalOut(brain.three_wire_port.a)

set(value)

Set the output level for the digital output
Arguments:
value : 0, 1, True or False
Returns:
None
Examples:
dig1.set(True)

value()

The current value of the digital output
Arguments:
None
Returns:
1 or 0

LED

vex.Led

Led class - create a new led
Arguments:
port : The 3wire port to use for the led
Returns:
An instance of the Led class
Examples:
led1 = Led(brain.three_wire_port.a)

off()

Turn the led off
Arguments:
None
Returns:
None
Examples:
led1.off()

on()

Turn the led on
Arguments:
None
Returns:
None
Examples:
led1.on()

value()

The current value of the led
Arguments:
None
Returns:
1 or 0

Pneumatics

vex.Pneumatics

Pneumatics class - create a new pneumatics driver class
Arguments:
port : The 3wire port to use for the pneumatics
Returns:
An instance of the Pneumatics class
Examples:
p1 = Pneumatics(brain.three_wire_port.a)

close()

Set the pneumatics driver to the close state
Arguments:
None
Returns:
None
Examples:
p1.close()

open()

Set the pneumatics driver to the open state
Arguments:
None
Returns:
None
Examples:
p1.open()

value()

The current state of the pneumatics driver
Arguments:
None
Returns:
1 or 0

Vision Sensor

vex.Vision

Vision class - a class for working with the vision sensor
Arguments:
port : The smartport this device is attached to
brightness (optional) : set the brightness value for the vision sensor
sigs (optional) : one or more signature objects
Returns:
An instance of the Vision class
Examples:
SIG_1 = Signature(1, 6035, 7111, 6572, -1345, -475, -910, 3.000, 0)\
vision1 = Vision(Ports.PORT1, 50, SIG_1)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

take_snapshot(index, count=1)

Request the vision sensor to filter latest objects to match signature or code
Arguments:
index : A signature, code or signature id.
count (optional) : the maximum number of objects to obtain.  default is 1.
Returns:
tuple of VisionObject or None if nothing is available
Examples:
# look for and return 1 object matching SIG_1\
objects = vision1.take_snapshot(SIG_1)

# look for and return a maximum of 4 objects matching SIG_1\
objects = vision1.take_snapshot(SIG_1, 4)

timestamp()

Request the timestamp of last received message from the vision sensor
Arguments:
None
Returns:
timestamp of the last status packet in mS

Vision Object

vex.VisionObject

A vision object, not instantiated by user programs


Signature

vex.Signature

Signature class - a class for holding vision sensor signatures
Arguments:
index : The signature index
p0 : signature value p0
p1 : signature value p1
p2 : signature value p2
p3 : signature value p3
p4 : signature value p4
p5 : signature value p5
sigrange : signature range
sigtype : signature type
Returns:
An instance of the Signature class
Examples:
SIG_1 = Signature(1, 6035, 7111, 6572, -1345, -475, -910, 3.000, 0)\
vision1 = Vision(Ports.PORT1, 50, SIG_1)

id()

Not used, always returns 0


Code

vex.Code

Code class - a class for holding vision sensor codes

A vision code is a collection of up to five vision signatures.

Arguments:
sig1 : A vision signature
sig2 : A vision signature
sig3 (optional) : A vision signature
sig4 (optional) : A vision signature
sig5 (optional) : A vision signature
Returns:
An instance of the Signature class
Examples:
SIG_1 = Signature(1, 6035, 7111, 6572, -1345, -475, -910, 3.000, 0)\
SIG_2 = Signature(2, 6035, 7111, 6572, -1345, -475, -910, 3.000, 0)\
C1 = Code(SIG_1, SIG_2)

id()

Not used, always returns 0


port : The smartport the VEXlink radio is attached to
name : The name of this link
linktype : The type of this link, either VexlinkType.MANAGER or VexlinkType.WORKER
wired (optional) : Set to True if this is a wired link
An instance of the MessageLink class
link = MessageLink(Ports.PORT1, 'james', VexlinkType.MANAGER)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

is_linked()

Arguments:
None
Returns:
True if the link is active and connected to the paired brain.

receive(timeout=300000)

Receive the next message
Arguments:
timeout (optional) : An optional timeout value in mS before the function returns.
Returns:
None or received message
Examples:
message = link.receive()

received(*args)

Register a function to be called when a message is received

If the message is omitted then the callback will be called for all messages.

Arguments:
message (optional) : A message name for which the callback will be called
callback : A function that will be called when a message is received
Returns:
None
Examples:
def cb(message, link, index, value):
    print(link, message, index, value)

link.received('test', cb)

send(message, *args)

Send a message with optional parameters
Arguments:
message : A string, the message to send
index (optional) : A int such as port number
value (optional) : A float
Returns:
length of transmitted data or None on error
Examples:
# send the message 'test' with no parameters\
link.send('test')

# send the message 'test' with parameters\
link.send('test', 1, 3.14)

port : The smartport the VEXlink radio is attached to
name : The name of this link
linktype : The type of this link, either VexlinkType.MANAGER or VexlinkType.WORKER
wired (optional) : Set to True if this is a wired link
An instance of the SerialLink class
link = SerialLink(Ports.PORT1, 'james', VexlinkType.MANAGER)

installed()

Check for device connection
Arguments:
None
Returns:
True or False

is_linked()

Arguments:
None
Returns:
True if the link is active and connected to the paired brain.

receive(length, timeout=300000)

Arguments:
length : maximum amount of data to wait for
timeout (optional) : An optional timeout value in mS before the function returns.
Returns:
None or bytearray with data
Examples:
# wait for 128 bytes of data for 1000mS\
buffer = link.receive(128, 1000)

received(callback)

Register a function to be called when data is received

This will receive a bytearray and a length indicating how much

Arguments:
callback : A function that will be called when data is received
Returns:
None
Examples:
def cb(buffer, length):
    print(buffer, length)

link.received(cb)

send(buffer)

Send a buffer of length length
Arguments:
buffer : A string or bytearray, the message to send
Returns:
None
Examples:
# send the string 'test'\
link.send('test')

# send the bytearray 'test' with parameters\
link.send('test', 1, 3.14)

Addressable LED

vex.AddressableLed

Addressable led class
Arguments:
port : The 3wire port to use for the addressable led strip
Returns:
An instance of the AddressableLed class
Examples:
addr1 = AddressableLed(brain.three_wire_port.a)

clear()

clear all addressable led to off
Arguments:
None
Returns:
None
Examples:
addr1.clear()

set(data, offset=0)

Set the addressable led strip to provided values
Arguments:
data : An list of Color values
offset (optional) : index of led to start at, 0 based
Returns:
None
Examples:
addr1 = AddressableLed(brain.three_wire_port.a)\
pix = [Color(0x800000),Color(0x008000),Color(0x000080)]\
addr1.set(pix)