visión#

El sensor de visión para VEX EXP detecta y rastrea firmas y códigos de color. Esto le permite analizar su entorno y reaccionar según los datos visuales detectados. A continuación, se muestra una lista de todos los métodos:

Métodos – Obtener datos del sensor de visión.

  • takeSnapshot – Captura datos para una firma de color o un código de color específico.

  • largestObject – Selecciona inmediatamente el objeto más grande de la instantánea.

  • objectCount – Devuelve la cantidad de objetos detectados como un entero.

  • objects – Devuelve una matriz que contiene las propiedades de los objetos detectados.

  • instalado – Si el sensor de visión está conectado al EXP Brain.

Propiedades: datos del objeto devueltos desde takeSnapshot.

  • .exists – Si el objeto existe en la detección actual como un valor booleano.

  • .width – Ancho del objeto detectado en píxeles.

  • .height – Altura del objeto detectado en píxeles.

  • .centerX – Posición X del centro del objeto en píxeles.

  • .centerY – Posición Y del centro del objeto en píxeles.

  • .angle – Orientación del código de color en grados.

  • .originX – Posición X de la esquina superior izquierda del objeto en píxeles.

  • .originY – Posición Y de la esquina superior izquierda del objeto en píxeles.

Constructores: inicializan y configuran manualmente el sensor de visión.

In VEXcode, the initialization of the Vision Sensor and its configured Color Signatures and Color Codes is done automatically. For the examples below, the configured Vision Sensor will be named Vision1. To manually initialize and construct a Vision Sensor and its Color Signatures and Color Codes, refer to the Constructors section on this page.

Métodos#

takeSnapshot#

takeSnapshot captures an image from the Vision Sensor, processes it based on the configured Color Signatures and Color Codes, and updates the objects array. This method can also limit the amount of objects captured in the snapshot.

Las Firmas de color y los Códigos de color deben configurarse primero en la Utilidad de visión antes de poder usarse con este método.

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. objects is an empty array if no matching objects are detected.

Default Usage:
Vision1.takeSnapshot(signature)

Parámetro

Descripción

signature

What signature or code object to get data of. Its name must be passed as a string in the format: the Vision Sensor’s name, followed by two underscores, and then the object’s name. For example: Vision1__REDBOX or Vision1__BOXCODE.

while (true) {
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // If objects were found, print the location.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.largestObject.centerX);
  } 
  else {
    Brain.Screen.print("no object");
  }

  wait(0.5, seconds);
}

Sobrecarga

  • Vision1.takeSnapshot(signature, count)

Parámetro de sobrecarga

Descripción

count

The number of objects to return as a uint32_t where the largest objects will be included.

Ejemplos de sobrecarga

// Display a location if a blue box is detected
while (true) {
  // Take a snapshot of only one object
  Vision1.takeSnapshot(Vision1__BLUEBOX, 1);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // If object was found, print the location.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.largestObject.centerX);
  } 
  else {
    Brain.Screen.print("no object");
  }

  wait(0.5, seconds);
}

Firmas de color#

Una firma de color es un color único que el sensor de visión puede reconocer. Estas firmas permiten al sensor detectar y rastrear objetos según su color. Una vez configurada una firma de color, el sensor puede identificar objetos con ese color específico en su campo de visión. Las firmas de color se utilizan con take_snapshot para procesar y detectar objetos de color en tiempo real.

To use a configured Color Signature in a project, its name must be passed as a string in the format: the Vision Sensor’s name, followed by two underscores, and then the Color Signature’s name. For example: vision_1__REDBOX.

//Display if any objects match the REDBOX signature
while (true) {
  // Take a snapshot to check for detected objects.
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearScreen();
  // Change to any configured Color Signature
  Vision1.takeSnapshot(Vision1__REDBOX);
  if (Vision1.objects[0].exists){
    Brain.Screen.print("Color signature");
    Brain.Screen.newLine();
    Brain.Screen.print("detected!");
    wait(0.1,seconds);
  }
}

