Sensor de visión#

Introducción#

The vision class is used to control and access data from the V5 Vision Sensor. This sensor detects and tracks Color Signatures within its field of view, allowing your robot to identify targets and respond to visual information in real time.

El sensor de visión puede detectar varios objetos característicos simultáneamente y proporciona información como la posición, el ancho, la altura y las coordenadas del centro del objeto.

Constructores de clases#

vision(
  int32_t  index,
  uint8_t  bright,
  Args&... sigs );

Instructor de clase#

Destroys the vision object and releases associated resources.

~vision();

Parámetros#

Parámetro

Tipo

Descripción

index

int32_t

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

bright

uint8_t

El nivel de brillo del sensor de visión.

sigs

vision::signature or vision::code objects

One or more vision::signature or vision::code objects to register with the sensor.

Ejemplos#

// Create Color Signatures
vision::signature Vision1__REDBOX = vision::signature(
    1,      // signature id
    10121,  // uMin
    10757,  // uMax
    10439,  // uMean
    -1657,  // vMin
    -1223,  // vMax
    -1440,  // vMean
    2.5,    // range
    1 );    // type

vision::signature Vision1__BLUEBOX = vision::signature(
    2,      // signature id
    -4479,  // uMin
    -3277,  // uMax
    -3878,  // uMean
    5869,   // vMin
    7509,   // vMax
    6689,   // vMean
    2.5,    // range
    1 );    // type

// Create a Color Code from two signatures
vision::code Vision1__BOXCODE = vision::code(
    Vision1__REDBOX,  // first signature
    Vision1__BLUEBOX  // second signature
);

// Create the Vision Sensor instance
vision Vision1 = vision(
    PORT1,                  // Smart Port
    50,                     // brightness
    Vision1__REDBOX,        // signature
    Vision1__BOXCODE );     // Color Code

Funciones de los miembros#

The vision class includes the following member functions:

  • takeSnapshot — Captures data for a specific Color Signature or Color Code.

  • installed — Whether the Vision Sensor is connected to the V5 Brain.

To access detected object data after calling takeSnapshot, use the available Properties.

Before calling any vision member functions, a vision instance must be created, as shown below:

/* This constructor is required when using VS Code.
Vision Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */


// Create Color Signatures
vision::signature Vision1__REDBOX = vision::signature(
    1,      // signature id
    10121,  // uMin
    10757,  // uMax
    10439,  // uMean
    -1657,  // vMin
    -1223,  // vMax
    -1440,  // vMean
    2.5,    // range
    1 );    // type

vision::signature Vision1__BLUEBOX = vision::signature(
    2,      // signature id
    -4479,  // uMin
    -3277,  // uMax
    -3878,  // uMean
    5869,   // vMin
    7509,   // vMax
    6689,   // vMean
    2.5,    // range
    1 );    // type

// Create a Color Code from two signatures
vision::code Vision1__BOXCODE = vision::code(
    Vision1__REDBOX,  // first signature
    Vision1__BLUEBOX  // second signature
);

// Create the Vision Sensor instance
vision Vision1 = vision(
    PORT1,                  // Smart Port
    50,                     // brightness
    Vision1__REDBOX,        // signature
    Vision1__BOXCODE );     // Color Code

takeSnapshot#

Captures an image from the Vision Sensor, processes it based on the signature, and updates the objects array. This method can also limit the amount of objects captured in the snapshot.

The objects array stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. If no matching objects are detected, objectCount will be 0 and objects[i].exists will be false.

Available Functions

1 Toma una instantánea y busca objetos que coincidan con el ID de firma especificado.

int32_t takeSnapshot( uint32_t id );

2 Takes a snapshot and searches for objects matching the specified vision::code object.

int32_t takeSnapshot( code &cc );

3 Takes a snapshot and searches for objects matching the specified vision::signature object.

int32_t takeSnapshot( signature &sig );

4 Toma una instantánea y almacena solo el mayor número especificado de objetos que coincidan con el ID de firma.

int32_t takeSnapshot( uint32_t id, uint32_t count );

5 Takes a snapshot and stores only the largest specified number of objects matching the specified vision::code object.

int32_t takeSnapshot( code &cc, uint32_t count );

6 Takes a snapshot and stores only the largest specified number of objects matching the specified vision::signature object.

int32_t takeSnapshot( signature &sig, uint32_t count );

Parameters

Parámetro

Tipo

Descripción

id

uint32_t

El número de identificación de la firma que se debe buscar.

