视觉传感器#
介绍#
vision 类用于控制和访问来自 V5 视觉传感器的数据。该传感器可检测并跟踪其视野内的颜色特征,使您的机器人能够识别目标并实时响应视觉信息。
视觉传感器可以同时检测多个特征物体,并提供物体位置、宽度、高度和中心坐标等信息。
类构造函数#
vision(
int32_t index,
uint8_t bright,
Args&... sigs );
类析构函数#
摧毁 vision 对象并释放相关资源。
~vision();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
视觉传感器连接的智能端口,记为 |
|
|
视觉传感器的亮度级别。 |
|
|
要向传感器注册的一个或多个 |
示例#
// 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
成员功能#
vision 类包含以下成员函数:
takeSnapshot— 捕获特定颜色签名或颜色代码的数据。installed— 视觉传感器是否连接到 V5 大脑。
调用 takeSnapshot 后,要访问检测到的对象数据,请使用可用的 Properties。
在调用任何 vision 成员函数之前,必须先创建一个 vision 实例,如下所示:
/* 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#
从视觉传感器捕获图像,根据特征码处理图像,并更新对象数量为 objects 数组。此方法还可以限制快照中捕获的对象数量。
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 — 拍摄快照并搜索与指定签名 ID 匹配的对象。
int32_t takeSnapshot( uint32_t id );
2 — 拍摄快照并搜索与指定的
vision::code对象匹配的对象。int32_t takeSnapshot( code &cc );
3 — 拍摄快照并搜索与指定的
vision::signature对象匹配的对象。int32_t takeSnapshot( signature &sig );
4 — 拍摄快照,并仅存储与签名 ID 匹配的最大指定数量的对象。
int32_t takeSnapshot( uint32_t id, uint32_t count );
5 — 拍摄快照,并仅存储与指定的
vision::code对象匹配的最大指定数量的对象。int32_t takeSnapshot( code &cc, uint32_t count );
Parameters6 — 拍摄快照,并仅存储与指定的
vision::signature对象匹配的最大指定数量的对象。int32_t takeSnapshot( signature &sig, uint32_t count );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要查找的签名ID号码。 |
|
|
引用要搜索的 vision::code 为 |
|
|
引用要搜索的 |
|
|
要检测的最大对象数量。 |
返回 int32_t ,表示检测到的匹配对象的数量。
视觉传感器必须先拍摄快照,才能访问物体数据。
当指定
count,仅存储检测到的最大对象(最多指定数量)。
示例
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 Functionsbool installed();
此函数不接受任何参数。
Return Values返回一个布尔值,指示视觉传感器是否已连接:
-
true— The Vision Sensor is connected. -
false— The Vision Sensor is not connected.
特性#
调用 takeSnapshot 会更新视觉传感器的检测结果。每次快照都会刷新包含所请求的颜色签名或颜色代码的检测到的对象数组 objects。
Vision Sensor data is accessed through properties of objects stored in Vision1.objects[index], where index begins at 0.
视觉传感器的图像分辨率为 316×212 像素。物体位置和大小属性以像素为单位,相对于传感器的当前视野进行报告。
以下房源可供选择:
largestObject— 从最近的快照中选择检测到的最大对象。objectCount— 返回存储在objects。objects— 由 takeSnapshottakeSnapshot检测到的对象数组。.exists— 对象条目是否包含有效数据。.width— 检测到的对象宽度(以像素为单位)。.height— 检测到的物体的高度(以像素为单位)。.centerX— 对象中心的 X 坐标(以像素为单位)。.centerY— 对象中心的 Y 坐标(以像素为单位)。.originX— 对象左上角的 X 坐标(以像素为单位)。.originY— 对象左上角的 Y 坐标(以像素为单位)。.angle— 检测到的颜色代码的方向(以度为单位)(如果适用)。
largestObject#
从 objects 对象的数组中检索检测到的最大对象。
此方法可用于始终从 objects 而无需指定索引。
VisionSensor.largestObject
成分 |
描述 |
|---|---|
|
您的视觉传感器实例的名称。 |
示例
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#
返回包含 objects 元素的数组中的元素数量,以整数形式返回。
VisionSensor.objectCount
成分 |
描述 |
|---|---|
|
您的视觉传感器实例的名称。 |
示例
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 是一个数组,其中包含最近一次调用 takeSnapshot 检测到的对象。
数组中的每个元素代表一个检测到的对象,并提供对其属性的访问,例如 width、height、centerX 等。
对象按宽度从大到小排序,从索引 0开始。
VisionSensor.objects[index].property
成分 |
描述 |
|---|---|
|
您的视觉传感器实例的名称。 |
|
数组中的对象索引。索引从 |
|
可用的对象属性。 |
.存在#
.exists 返回指定的对象索引是否包含有效的检测到的对象。
访问
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);
}
。宽度#
.width 返回检测到的对象的宽度。
访问
SensorName.objects[index].width
返回值
返回一个 int 之间的整数,表示检测到的对象以像素为单位的宽度。该值的范围从 1 到 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);
}
。高度#
.height 返回检测到的对象的高度。
访问
SensorName.objects[index].height
返回值
返回一个 int 之间的整数,表示检测到的对象的高度(以像素为单位)。该值的范围从 1 到 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#
.centerX 返回检测到的对象中心的 x 坐标。
访问
SensorName.objects[index].centerX
返回值
返回一个 int 整数,表示对象中心的 x 坐标(以像素为单位)。该值的范围从 0 到 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#
.centerY 返回检测到的对象的中心的 y 坐标。
访问
SensorName.objects[index].centerY
返回值
返回一个 int 整数,表示对象中心的 y 坐标(以像素为单位)。该值的范围从 0 到 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#
.originX 返回检测到的对象边界框左上角的 x 坐标。
访问
SensorName.objects[index].originX
返回值
返回一个 int 整数,表示对象边界框左上角的 x 坐标(以像素为单位)。该值的范围从 0 到 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#
.originY 返回检测到的对象边界框左上角的 y 坐标。
访问
SensorName.objects[index].originY
返回值
返回一个 int 整数,表示对象边界框左上角的 y 坐标(以像素为单位)。该值的范围从 0 到 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);
}
。角度#
.angle 返回检测到的颜色代码的方向。
访问
SensorName.objects[index].angle
返回值
返回一个 double 值,表示检测到的颜色代码的方向(以度为单位)。该值的范围从 0 到 359.9。
示例
// 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);
}