óptico#

Para que los comandos ópticos aparezcan en VEXcode V5, se debe configurar un sensor óptico en la ventana Dispositivos.

Para obtener más información, consulte estos artículos:

Inicializando la clase óptica#

Un sensor óptico se crea utilizando el siguiente constructor:

The optical constructor creates an optical object.

Parámetro

Descripción

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.

Métodos de clase#

hue()#

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

Devuelve: Un doble que representa el valor del tono detectado por el sensor óptico como un flotante en el rango 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.

Parámetros

Descripción

bRaw

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

Devuelve: Un doble que representa el brillo detectado por el sensor óptico como un flotante en el rango 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.

Devuelve: El color detectado por el sensor óptico como una instancia de la clase de color.

// 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.

Parámetros

Descripción

value

A valid ledStateType.

Devoluciones: Ninguna.

// 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.

Parámetros

Descripción

value

El nivel de potencia para configurar la luz de 0 a 100.

units

The only valid unit for reflectivity is percent.

Devoluciones: Ninguna.

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

integrationTime()#

Este método se llama de las siguientes maneras:

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

Parámetros

Descripción

value

El tiempo de integración en milisegundos de 5 a 700.

Devoluciones: Ninguna.

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

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

Devuelve: Un doble que representa el tiempo de integración del sensor óptico.

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

getRgb()#

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

Parámetros

Descripción

raw

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

Devuelve: Una tupla con valores de rojo, verde, azul y brillo.

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

objectDetectThreshold()#

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

Parámetros

Descripción

value

Un número en el rango 0 - 255. Un valor de 0 solo devolverá el valor actual.

Devuelve: El valor actual del umbral de detección de objetos.

// 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.

Parámetros

Descripción

callback

La función de devolución de llamada que se llamará cuando se pierda un objeto.

Devoluciones: Ninguna.

// 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.

Devuelve: Marca de tiempo del último paquete de estado como un entero de 32 bits sin signo en milisegundos.

installed()#

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

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