ビジョンセンサー#
導入#
vision クラスは、IQ(第2世代)ビジョンセンサーの制御とデータアクセスに使用されます。このセンサーは視野内のカラーシグネチャを検出および追跡し、ロボットがターゲットを識別して視覚情報にリアルタイムで対応できるようにします。
ビジョンセンサーは、複数の特徴的な物体を同時に検出することができ、物体の位置、幅、高さ、中心座標などの情報を提供する。
クラスコンストラクタ#
vision(
int32_t index,
uint8_t bright,
Args&... sigs );
クラスデストラクタ#
vision オブジェクトを破壊し、関連するリソースを解放します。
~vision();
パラメータ#
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ビジョンセンサーが接続されているスマートポートは、 |
|
|
ビジョンセンサーの輝度レベル。 |
|
|
センサーに登録する 1 つ以上の |
例#
// 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 から始まる幅の大きい順から小さい順にオブジェクトが格納されます。各オブジェクトの properties には、そのインデックスを使用してアクセスできます。一致するオブジェクトが検出されない場合、objectCount は 0 になり、 objects[i].exists は 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番号。 |
|
|
検索対象の |
|
|
検索対象の |
|
|
検出するオブジェクトの最大数。 |
検出された一致するオブジェクトの数を表す、 int32_t 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#
ビジョンセンサーがIQ(第2世代)ブレインに接続されているかどうかを返します。
Available Functionsbool installed();
この関数は引数を取りません。
Return Valuesビジョンセンサーが接続されているかどうかを示すブール値を返します。
-
true— The Vision Sensor is connected. -
false— The Vision Sensor is not connected.
不動産#
takeSnapshot を呼び出すと、ビジョンセンサーの検出結果が更新されます。各スナップショットは、要求されたカラーシグネチャまたはカラーコードに対して検出されたオブジェクトを含む objects 配列を更新します。
ビジョンセンサーデータは、 Vision1.objects[index]に格納されているオブジェクトのプロパティを介してアクセスされます。ここで、 index 0から始まります。
ビジョンセンサーの画像解像度は316×212ピクセルです。物体の位置とサイズは、センサーの現在の視野を基準としたピクセル単位で報告されます。
以下の物件が利用可能です。
largestObject— 最新のスナップショットから検出された最大のオブジェクトを選択します。objectCount—objects。objects— 検出されたオブジェクトの配列がtakeSnapshotによって更新されました。.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
成分 |
説明 |
|---|---|
|
ビジョンセンサーインスタンスの名前。 |
|
配列内のオブジェクトインデックス。インデックスは |
|
利用可能なオブジェクトプロパティの1つ。 |
.exists#
.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
戻り値
オブジェクトの中心の x 座標をピクセル単位で表す int 整数値を返します。値の範囲は 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
戻り値
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);
}
.originX#
.originX は、検出されたオブジェクトのバウンディングボックスの左上隅の x 座標を返します。
アクセス
SensorName.objects[index].originX
戻り値
オブジェクトの境界ボックスの左上隅の x 座標をピクセル単位で表す int の整数値を返します。値の範囲は 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
戻り値
オブジェクトの境界ボックスの左上隅のy座標をピクセル単位で表す int を返します。値の範囲は 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);
}