Códigos de color#

Un código de color es un patrón estructurado compuesto por firmas de color dispuestas en un orden específico. Estos códigos permiten al sensor de visión reconocer patrones de color predefinidos. Los códigos de color son útiles para identificar objetos complejos o crear marcadores únicos para la navegación autónoma.

To use a configured Color Code in a project, its name must be passed as a string in the format: the Vision Sensor’s name, followed by two underscores, and then the Color Code’s name. For example: vision_1__BOXCODE.

// Display if any objects match the BOXCODE code
while (true) {
  // Take a snapshot to check for detected objects.
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearLine(1);
  // Change to any configured Color Code
  Vision1.takeSnapshot(Vision1__BOXCODE);
  if (Vision1.objects[0].exists){
    Brain.Screen.print("Color Code Detected!");
    wait(0.1, seconds);
  }
}

largestObject#

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.

Default Usage:
Vision1.largestObject

while (true){
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__BLUEBOX);
  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  // If objects were found, print the location 
  // of largest.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.largestObject.centerX);
  } 
  else {
    Brain.Screen.print("no object");
  }
  wait(0.5, seconds);
}

objectCount#

objectCount returns the number of items inside the objects array as an integer.

Default Usage:
Vision1.objectCount

while (true) {
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // Print how many objects were detected.
  Brain.Screen.print("object count: %d", Vision1.objectCount);

  wait(0.5, seconds);
}

installed#

installed returns an integer indicating whether the Vision Sensor is currently connected to the EXP Brain.

  • 1 – The Vision Sensor is connected to the EXP Brain.

  • 0 – The Vision Sensor is not connected to the EXP Brain.

Parámetros

Descripción

Este método no tiene parámetros.

// Display a message if the Vision Sensor is detected
if (Vision1.installed()){
  Brain.Screen.print("Vision Sensor");
  Brain.Screen.newLine();
  Brain.Screen.print("Installed!");
}

objects#

objects returns an array of detected object properties. Use the array to access specific property values of individual objects.

Default Usage:
Vision1.objects

Propiedades#

There are eight properties that are included with each object stored in the objects array after takeSnapshot is used.

Some property values are based off of the detected object’s position in the Vision Sensor’s view at the time that takeSnapshot was used. The Vision Sensor has a resolution of 316 by 212 pixels.

.exists#

.exists returns an integer indicating if the index exists in the objects array or not.

  • 1: The index exists.

  • 0: The index does not exist.

// Check if at least one object is detected
while (true) {
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // If an object exists, print its location.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.objects[0].centerX);
  } 
  else {
    Brain.Screen.print("No objects detected.");
  }

  wait(0.5, seconds);
}

.width#

.width returns the width of the detected object in pixels, which is an integer between 1 and 316.

// Move towards a blue box until its width is
// larger than 100 pixels
while (true){
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].width < 100) {
    Drivetrain.driveFor(forward, 10, mm);
  } 
  else {
    Drivetrain.stop();
  }

  wait(0.5, seconds);
}

.height#

.height returns the height of the detected object in pixels, which is an integer between 1 and 212.

// Move towards a blue box until its height is
// larger than 100 pixels
while (true){
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].height < 100) {
    Drivetrain.driveFor(forward, 10, mm);
  } 
  else {
    Drivetrain.stop();
  }

  wait(0.5, seconds);
}

.centerX#

.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 316.

// Turn slowly until a blue box is centered in
// front of the robot
Drivetrain.setTurnVelocity(10,percent);
Drivetrain.turn(right);

while (true){
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists){
    if (140 < Vision1.largestObject.centerX && Vision1.largestObject.centerX < 180){
      Drivetrain.stop();
    }
  }
  wait(0.5,seconds);
}

.centerY#

.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 212.

// Move towards a blue object until its
// center y-coordinate is more than 140 pixels
while (true){
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  if (Vision1.objects[0].exists){
    if (Vision1.largestObject.centerY < 140){
      Drivetrain.drive(forward);
    }
  }
  else{
    Drivetrain.stop();
  }
  wait(0.5,seconds);
}

