optical#

To make optical commands appear in VEXcode V5, an Optical Sensor must be configured in the Devices window.

For more information, refer to these articles:

Initializing the optical Class#

An Optical Sensor is created by using the following constructor:

The optical constructor creates an optical object.

Parameter

Description

port

A valid Smart Port that the Optical Sensor is connected to.

// Construct an Optical Sensor "Optical" with the
// optical class.
optical Optical = optical(PORT1);

This Optical object will be used in all subsequent examples throughout this API documentation when referring to optical class methods.

Class Methods#

hue()#

The hue() method returns the value of the hue detected by the Optical Sensor.

Returns: A double representing the value of the hue detected by the Optical Sensor as a float in the range 0 - 359.99.

// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();

brightness()#

The brightness(bRaw) method returns the brightness detected by the Optical Sensor.

Parameters

Description

bRaw

A boolean value to read raw brightness data instead of percentage. The default is false.

Returns: A double representing the brightness detected by the Optical Sensor as a float in the range 0 - 100%.

// Set a variable, brightness, to the value of the brightness
// detected by the Optical Sensor.    
double brightness = Optical.brightness();

// Print the brightness detected by the Optical Sensor to the
// Brain's screen.
brain.screen.print(brightness);

color()#

The color() method returns the color detected by the Optical Sensor.

Returns: The color detected by the Optical Sensor as an instance of the color class.

// Set a variable, detectColor, to the color detected by the
// Optical Sensor.
color detectColor = Optical.color();

// Print the color detected by the Optical Sensor
// to the Brain's screen.
brain.screen.print(detectColor);

isNearObject()#

The isNearObject() method returns if the Optical Sensor detects a nearby object.

Returns: true if a nearby object is detected. false if one is not.

// If an object is detected yb the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
  Brain.Screen.print("near object");
}

setLight()#

The setLight(value) method sets Optical Sensor’s LED to on or off.

Parameters

Description

value

A valid ledStateType.

Returns: None.

// Turn on LED with previous intensity.
Optical.setLight(ledState::on);

setLightPower()#

The setLightPower(value) method sets the light power of the Optical Sensor’s LED.

Parameters

Description

value

The power level to set the light from 0 - 100.

units

The only valid unit for reflectivity is percent.

Returns: None.

// Set the light power to 50 percent.
Optical.setLightPower(50, percent);

integrationTime()#

This method is called in the following ways:

The integrationTime(value) method sets the Optical Sensor’s integration time.

Parameters

Description

value

The integration time in milliseconds from 5 - 700.

Returns: None.

// Set the integration time to 50 milliseconds.
Optical.integrationTime(50);

The integrationTime() method gets the Optical Sensor’s integration time.

Returns: A double representing the Optical Sensor’s integration time.

// Get the Optical Sensor's integration time.
double intTime = Optical.integrationTime();

getRgb()#

The getRgb(raw) method returns the Optical Sensor’s RGB value.

Parameters

Description

raw

A boolean value to determine if you return raw or processed values. The default is true.

Returns: A tuple with red, green, blue, and brightness values.

// Get the RGB value of the Optical Sensor.
optical::rgbc value = Optical.rgb();

objectDetectThreshold()#

The objectDetectThreshold(value) method sets the object detection threshold.

Parameters

Description

value

A number in the range 0 - 255. A value of 0 will just return the current value.

Returns: The current value of the object detection threshold.

// Set the object detection threshold to 100.
int value = Optical.objectDetectThreshold(100);

objectLost()#

The objectLost(callback) method registers a callback function for when an object is lost.

Parameters

Description

callback

The callback function to be called when an object is lost.

Returns: None.

// Define the lost function with a void return type,
// showing it doesn't return a value.
void lost() {
  // The Brain will print that the Optical Sensor lost an
  // object to the Brain's screen.
  Brain.Screen.print("object lost");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run lost when the Optical Sensor loses an object.
  Optical.objectLost(lost);
}

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the Optical Sensor.

Returns: Timestamp of the last status packet as an unsigned 32-bit integer in milliseconds.

installed()#

The installed() method checks if the Optical Sensor is connected.

Returns: true if the Optical Sensor is connected. false if it is not.