Vision Sensor#
Introduction#
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.
The Vision Sensor can detect multiple signature objects simultaneously and provides information such as object position, width, height, and center coordinates.
Class Constructors#
vision(
int32_t index,
uint8_t bright,
Args&... sigs );
Class Destructor#
Destroys the vision object and releases associated resources.
~vision();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The Smart Port that the Vision Sensor is connected to, written as |
|
|
The brightness level of the Vision Sensor. |
|
|
One or more |
Examples#
// 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
Member Functions#
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.
1 — Takes a snapshot and searches for objects matching the specified signature ID.
int32_t takeSnapshot( uint32_t id );
2 — Takes a snapshot and searches for objects matching the specified
vision::codeobject.int32_t takeSnapshot( code &cc );
3 — Takes a snapshot and searches for objects matching the specified
vision::signatureobject.int32_t takeSnapshot( signature &sig );
4 — Takes a snapshot and stores only the largest specified number of objects matching the signature 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::codeobject.int32_t takeSnapshot( code &cc, uint32_t count );
Parameters6 — Takes a snapshot and stores only the largest specified number of objects matching the specified
vision::signatureobject.int32_t takeSnapshot( signature &sig, uint32_t count );
Parameter |
Type |
Description |
|---|---|---|
|
|
The signature ID number to search for. |
|
|
A reference to a |
|
|
A reference to a |
|
|
The maximum number of objects to detect. |
Returns an int32_t representing the number of matching objects detected.
The Vision Sensor must take a snapshot before object data can be accessed.
When
countis specified, only the largest detected objects (up to the specified amount) are stored.
Examples
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#
Returns whether the Vision Sensor is connected to the V5 Brain.
Available Functionsbool installed();
This function does not take any parameters.
Return ValuesReturns a Boolean indicating whether the Vision Sensor is connected:
-
true— The Vision Sensor is connected. -
false— The Vision Sensor is not connected.
Properties#
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.
The Vision Sensor image resolution is 316 by 212 pixels. Object position and size properties are reported in pixel units relative to the sensor’s current view.
The following properties are available:
largestObject— Selects the largest detected object from the most recent snapshot.objectCount— Returns the number of valid objects stored inobjects.objects— Array of detected objects updated bytakeSnapshot..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.
VisionSensor.largestObject
Component |
Description |
|---|---|
|
The name of your Vision Sensor instance. |
Examples
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.
VisionSensor.objectCount
Component |
Description |
|---|---|
|
The name of your Vision Sensor instance. |
Examples
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.
VisionSensor.objects[index].property
Component |
Description |
|---|---|
|
The name of your Vision Sensor instance. |
|
The object index in the array. Index begins at |
|
One of the available object properties. |
.exists#
Indicates whether the specified object index contains a valid detected object.
Access
SensorName.objects[index].exists
Return Values
Returns a Boolean indicating whether the specified object index contains a valid detected object:
-
true— A valid object exists at the specified index. -
false— No object exists at the specified index.
Examples
// 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);
}
.width#
Returns the width of the detected object.
Access
SensorName.objects[index].width
Return Values
Returns an int representing the width of the detected object in pixels. The value ranges from 1 to 316.
Examples
// 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);
}
.height#
Returns the height of the detected object.
Access
SensorName.objects[index].height
Return Values
Returns an int representing the height of the detected object in pixels. The value ranges from 1 to 212.
Examples
// 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#
Returns the x-coordinate of the detected object’s center.
Access
SensorName.objects[index].centerX
Return Values
Returns an int representing the x-coordinate of the object’s center in pixels. The value ranges from 0 to 316.
Examples
// 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#
Returns the y-coordinate of the detected object’s center.
Access
SensorName.objects[index].centerY
Return Values
Returns an int representing the y-coordinate of the object’s center in pixels. The value ranges from 0 to 212.
Examples
// 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#
Returns the x-coordinate of the top-left corner of the detected object’s bounding box.
Access
SensorName.objects[index].originX
Return Values
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.
Examples
// 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#
Returns the y-coordinate of the top-left corner of the detected object’s bounding box.
Access
SensorName.objects[index].originY
Return Values
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.
Examples
// 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);
}
.angle#
Returns the orientation of the detected Color Code.
Access
SensorName.objects[index].angle
Return Values
Returns a double representing the orientation of the detected Color Code in degrees. The value ranges from 0 to 360.
Examples
// 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);
}