.angle#

.angle returns the orientation of the detected object in degrees, which is a double between 0 and 316.

// Turn right or left depending on how the
// configured box code is rotated.
while (true){
  Vision1.takeSnapshot(Vision1__BOXCODE);

  if (Vision1.objects[0].exists){
    if (70 < Vision1.objects[0].angle && Vision1.objects[0].angle < 110){
      Drivetrain.turnFor(right, 45, degrees);
    }
    else if (250 < Vision1.objects[0].angle && Vision1.objects[0].angle < 290){
      Drivetrain.turnFor(left, 45, degrees);
    }
    else{
      Drivetrain.stop();
    }
  }
  wait(0.5,seconds);
}

.originX#

.originX returns the x-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 316.

// Display if a red box is to the
// left or the right
while (true){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
    
  Vision1.takeSnapshot(Vision1__REDBOX);
    
  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);
}

.originY#

.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 212.

// Display if a red box is close or far
// from the robot.
while (true){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
    
  Vision1.takeSnapshot(Vision1__REDBOX);

  if (Vision1.objects[0].exists){
    if (Vision1.objects[0].originY < 80){
      Brain.Screen.print("Far");
    }
    else{
      Brain.Screen.print("Close");
    }
  }
  wait(0.5,seconds);
}

Constructores#

Constructors are used to manually create vision, signature, and code objects, which are necessary for configuring the Vision Sensor outside of VEXcode. If fewer arguments are provided, default arguments or function overloading should be used in the constructor definition.

For the examples below, the configured Vision Sensor will be named Vision1, and the configured Color Signature objects, such as Vision1__BLUEBOX, will be used in all subsequent examples throughout this API documentation when referring to vision class methods.

Vision Sensor#

vision creates a Vision Sensor and configures the brightness level and signatures to be used with the sensor.

Default Usage: vision( int32_t index, uint8_t bright, Args &… sigs )

Parámetros predeterminados

Descripción

port

Un Puerto inteligente válido al que está conectado el sensor de visión.

brightness

El valor de brillo del sensor de visión, de 10 a 150.

sigs

El nombre de uno o más objetos Firma de color o Código de color creados previamente.

// Construct a vision object Vision1 with 1 color
// Vision1__REDBOX.
vision::signature Vision1__REDBOX = vision::signature (1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1);
vision Vision1 = vision (PORT1, 50, Vision1__REDBOX);

while (true) {
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__REDBOX);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // If objects were found, print the location.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.largestObject.centerX);
  } else {
    Brain.Screen.print("no object");
  }

  wait(0.5, seconds);
}

Color Signature#

signature creates a Color Signature. Up to seven different Color Signatures can be stored on a Vision Sensor at once.

Uso predeterminado:

signature(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)

Parámetro

Descripción

index

The signature object’s index, from 1 - 7. Note: Creating two signature objects with the same index number will cause the second created object to override the first.

uMin

The value from uMin in the Vision Utility.

uMax

The value from uMax in the Vision Utility.

uMean

The value from uMean in the Vision Utility.

vMin

The value from vMin in the Vision Utility.

vMax

The value from vMax in the Vision Utility.

vMean

The value from vMean in the Vision Utility.

rgb

The value from rgb in the Vision Utility.

type

The value from type in the Vision Utility.

Para obtener los valores necesarios para crear una Firma de Color, acceda a la Utilidad de Visión. Una vez configurada la Firma de Color, copie los valores de los parámetros desde la ventana de Configuración.

// Construct a vision object Vision1 with two Color
// Signatures, Vision1__REDBOX and Vision1__BLUEBOX.
vision::signature Vision1__REDBOX = vision::signature (1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1);
vision::signature Vision1__BLUEBOX = vision::signature (2, -4479, -3277, -3878,5869, 7509, 6689,2.5, 1);
vision Vision1 = vision (PORT1, 50, Vision1__REDBOX, Vision1__BLUEBOX);

