想象#
VEX EXP 的视觉传感器可以检测并追踪颜色特征和颜色代码。这使得视觉传感器能够分析周围环境并根据检测到的视觉数据做出反应。以下是所有方法的列表:
方法——从视觉传感器获取数据。
takeSnapshot – 捕获特定颜色特征或颜色代码的数据。
largestObject – 立即从快照中选择最大的对象。
objectCount – 以整数返回检测到的物体的数量。
objects – 返回包含检测到的对象属性的数组。
已安装 – 视觉传感器是否连接到 EXP Brain。
属性 – 从 takeSnapshot 返回的对象数据。
.exists – 以布尔值表示该对象是否存在于当前检测中。
.width – 检测到的物体的宽度(以像素为单位)。
.height – 检测到的物体的高度(以像素为单位)。
.centerX – 对象中心的 X 位置(以像素为单位)。
.centerY – 对象中心的 Y 位置(以像素为单位)。
.angle – 颜色代码的方向(以度为单位)。
.originX – 对象左上角的 X 位置(以像素为单位)。
.originY – 对象左上角的 Y 位置(以像素为单位)。
构造函数——手动初始化和配置视觉传感器。
vision – 创建视觉传感器。
vision::signature – 创建颜色签名。
vision::code – 创建颜色代码。
在 VEXcode 中,视觉传感器及其配置的 颜色签名 和 颜色代码 的初始化是自动完成的。在以下示例中,配置的视觉传感器将被命名为 Vision1
。要手动初始化和构建视觉传感器及其颜色签名和颜色代码,请参阅本页的 构造函数 部分。
方法#
拍摄快照#
takeSnapshot
方法会从视觉传感器捕获图像,根据配置的颜色签名和颜色代码进行处理,并更新 objects
数组。此方法还可以限制快照中捕获的对象数量。
必须先在 Vision Utility 中配置 颜色签名 和 颜色代码,然后才能使用此方法。
objects
数组存储按宽度从大到小排序的对象,从索引 0 开始。可以使用其索引访问每个对象的 属性。如果没有检测到匹配的对象,则 objects
为空数组。
默认用法:
Vision1.takeSnapshot(signature)
范围 |
描述 |
---|---|
|
要获取哪个 |
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.clearScreen();
// Change to any configured Color Signature
Vision1.takeSnapshot(Vision1__REDBOX);
if (Vision1.objects[0].exists){
Brain.Screen.print("Color signature");
Brain.Screen.newLine();
Brain.Screen.print("detected!");
wait(0.1,seconds);
}
}
颜色代码#
颜色代码是由按特定顺序排列的颜色特征组成的结构化模式。这些代码使视觉传感器能够识别预定义的颜色模式。颜色代码可用于识别复杂物体或为自主导航创建独特的标记。
要在项目中使用已配置的颜色代码,其名称必须以字符串形式传递,格式如下:视觉传感器名称,后跟两个下划线,然后是颜色代码名称。例如:“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
返回一个整数,指示视觉传感器当前是否连接到 EXP Brain。
1
– 视觉传感器连接到 EXP Brain。0
– 视觉传感器未连接到 EXP Brain。
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display a message if the Vision Sensor is detected
if (Vision1.installed()){
Brain.Screen.print("Vision Sensor");
Brain.Screen.newLine();
Brain.Screen.print("Installed!");
}
对象#
objects
返回检测到的对象属性的数组。使用该数组可以访问各个对象的特定 property 值。
默认用法:
Vision1.objects
特性#
使用“takeSnapshot”后,“objects”数组中存储的每个对象都包含八个属性。
某些属性值基于使用“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);
}
构造函数#
构造函数用于手动创建 vision
、signature
和 code
对象,这些对象是在 VEXcode 之外配置视觉传感器所必需的。如果提供的参数较少,则应在构造函数定义中使用默认参数或函数重载。
对于下面的示例,配置的视觉传感器将被命名为“Vision1”,并且配置的颜色签名对象(例如“Vision1__BLUEBOX”)将在整个 API 文档的所有后续示例中用于引用“vision”类方法。
视觉传感器#
“vision”创建一个视觉传感器并配置传感器要使用的亮度级别和签名。
默认用法:vision(int32_t index,uint8_t bright,Args&...sigs)
默认参数 |
描述 |
---|---|
|
视觉传感器连接到的有效 智能端口。 |
|
视觉传感器的亮度值,从 10 到 150。 |
|
// Construct a vision object Vision1 with 1 color
// Vision1__REDBOX.
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
创建一个颜色签名。视觉传感器上最多可同时存储七种不同的颜色签名。
默认用法:
签名(索引、uMin、uMax、uMean、vMin、vMax、vMean、rgb、类型)
范围 |
描述 |
---|---|
|
|
|
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);
}
颜色代码#
“代码”用于创建颜色代码。它需要至少两个已定义的颜色签名才能使用。一个视觉传感器最多可同时存储八个不同的颜色代码。
默认用法:
代码(sig1,sig2)
参数 |
描述 |
---|---|
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
// Construct a vision object Vision1 with two Color
// Signatures, Vision1__REDBOX and Vision1__BLUEBOX,
// Alongside a Color Code for a red box to the left of
// a blue box, Vision1__BOXCODE.
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){
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);
}
重载
代码(sig1,sig2,sig3)
代码(sig1,sig2,sig3,sig4)
代码(sig1、sig2、sig3、sig4、sig5)
重载参数 |
描述 |
---|---|
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
|
先前创建的“签名”对象,或先前创建的签名对象的“int32_t”索引。 |
// Construct a vision object Vision1 with two Color
// Signatures, Vision1__REDBOX and Vision1__BLUEBOX,
// Alongside a Color Code for a red box to the left of
// a blue box alternating for 5 boxes, Vision1__BOXCODE.
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, Vision1__REDBOX, Vision1__BLUEBOX, Vision1__REDBOX);
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){
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);
}