cc

code &

A reference to a vision::code object to search for.

sig

signature &

A reference to a vision::signature object to search for.

count

uint32_t

El número máximo de objetos a detectar.

Return Values

Returns an int32_t representing the number of matching objects detected.

Notes
  • El sensor de visión debe tomar una instantánea antes de que se pueda acceder a los datos del objeto.

  • When count is specified, only the largest detected objects (up to the specified amount) are stored.

Ejemplos

while (true) {
  // Display if a blue object is detected
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Blue detected");
  } 
  else {
    Brain.Screen.print("No blue");
  }

  wait(0.5, seconds);
}

// Display when a blue object is detected
while (true) {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearLine(1);

  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Color detected!");
  }

  wait(100, msec); 
}

// Display when BOXCODE is detected
while (true) {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearLine(1);

  Vision1.takeSnapshot(Vision1__BOXCODE);

  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Code detected!");
  }
  wait( 100, msec);
}

installed#

Indica si el sensor de visión está conectado al procesador V5.

Available Functions
bool installed();

Parameters

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

Return Values

Devuelve un valor booleano que indica si el sensor de visión está conectado:

  • true — The Vision Sensor is connected.
  • false — The Vision Sensor is not connected.

Propiedades#

Calling takeSnapshot updates the Vision Sensor’s detection results. Each snapshot refreshes the objects array, which contains the detected objects for the requested Color Signature or Color Code.

Vision Sensor data is accessed through properties of objects stored in Vision1.objects[index], where index begins at 0.

La resolución de imagen del sensor de visión es de 316 por 212 píxeles. La posición y el tamaño del objeto se muestran en unidades de píxeles con respecto a la vista actual del sensor.

Las siguientes propiedades están disponibles:

  • largestObject — Selects the largest detected object from the most recent snapshot.

  • objectCount — Returns the number of valid objects stored in objects.

  • objects — Array of detected objects updated by takeSnapshot.

  • .exists — Whether the object entry contains valid data.

  • .width — Width of the detected object in pixels.

  • .height — Height of the detected object in pixels.

  • .centerX — X position of the object’s center in pixels.

  • .centerY — Y position of the object’s center in pixels.

  • .originX — X position of the object’s top-left corner in pixels.

  • .originY — Y position of the object’s top-left corner in pixels.

  • .angle — Orientation of the detected Color Code in degrees (when applicable).

largestObject#

Retrieves the largest detected object from the objects array.

This method can be used to always get the largest object from objects without specifying an index.

Syntax
VisionSensor.largestObject

Components

Componente

Descripción

VisionSensor

El nombre de su instancia de Vision Sensor.

Ejemplos

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

  // Display the largest detected object's width
  while (true) {
    Vision1.takeSnapshot(Vision1__BLUEBOX);

    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);

    if (Vision1.objects[0].exists) {
      Brain.Screen.print("%d", Vision1.largestObject.width);
    } 
    wait(0.5, seconds);
  }
}

objectCount#

Returns the number of items inside the objects array as an integer.

Syntax
VisionSensor.objectCount

Components

Componente

Descripción

VisionSensor

El nombre de su instancia de Vision Sensor.

Ejemplos

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

  // Display how many blue objects are detected
  while (true) {
    Vision1.takeSnapshot(Vision1__BLUEBOX);

    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);

    Brain.Screen.print("Object Count: %d", Vision1.objectCount);

    wait(0.5, seconds);
  }
}

objects#

objects is an array containing the detected objects from the most recent call to takeSnapshot.

Each element in the array represents a detected object and provides access to its properties, such as width, height, centerX, and more.

Objects are ordered from largest to smallest (by width), beginning at index 0.

Syntax
VisionSensor.objects[index].property

Components

Componente

Descripción

VisionSensor

El nombre de su instancia de Vision Sensor.

index

The object index in the array. Index begins at 0.

property

Una de las propiedades del objeto.

.existe#

Indica si el índice de objeto especificado contiene un objeto válido detectado.

Acceso

SensorName.objects[index].exists

Valores de retorno

Devuelve un valor booleano que indica si el índice de objeto especificado contiene un objeto válido detectado:

  • true — A valid object exists at the specified index.
  • false — No object exists at the specified index.

Ejemplos

// Display when a blue object is detected
while (true) {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearLine(1);

  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Color detected!");
  }

  wait(100, msec); 
}

.ancho#

Devuelve el ancho del objeto detectado.

