Sensor óptico#

Introducción#

The optical class is used to access data from the V5 Optical Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.

Constructor de clases#

optical Optical = optical(index);

Instructor de clase#

Destroys the optical object and releases associated resources.

virtual ~optical();

Parámetros#

Parámetro

Tipo

Descripción

index

int32_t

The Smart Port that the Optical Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1).

Ejemplos#

// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);

Funciones de los miembros#

The optical class includes the following member functions:

  • setLight — Turns the Optical Sensor’s LED on or off.

  • setLightPower — Sets the Optical Sensor’s LED to a specific brightness.

  • isNearObject — Returns whether or not an object is close to the sensor.

  • color — Returns the detected color as a predefined color object.

  • brightness — Returns the detected brightness of an object.

  • hue — Returns the detected hue value.

  • objectDetected — Registers a function to be called when the Optical Sensor detects an object.

  • objectLost — Registers a function to be called when the Optical Sensor loses an object.

Before calling any optical member functions, an optical instance must be created, as shown below:

// Create an optical object in Port 1
optical Optical1 = optical(PORT1);

setLight#

Configura el LED del sensor óptico para que se encienda o se apague.

Available Functions
void  setLight(ledState state);

Parameters

Parámetro

Tipo

Descripción

state

ledState

A valid ledState:

  • ledState::off
  • ledState::on

Return Values

Esta función no devuelve ningún valor.

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

setLightPower#

Configura la potencia lumínica del LED del sensor óptico.

Available Functions
void setLightPower( 
      int32_t      intensity, 
      percentUnits units = percent );

Parameters

Parámetro

Tipo

Descripción

intensity

int32_t

Nivel de potencia para ajustar la luz de 0 a 100.

units

percentUnits

The unit that represents the light intensity:

  • percent / pct — percent

Return Values

Esta función no devuelve ningún valor.

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

isNearObject#

Devuelve un valor si el sensor óptico detecta un objeto cercano.

Available Functions
bool  isNearObject();

Parameters

Esta función no acepta ningún parámetro.

Return Values

Esta función devuelve un valor booleano que indica si se ha detectado un objeto cercano:

  • true — An object is detected nearby.

  • false — An object is not detected.

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

color#

Devuelve el color detectado por el sensor óptico.

Available Functions
vex::color  color();

Parameters

Esta función no acepta ningún parámetro.

Return Values

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

Examples
// 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);

brightness#

Devuelve el brillo detectado por el sensor óptico.

Available Functions
double  brightness(bool bRaw = false);

Parameters

Parámetro

Tipo

Descripción

bRaw

bool

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

Return Values

Un valor double que representa el brillo detectado por el sensor óptico como un valor flotante en el rango de 0 a 100 %, o un valor double que representa los datos brutos detectados por el sensor.

Examples
// 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);

hue#

Devuelve el valor del tono detectado por el sensor óptico.

Available Functions
double hue();

Parameters

Esta función no acepta ningún parámetro.

Return Values

Un valor double que representa el valor del tono detectado por el sensor óptico como un número flotante en el rango de 0 a 359,99.

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

objectDetected#

Registra una función de devolución de llamada para cuando se detecta un objeto.

Available Functions
void objectDetected(void (* callback)(void));

Parameters

Parámetro

Tipo

Descripción

callback

void (*)(void)

La función de devolución de llamada que se invocará cuando se detecte un objeto.

Return Values

Esta función no devuelve ningún valor.

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

// Run lost when the Optical Sensor detects an object.
Optical.objectDetected(detected);

objectLost#

Registra una función de devolución de llamada para cuando se pierde un objeto.

Available Functions
void objectLost(void (* callback)(void));

Parameters

Parámetro

Tipo

Descripción

callback

void (*)(void)

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

Return Values

Esta función no devuelve ningún valor.

Examples
// 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");
}

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