Vision#
The Vision and AI Vision Sensors enable robots to detect and track visual information in their environment. By identifying objects, colors, and patterns, these sensors allow robots to analyze their surroundings and respond to what they see.
Below is a list of all methods:
AI Vision Sensor
Getters
takeSnapshot – 捕获特定颜色特征或颜色代码的数据。
objectCount – 以整数返回检测到的物体的数量。
largestObject – 立即从快照中选择最大的对象。
Properties – Object data returned from takeSnapshot.
.exists – 以布尔值表示该对象是否存在于当前检测中。
.width – 检测到的物体的宽度(以像素为单位)。
.height – 检测到的物体的高度(以像素为单位)。
.centerX – 对象中心的 X 位置(以像素为单位)。
.centerY – 对象中心的 Y 位置(以像素为单位)。
.angle – 颜色代码的方向(以度为单位)。
.originX – 对象左上角的 X 位置(以像素为单位)。
.originY – 对象左上角的 Y 位置(以像素为单位)。
.id – Classification or tag ID of the object.
.score – Confidence score for AI Classifications (1–100).
Vision Sensor
Getters
takeSnapshot – Captures data for a specific Color Signature or Color Code.
largestObject – Immediately select the largest object from the snapshot.
objectCount – Returns the number of detected objects as an integer.
objects – Returns an array containing the properties of detected objects.
installed – Whether the Vision Sensor is connected to the IQ (2nd Gen) Brain.
属性 – 从 takeSnapshot 返回的对象数据。
.exists – Whether the object exists in the current detection as a Boolean.
.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.
.angle – Orientation of the Color Code in degrees.
.originX – X position of the object’s top-left corner in pixels.
.originY – Y position of the object’s top-left corner in pixels.
Constructors – Manually initialize and configure the sensors.
aivision – Creates an AI Vision Sensor.
vision – Creates a Vision Sensor.
vision::signature – 创建颜色签名。
vision::code – 创建颜色代码。
AI Vision Sensor#
拍摄快照#
takeSnapshot
captures an image from the AI 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.
必须先在 Vision Utility 中配置 颜色签名 和 颜色代码,然后才能使用此方法。
objects
数组存储按宽度从大到小排序的对象,从索引 0 开始。可以使用其索引访问每个对象的 属性。如果没有检测到匹配的对象,则 objects
为空数组。
Default Usage:
AIVision1.takeSnapshot(signature)
Overloads:
AIVision1.takeSnapshot(signature, count)
参数 |
描述 |
---|---|
|
What signature to get data of.
|
|
Optional. Sets the maximum number of objects that can be returned from 1 to 24 (default: 8). |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Move forward if an object is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Drivetrain.driveFor(forward, 50, mm);
}
wait(50, msec);
}
}
色彩签名#
A Color Signature is a unique color that the AI Vision Sensor can recognize. These signatures allow the AI Vision Sensor to detect and track objects based on their color. Once a Color Signature is configured, the sensor can identify objects with that specific color in its field of view. Color signatures are used with take_snapshot to process and detect colored objects in real-time.
In order to use a configured Color Signature in a project, its name must be the name of the sensor, two underscores, and then the Color Signature’s name. For example: AIVision1__redBox
.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
// Change to any configured Color Signature
AIVision1.takeSnapshot(AIVision1__redBox);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("Detected!");
}
wait(50, msec);
}
}
颜色代码#
颜色代码是由按特定顺序排列的颜色特征组成的结构化模式。这些代码使视觉传感器能够识别预定义的颜色模式。颜色代码可用于识别复杂物体或为自主导航创建独特的标记。
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: AIVision1__redBlue
.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
// Change to any configured Color Code
AIVision1.takeSnapshot(AIVision1__redBlue);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("Detected!");
}
wait(50, msec);
}
}
最大对象#
“largestObject”从“objects”数组中检索检测到的最大对象。
此方法可用于始终从“对象”中获取最大的对象,而无需指定索引。
Default Usage:
AIVision1.largestObject
参数 |
描述 |
---|---|
该方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display the closest AprilTag's ID
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
AIVision1.takeSnapshot(aivision::ALL_TAGS);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("%d", AIVision1.largestObject.id);
}
wait(50, msec);
}
}
对象数量#
objectCount
以整数形式返回 objects
数组内的项目数。
Default Usage:
AIVision1.objectCount
参数 |
描述 |
---|---|
该方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display the number of detected objects
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("%d", AIVision1.objectCount);
}
wait(50, msec);
}
}
已安装#
installed
returns an integer indicating whether the AI Vision Sensor is currently connected to the IQ (2nd Gen) Brain.
1
– The AI Vision Sensor is connected to the IQ (2nd Gen) Brain.0
– The AI Vision Sensor is not connected to the IQ (2nd Gen) Brain.
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display a message if the Vision Sensor is detected
if (AIVision1.installed()){
Brain.Screen.print("Installed!");
}
对象#
objects
返回检测到的对象属性的数组。使用该数组可以访问各个对象的特定 property 值。
Default Usage:
AIVision1.objects
特性#
There are ten 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 AI Vision Sensor has a resolution of 320 by 240 pixels.
.exists#
.exists
返回一个整数,指示索引是否存在于 objects
数组中。
1
:索引存在。0
:索引不存在。
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Move forward if an object is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Drivetrain.driveFor(forward, 50, mm);
}
wait(50, msec);
}
}
。宽度#
.width
returns the width of the detected object in pixels, which is an integer between 1 and 320.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Approach an object until it's at least 100 pixels wide
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].width < 100) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
}
。高度#
.height
returns the height of the detected object in pixels, which is an integer between 1 and 240.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Approach an object until it's at least 90 pixels tall
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].height < 90) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
}
.centerX#
.centerX
returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Turn until an object is directly in front of the sensor
Drivetrain.setTurnVelocity(10, percent);
Drivetrain.turn(right);
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].centerX > 140 && AIVision1.objects[0].centerX < 180) {
Drivetrain.stop();
}
}
wait(10, msec);
}
}
.centerY#
.centerY
returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Approach an object until it's at least 90 pixels tall
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].centerY < 150) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
}
。角度#
.angle
returns the orientation of the detected Color Code or AprilTag in degrees, which is a double between 0 and 360.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Turn left or right depending on how a configured
// Color Code is rotated
while (true) {
AIVision1.takeSnapshot(AIVision1__redBlue);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].angle > 50 && AIVision1.objects[0].angle < 100) {
Drivetrain.turn(right);
}
else if (AIVision1.objects[0].angle > 270 && AIVision1.objects[0].angle < 330) {
Drivetrain.turn(left);
}
else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
}
.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 320.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display if an object is to the left or the right
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].originX < 120) {
Brain.Screen.print("To the left!");
} else {
Brain.Screen.print("To the right!");
}
} else {
Brain.Screen.print("No objects");
}
wait(100, msec);
}
}
.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 240.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display if an object is close or far
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Brain.Screen.print(AIVision1.objects[0].originY);
Brain.Screen.newLine();
if (AIVision1.objects[0].originY < 110) {
Brain.Screen.print("Close");
} else {
Brain.Screen.print("Far");
}
}
wait(100, msec);
}
}
.id#
.id
returns the ID of the detected AI Classification or AprilTag as an integer.
AI Classification |
ID |
---|---|
Blue Ball |
0 |
Green Ball |
1 |
Red Ball |
2 |
Blue Ring |
3 |
Green Ring |
4 |
Red Ring |
5 |
Blue Cube |
6 |
Green Cube |
7 |
Red Cube |
8 |
For an AprilTag, the .id property represents the detected AprilTag’s ID number in the range of 0 to 36. For an AI Classification, the id corresponds to the predefined id as shown below.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Move forward when AprilTag 1 is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_APRILTAGS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].id == 1) {
Drivetrain.drive(forward);
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
}
.score#
.score
returns the confidence score of the detected AI Classification as an integer between 1 and 100.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display if a score is confident
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].score > 95) {
Brain.Screen.print("Confident");
} else {
Brain.Screen.print("Not confident");
}
}
wait(50, msec);
}
}
视觉传感器#
拍摄快照#
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.
必须先在 Vision Utility 中配置 颜色签名 和 颜色代码,然后才能使用此方法。
objects
数组存储按宽度从大到小排序的对象,从索引 0 开始。可以使用其索引访问每个对象的 属性。如果没有检测到匹配的对象,则 objects
为空数组。
默认用法:
Vision1.takeSnapshot(signature)
范围 |
描述 |
---|---|
|
What signature to get data of.
|
|
Optional. Sets the maximum number of objects that can be returned from 1 to 24 (default: 8). |
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);
}
超载
Vision1.takeSnapshot(签名,计数)
过载参数 |
描述 |
---|---|
|
以“uint32_t”形式返回的对象数量,其中包括最大的对象。 |
重载示例
// 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);
}
色彩签名#
颜色特征是视觉传感器能够识别的独特颜色。这些特征使传感器能够根据物体的颜色检测和跟踪它们。配置颜色特征后,传感器可以识别其视野范围内具有该特定颜色的物体。颜色特征与 take_snapshot 一起使用,可以实时处理和检测彩色物体。
要在项目中使用已配置的颜色签名,其名称必须以字符串形式传递,格式如下:视觉传感器的名称,后跟两个下划线,然后是颜色签名的名称。例如:“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.clearLine(1);
// Change to any configured Color Signature
Vision1.takeSnapshot(Vision1__REDBOX);
if (Vision1.objects[0].exists){
Brain.Screen.print("Color signature detected!");
}
}
颜色代码#
颜色代码是由按特定顺序排列的颜色特征组成的结构化模式。这些代码使视觉传感器能够识别预定义的颜色模式。颜色代码可用于识别复杂物体或为自主导航创建独特的标记。
要在项目中使用已配置的颜色代码,其名称必须以字符串形式传递,格式如下:视觉传感器名称,后跟两个下划线,然后是颜色代码名称。例如:“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”从“objects”数组中检索检测到的最大对象。
此方法可用于始终从“对象”中获取最大的对象,而无需指定索引。
默认用法:
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
以整数形式返回 objects
数组内的项目数。
默认用法:
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
返回一个整数,指示视觉传感器当前是否连接到 IQ(第二代)大脑。
1
– 视觉传感器连接到 IQ(第二代)大脑。0
– 视觉传感器未连接到 IQ(第二代)大脑。
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display a message if the Vision Sensor is detected
if (Vision1.installed()){
Brain.Screen.print("Vision Sensor Installed!");
}
对象#
objects
返回检测到的对象属性的数组。使用该数组可以访问各个对象的特定 property 值。
默认用法:
Vision1.objects
特性#
There are eight properties that are included with each object stored in the objects
array after takeSnapshot
is used.
某些属性值基于使用“takeSnapshot”时检测到的物体在视觉传感器视图中的位置。视觉传感器的分辨率为 316 x 212 像素。
.exists#
.exists
返回一个整数,指示索引是否存在于 objects
数组中。
1
:索引存在。0
:索引不存在。
// 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
返回检测到的对象的宽度(以像素为单位),它是 1 到 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
返回检测到的物体的高度(以像素为单位),它是 1 到 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
返回检测到的物体中心的 x 坐标(以像素为单位),它是 0 到 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
返回检测到的物体中心的 y 坐标(以像素为单位),它是 0 到 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
以度为单位返回检测到的物体的方向,它是 0 到 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
返回检测到的对象边界框左上角的 x 坐标(以像素为单位),它是 0 到 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
返回检测到的对象边界框左上角的 y 坐标(以像素为单位),它是 0 到 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);
}
构造函数#
Constructors are used to manually create aivision
, vision
, signature
, and code
objects, which are necessary for configuring the sensors outside of VEXcode. If fewer arguments are provided, default arguments or function overloading should be used in the constructor definition.
AI Vision Sensor#
aivision
creates an AI Vision Sensor.
Default Usage:
aivision( int32_t index, uint8_t bright, Args &... sigs )
参数 |
描述 |
---|---|
|
Which Smart Port the AI Vision Sensor is connected to, from 1 to 12. |
|
Optional. The name of one or more Color Signature or Color Code objects. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Move forward if an object is detected
vex::aivision AIVision1(PORT12, aivision::ALL_AIOBJS);
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Drivetrain.driveFor(forward, 50, mm);
}
wait(50, msec);
}
}
视觉传感器#
vision
creates a Vision Sensor.
默认用法:vision(int32_t index,uint8_t bright,Args&...sigs)
参数 |
描述 |
---|---|
|
视觉传感器连接到的有效 智能端口。 |
|
视觉传感器的亮度值,从 10 到 150。 |
|
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
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);
}
}
色彩签名#
signature
创建一个颜色签名。视觉传感器上最多可同时存储七种不同的颜色签名。
Default Usage:
signature(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)
范围 |
描述 |
---|---|
|
|
|
Vision Utility 中“uMin”的值。 |
|
Vision Utility 中“uMax”的值。 |
|
Vision Utility 中“uMean”的值。 |
|
Vision Utility 中“vMin”的值。 |
|
Vision Utility 中“vMax”的值。 |
|
Vision Utility 中“vMean”的值。 |
|
Vision Utility 中“rgb”的值。 |
|
Vision Utility 中“type”的值。 |
要获取创建颜色签名的值,请转到 Vision Utility。配置颜色签名后,从配置窗口复制参数值。
// 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);
}
颜色代码#
“代码”用于创建颜色代码。它需要至少两个已定义的颜色签名才能使用。一个视觉传感器最多可同时存储八个不同的颜色代码。
Default Usage:
code(sig1, sig2)
Overloads:
代码(sig1,sig2,sig3)
代码(sig1,sig2,sig3,sig4)
代码(sig1、sig2、sig3、sig4、sig5)
参数 |
描述 |
---|---|
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
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) {
int angle = Vision1.objects[0].angle;
if (angle > 70 && angle < 110) {
Drivetrain.turnFor(right, 45, degrees);
}
else if (angle > 250 && angle < 290) {
Drivetrain.turnFor(left, 45, degrees);
}
else {
Drivetrain.stop();
}
}
wait(0.5, seconds);
}
}