视觉传感器#

介绍#

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.

视觉传感器可以同时检测多个特征物体,并提供物体位置、宽度、高度和中心坐标等信息。

类构造函数#

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

类析构函数#

Destroys the vision object and releases associated resources.

~vision();

参数#

范围

类型

描述

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

视觉传感器的亮度级别。

sigs

vision::signature or vision::code objects

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

示例#

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

成员功能#

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 拍摄快照并搜索与指定签名 ID 匹配的对象。

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 拍摄快照,并仅存储与签名 ID 匹配的最大指定数量的对象。

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

范围

类型

描述

id

uint32_t

要查找的签名ID号码。

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

要检测的最大对象数量。

Return Values

Returns an int32_t representing the number of matching objects detected.

Notes
  • 视觉传感器必须先拍摄快照,才能访问物体数据。

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

示例

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#

返回视觉传感器是否已连接到 V5 大脑。

Available Functions
bool installed();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示视觉传感器是否已连接:

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

特性#

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.

视觉传感器的图像分辨率为 316×212 像素。物体位置和大小属性以像素为单位,相对于传感器的当前视野进行报告。

以下房源可供选择:

  • 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

成分

描述

VisionSensor

您的视觉传感器实例的名称。

示例

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

成分

描述

VisionSensor

您的视觉传感器实例的名称。

示例

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

成分

描述

VisionSensor

您的视觉传感器实例的名称。

index

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

property

可用的对象属性

.存在#

指示指定的对象索引是否包含有效的检测到的对象。

访问

SensorName.objects[index].exists

返回值

返回一个布尔值,指示指定的对象索引是否包含有效的检测到的对象:

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

示例

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

。宽度#

返回检测到的对象的宽度。

访问

SensorName.objects[index].width

返回值

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

示例

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

。高度#

返回检测到的物体的高度。

访问

SensorName.objects[index].height

返回值

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

示例

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

返回检测到的物体中心的 x 坐标。

访问

SensorName.objects[index].centerX

返回值

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

示例

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

.centerY#

返回检测到的物体中心的 y 坐标。

访问

SensorName.objects[index].centerY

返回值

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

示例

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

返回检测到的对象边界框左上角的 x 坐标。

访问

SensorName.objects[index].originX

返回值

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.

示例

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

.originY#

返回检测到的对象边界框左上角的 y 坐标。

访问

SensorName.objects[index].originY

返回值

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.

示例

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

。角度#

返回检测到的颜色代码的方向。

访问

SensorName.objects[index].angle

返回值

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

示例

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