비전 센서#

소개#

vision 클래스는 IQ(2세대) 비전 센서의 데이터를 제어하고 액세스하는 데 사용됩니다. 이 센서는 시야 내의 색상 특징을 감지하고 추적하여 로봇이 목표물을 식별하고 시각 정보에 실시간으로 반응할 수 있도록 합니다.

비전 센서는 여러 개의 특징적인 객체를 동시에 감지할 수 있으며, 객체의 위치, 너비, 높이 및 중심 좌표와 같은 정보를 제공합니다.

클래스 생성자#

vision(
  int32_t  index,
  uint8_t  bright,
  Args&... sigs );

클래스 소멸자#

vision 객체를 파괴하고 관련 리소스를 해제합니다.

~vision();

매개변수#

매개변수

유형

설명

index

int32_t

비전 센서가 연결된 스마트 포트는 PORTx형식으로 표기되며, 여기서 x 포트 번호입니다(예: PORT1).

bright

uint8_t

비전 센서의 밝기 수준.

sigs

vision::signature or vision::code objects

센서에 등록할 하나 이상의 vision::signature 또는 vision::code 객체.

예시#

// 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 — 비전 센서가 IQ(2세대) 브레인에 연결되어 있는지 여부.

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 객체 배열을 업데이트합니다. 이 메서드는 스냅샷에 캡처되는 객체 수를 제한할 수도 있습니다.

objects 배열은 인덱스 0부터 시작하여 너비가 가장 큰 것부터 가장 작은 것 순으로 정렬된 객체를 저장합니다. 각 객체의 속성은 인덱스를 사용하여 접근할 수 있습니다. 일치하는 객체가 감지되지 않으면 objectCount는 0이 되고 objects[i].exists false가 됩니다.

Available Functions

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 );

6 — 스냅샷을 찍고 지정된 vision::signature 객체와 일치하는 지정된 수의 객체만 저장합니다.

int32_t takeSnapshot( signature &sig, uint32_t count );

Parameters

매개변수

유형

설명

id

uint32_t

검색할 서명 식별 번호입니다.

cc

code &

검색할 vision::code 객체에 대한 참조입니다.

sig

signature &

검색할 vision::signature 객체에 대한 참조입니다.

count

uint32_t

감지할 최대 객체 수.

Return Values

일치하는 객체가 감지된 수를 나타내는 int32_t 을 반환합니다.

Notes
  • 비전 센서는 객체 데이터에 접근하기 전에 먼저 스냅샷을 촬영해야 합니다.

  • 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#

비전 센서가 IQ(2세대) 브레인에 연결되었는지 여부를 반환합니다.

Available Functions
bool installed();

Parameters

이 함수는 매개변수를 받지 않습니다.

Return Values

비전 센서가 연결되었는지 여부를 나타내는 부울 값을 반환합니다.

  • true — The Vision Sensor is connected.
  • false — The Vision Sensor is not connected.

속성#

takeSnapshot 호출은 비전 센서의 감지 결과를 업데이트합니다. 각 스냅샷은 요청된 색상 서명 또는 색상 코드에 대해 감지된 객체를 포함하는 objects 배열을 새로 고칩니다.

비전 센서 데이터는 Vision1.objects[index]에 저장된 객체의 속성을 통해 액세스되며, 여기서 index 0 에서 시작합니다.

비전 센서 이미지 해상도는 316 x 212 픽셀입니다. 객체의 위치와 크기 속성은 센서의 현재 시야를 기준으로 픽셀 단위로 표시됩니다.

다음과 같은 부동산을 이용하실 수 있습니다:

  • largestObject — 가장 최근 스냅샷에서 감지된 가장 큰 객체를 선택합니다.

  • objectCount — objects 에 저장된 유효한 객체의 수를 반환합니다.

  • objects — takeSnapshot 에 의해 업데이트된 감지된 객체 배열.

  • .exists — 객체 항목에 유효한 데이터가 포함되어 있는지 여부.

  • .width — 감지된 객체의 너비(픽셀).

  • .height — 감지된 객체의 높이(픽셀 단위).

  • .centerX — 객체 중심의 X 위치(픽셀 단위).

  • .centerY — 객체 중심의 Y 위치(픽셀 단위).

  • .originX — 객체의 왼쪽 상단 모서리의 X 위치(픽셀 단위).

  • .originY — 객체의 왼쪽 상단 모서리의 Y 위치(픽셀 단위).

  • .angle — 감지된 색상 코드의 방향(도 단위)(해당되는 경우).

largestObject#

objects 객체로 이루어진 배열에서 가장 큰 감지된 객체를 검색합니다.

이 메서드는 인덱스를 지정하지 않고도 objects 객체 중 가장 큰 객체를 항상 가져오는 데 사용할 수 있습니다.

Syntax
VisionSensor.largestObject

Components

요소

설명

VisionSensor

Vision Sensor 인스턴스의 이름입니다.

예시

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 객체로 이루어진 배열 내 항목 수를 정수로 반환합니다.

Syntax
VisionSensor.objectCount

Components

요소

설명

VisionSensor

Vision Sensor 인스턴스의 이름입니다.

예시

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 부터 시작하여 너비를 기준으로 가장 큰 것부터 가장 작은 것 순으로 정렬됩니다.

Syntax
VisionSensor.objects[index].property

Components

요소

설명

VisionSensor

Vision Sensor 인스턴스의 이름입니다.

index

The object index in the array. Index begins at 0.

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

반환 값

Returns an int representing the x-coordinate of the object’s center in pixels. The value ranges from 0 to 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);
}

.센터Y#

.centerY 감지된 객체의 중심의 y 좌표를 반환합니다.

액세스

SensorName.objects[index].centerY

반환 값

Returns an int representing the y-coordinate of the object’s center in pixels. The value ranges from 0 to 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);
}

.오리진X#

.originX 감지된 객체의 경계 상자의 왼쪽 상단 모서리의 x 좌표를 반환합니다.

액세스

SensorName.objects[index].originX

반환 값

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.

예시

// 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); 
}

.오리진Y#

.originY 감지된 객체의 경계 상자의 왼쪽 상단 모서리의 y 좌표를 반환합니다.

액세스

SensorName.objects[index].originY

반환 값

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.

예시

// 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 의 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);
}