while (true) {
  // Take a snapshot to check for detected objects.
  Vision1.takeSnapshot(Vision1__BLUEBOX);

  // Clear the screen/reset so that we can display
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // If objects were found, print the location.
  if (Vision1.objects[0].exists) {
    Brain.Screen.print("Center X: %d", Vision1.largestObject.centerX);
  } else {
    Brain.Screen.print("no object");
  }

  wait(0.5, seconds);
}

Color Code#

Code creates a Color Code. It requires at least two already defined Color Signatures in order to be used. Up to eight different Color Codes can be stored on a Vision Sensor at once.

Uso predeterminado:

code(sig1, sig2)

Parámetros

Descripción

sig1

A previously created signature object, or the int32_t index of a previously created signature object.

sig2

A previously created signature object, or the int32_t index of a previously created signature object.

// Construct a vision object Vision1 with two Color
// Signatures, Vision1__REDBOX and Vision1__BLUEBOX,
// Alongside a Color Code for a red box to the left of
// a blue box, Vision1__BOXCODE.
vision::signature Vision1__REDBOX = vision::signature (1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1);
vision::signature Vision1__BLUEBOX = vision::signature (2, -4479, -3277, -3878,5869, 7509, 6689,2.5, 1);
vision::code Vision1__BOXCODE = vision::code (Vision1__REDBOX, Vision1__BLUEBOX);
vision Vision1 = vision (PORT1, 50, Vision1__REDBOX, Vision1__BLUEBOX);

// Turn right or left depending on how the
// configured box code is rotated.
while (true){
  Vision1.takeSnapshot(Vision1__BOXCODE);

  if (Vision1.objects[0].exists){
    if (70 < Vision1.objects[0].angle && Vision1.objects[0].angle < 110){
      Drivetrain.turnFor(right, 45, degrees);
    }
    else if (250 < Vision1.objects[0].angle && Vision1.objects[0].angle < 290){
      Drivetrain.turnFor(left, 45, degrees);
    }
    else{
      Drivetrain.stop();
    }
  }
  wait(0.5,seconds);
}

Sobrecargas

  • code(sig1, sig2, sig3)

  • code(sig1, sig2, sig3, sig4)

  • code(sig1, sig2, sig3, sig4, sig5)

Parámetros de sobrecarga

Descripción

sig3

A previously created signature object, or the int32_t index of a previously created signature object.

sig4

A previously created signature object, or the int32_t index of a previously created signature object.

sig5

A previously created signature object, or the int32_t index of a previously created signature object.

// Construct a vision object Vision1 with two Color
// Signatures, Vision1__REDBOX and Vision1__BLUEBOX,
// Alongside a Color Code for a red box to the left of
// a blue box alternating for 5 boxes, Vision1__BOXCODE.
vision::signature Vision1__REDBOX = vision::signature (1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1);
vision::signature Vision1__BLUEBOX = vision::signature (2, -4479, -3277, -3878,5869, 7509, 6689,2.5, 1);
vision::code Vision1__BOXCODE = vision::code (Vision1__REDBOX, Vision1__BLUEBOX, Vision1__REDBOX, Vision1__BLUEBOX, Vision1__REDBOX);
vision Vision1 = vision (PORT1, 50, Vision1__REDBOX, Vision1__BLUEBOX);

// Turn right or left depending on how the
// configured box code is rotated.
while (true){
  Vision1.takeSnapshot(Vision1__BOXCODE);

  if (Vision1.objects[0].exists){
    if (70 < Vision1.objects[0].angle && Vision1.objects[0].angle < 110){
      Drivetrain.turnFor(right, 45, degrees);
    }
    else if (250 < Vision1.objects[0].angle && Vision1.objects[0].angle < 290){
      Drivetrain.turnFor(left, 45, degrees);
    }
    else{
      Drivetrain.stop();
    }
  }
  wait(0.5,seconds);
}