Vision Sensing#
Introduction#
The Vision Sensor for VEX V5 detects and tracks colors and color codes. This allows the Vision Sensor to analyze its surroundings, follow objects, and react based on detected visual data. Below is a list of all available blocks:
take Vision Sensor snapshot – Captures data for a specific Color Signature or Color Code.
set Vision Sensor object item – Selects a specific object from the detected object list.
Vision Sensor object count – Returns the number of detected objects.
Vision Sensor object exists? – Returns whether an object is detected.
Vision Sensor object property – Returns details such as:
take Vision Sensor snapshot#
The take Vision Sensor snapshot block filters data from the Vision Sensor frame. The Vision Sensor can detect signatures that include configured colors and color codes.
Colors and color codes must be configured first in the Vision Utility before they can be used with this block.
The dataset stores objects ordered from largest to smallest by width, starting at item 1. Each object’s properties can be accessed using Vision object property block. An empty dataset is returned if no matching objects are detected.
take a [Vision1 v] snapshot of [SELECT_A_SIG v]
Parameter |
Description |
---|---|
vision |
The Vision Sensor to use, configured in the Devices window. |
signature |
The color signature or color code to analyze in the snapshot. |
Example
when started :: hat events
[Move forward if a red box is detected.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
drive [forward v] for [10] [mm v] ▶
set Vision Sensor object item#
The set Vision Sensor object item block sets which item in the dataset to use.
set [Vision1 v] object item to (1)
Parameter |
Description |
---|---|
vision |
The Vision Sensor to use, configured in the Devices window. |
item |
The number of the item in the dataset to use. |
Example
when started :: hat events
[Display the largest detected red object.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
clear all rows on [Brain v]
set cursor to row [1] column [1] on [Brain v]
if <[Vision1 v] object exists?> then
set [Vision1 v] object item to (1)
print ([Vision1 v] object [width v] :: #5cb0d6) on [Brain v] ▶
end
wait [0.5] seconds
Vision Sensor object count#
The Vision Sensor object count block returns the number of detected objects in the dataset as an integer.
([Vision1 v] object count)
Parameter |
Description |
---|---|
vision |
The Vision Sensor to use, configured in the Devices window. |
Example
when started :: hat events
[Display the amount of red objects.]
forever
clear all rows on [Brain v]
set cursor to row [1] column [1] on [Brain v]
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
print ([Vision1 v] object count) on [Brain v] ▶
end
wait [0.5] seconds
Vision Sensor object exists?#
The Vision Sensor object exists? block returns a Boolean indicating whether any object is detected in the dataset.
True – The dataset includes a detected object.
False – The dataset does not include any detected objects.
<[Vision1 v] object exists?>
Parameter |
Description |
---|---|
vision |
The Vision Sensor to use, configured in the Devices window. |
Example
when started :: hat events
[Move forward if a red box is detected.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
drive [forward v] for [10] [mm v] ▶
Vision Sensor object property#
There are five properties that are included with each object (shown below) stored after the take Vision Sensor snapshot block is used.
([Vision1 v] object [width v] :: #5cb0d6)
Some property values are based off of the detected object’s position in the Vision Sensor’s view at the time that the take Vision Sensor snapshot block was used. The Vision Sensor has a resolution of 316 by 212 pixels.
Parameter |
Description |
---|---|
vision |
The Vision Sensor to use, configured in the Devices window. |
property |
Which property of the detected object to use: |
width#
width returns the width of the detected object in pixels as an integer from 1 to 316.
([Vision1 v] object [width v] :: #5cb0d6)
Example
when started :: hat events
[Move towards a red object until its width is larger than 100 pixels.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
if <([Vision1 v] object [width v] :: #5cb0d6) < [100]> then
drive [forward v]
end
else
stop driving
height#
height returns the height of the detected object in pixels as an integer from 1 to 212.
([Vision1 v] object [height v] :: #5cb0d6)
Example
when started :: hat events
[Move towards a red object until its height is larger than 100 pixels.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
if <([Vision1 v] object [height v] :: #5cb0d6) < [100]> then
drive [forward v]
end
else
stop driving
centerX#
centerX returns the x-coordinate of the center of the detected object in pixels as an integer from 0 to 316.
([Vision1 v] object [centerX v] :: #5cb0d6)
Example
when started :: hat events
[Turn slowly until a red object is centered in front of the Vision Sensor.]
set turn velocity to [30] [% v]
turn [right v]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
if <<[140] < ([Vision1 v] object [centerX v] :: #5cb0d6)> and <([Vision1 v] object [centerX v] :: #5cb0d6) < [180]>> then
stop driving
centerY#
centerY returns the y-coordinate of the center of the detected object in pixels as an integer from 0 to 212.
([Vision1 v] object [centerY v] :: #5cb0d6)
Example
when started :: hat events
[Move towards a red object until its center y-coordinate is more than 140 pixels.]
forever
take a [Vision1 v] snapshot of [RED_BOX v]
if <[Vision1 v] object exists?> then
if <([Vision1 v] object [centerY v] :: #5cb0d6) < [140]> then
drive [forward v]
end
else
stop driving
angle#
angle returns the orientation of the detected Color Code as an integer in degrees from 0 to 180.
([Vision1 v] object [angle v] :: #5cb0d6)
Example
when started :: hat events
[Turn left or right depending on how the Color Code is rotated.]
forever
take a [Vision1 v] snapshot of [BOX_CODE v]
if <[Vision1 v] object exists?> then
if <<[70] < ([Vision1 v] object [angle v] :: #5cb0d6)> and <([Vision1 v] object [angle v] :: #5cb0d6) < [110]>> then
turn [right v] for (45) degrees ▶
else if <<[250] < ([Vision1 v] object [angle v] :: #5cb0d6)> and <([Vision1 v] object [angle v] :: #5cb0d6) < [290]>> then
turn [left v] for (45) degrees ▶
else
stop driving
end