Acceso

SensorName.objects[index].width

Valores de retorno

Returns an int representing the width of the detected object in pixels. The value ranges from 1 to 316.

Ejemplos

// Approach an object until it's at least 100 pixels wide
while (true) {
  Vision1.takeSnapshot(Vision1__BLUEBOX);
  if (Vision1.objects[0].exists && Vision1.objects[0].width < 100) {
    Drivetrain.driveFor(forward, 10, mm);
  } 
  else {
    Drivetrain.stop();
  }
  wait(0.5, seconds);
}

.altura#

Devuelve la altura del objeto detectado.

Acceso

SensorName.objects[index].height

Valores de retorno

Returns an int representing the height of the detected object in pixels. The value ranges from 1 to 212.

Ejemplos

// Approach an object until it's at least 100 pixels tall
while (true) {
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists && Vision1.objects[0].height < 100) {
    Drivetrain.driveFor(forward, 10, mm);
  } 
  else {
    Drivetrain.stop();
  }
  wait(0.5, seconds);  // Avoid over-processing
}

.centerX#

Devuelve la coordenada x del centro del objeto detectado.

Acceso

SensorName.objects[index].centerX

Valores de retorno

Returns an int representing the x-coordinate of the object’s center in pixels. The value ranges from 0 to 316.

Ejemplos

// Turn until an object is directly in front of the sensor
Drivetrain.setTurnVelocity(10, percent);
Drivetrain.turn(right);
while (true) {
  Vision1.takeSnapshot(Vision1__BLUEBOX);
  if (Vision1.objects[0].exists) {
    int centerX = Vision1.largestObject.centerX;
    if (140 < centerX && centerX < 180) {
      Drivetrain.stop();
    }
  }
  wait(0.5, seconds);
}

.centroY#

Devuelve la coordenada y del centro del objeto detectado.

Acceso

SensorName.objects[index].centerY

Valores de retorno

Returns an int representing the y-coordinate of the object’s center in pixels. The value ranges from 0 to 212.

Ejemplos

// Approach an object until it's at least 90 pixels tall
while (true) {
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists) {
    if (Vision1.objects[0].centerY < 90) {
      Drivetrain.drive(forward);
    } else {
      Drivetrain.stop();
    }
  } else {
    Drivetrain.stop();
  }
  wait(50, msec);
}

.originX#

Devuelve la coordenada x de la esquina superior izquierda del cuadro delimitador del objeto detectado.

Acceso

SensorName.objects[index].originX

Valores de retorno

Returns an int representing the x-coordinate of the top-left corner of the object’s bounding box in pixels. The value ranges from 0 to 316.

Ejemplos

// Display if an object is to the left or the right
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Vision1.takeSnapshot(Vision1__BLUEBOX);
  if (Vision1.objects[0].exists) {
    if (Vision1.objects[0].originX < 160) {
      Brain.Screen.print("To the left!");
    }
    else {
      Brain.Screen.print("To the right!");
    }
  }
  wait(0.5, seconds); 
}

.origenY#

Devuelve la coordenada y de la esquina superior izquierda del cuadro delimitador del objeto detectado.

Acceso

SensorName.objects[index].originY

Valores de retorno

Returns an int representing the y-coordinate of the top-left corner of the object’s bounding box in pixels. The value ranges from 0 to 212.

Ejemplos

// Display if an object is close or far
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Vision1.takeSnapshot(Vision1__BLUEBOX);
  if (Vision1.objects[0].exists) {
    if (Vision1.objects[0].originY < 80) {
      Brain.Screen.print("Far");
    } else {
      Brain.Screen.print("Close");
    }
  }
  wait(0.5, seconds);
}

.ángulo#

Devuelve la orientación del código de color detectado.

Acceso

SensorName.objects[index].angle

Valores de retorno

Returns a double representing the orientation of the detected Color Code in degrees. The value ranges from 0 to 360.

Ejemplos

// Turn left or right depending on how a configured
// Color Code is rotated
while (true) {
  Vision1.takeSnapshot(Vision1__BOXCODE);
  if (Vision1.objects[0].exists) {
    double angle = Vision1.objects[0].angle;
    if (70 < angle && angle < 110) {
      Drivetrain.turnFor(right, 45, degrees);
    }
    else if (250 < angle && angle < 290) {
      Drivetrain.turnFor(left, 45, degrees);
    }
    else {
      Drivetrain.stop();
    }
  }
  wait(0.5, seconds);
}