Αισθητήρας όρασης#
Εισαγωγή#
The vision class is used to control and access data from the IQ (2nd gen) Vision Sensor. This sensor detects and tracks Color Signatures within its field of view, allowing your robot to identify targets and respond to visual information in real time.
Ο αισθητήρας όρασης μπορεί να ανιχνεύσει ταυτόχρονα πολλά αντικείμενα-υπογραφές και παρέχει πληροφορίες όπως η θέση του αντικειμένου, το πλάτος, το ύψος και οι κεντρικές συντεταγμένες.
Κατασκευαστές κλάσεων#
vision(
int32_t index,
uint8_t bright,
Args&... sigs );
Καταστροφέας κλάσης#
Destroys the vision object and releases associated resources.
~vision();
Παράμετροι#
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
The Smart Port that the Vision Sensor is connected to, written as |
|
|
Το επίπεδο φωτεινότητας του αισθητήρα όρασης. |
|
|
One or more |
Παραδείγματα#
// 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
Συναρτήσεις Μελών#
The vision class includes the following member functions:
takeSnapshot— Captures data for a specific Color Signature or Color Code.installed— Whether the Vision Sensor is connected to the IQ (2nd gen) Brain.
To access detected object data after calling takeSnapshot, use the available Properties.
Before calling any vision member functions, a vision instance must be created, as shown below:
/* 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#
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.
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 — Λαμβάνει ένα στιγμιότυπο και αναζητά αντικείμενα που ταιριάζουν με το καθορισμένο αναγνωριστικό υπογραφής.
int32_t takeSnapshot( uint32_t id );
2 — Takes a snapshot and searches for objects matching the specified
vision::codeobject.int32_t takeSnapshot( code &cc );
3 — Takes a snapshot and searches for objects matching the specified
vision::signatureobject.int32_t takeSnapshot( signature &sig );
4 — Λαμβάνει ένα στιγμιότυπο και αποθηκεύει μόνο τον μεγαλύτερο καθορισμένο αριθμό αντικειμένων που ταιριάζουν με το αναγνωριστικό υπογραφής.
int32_t takeSnapshot( uint32_t id, uint32_t count );
5 — Takes a snapshot and stores only the largest specified number of objects matching the specified
vision::codeobject.int32_t takeSnapshot( code &cc, uint32_t count );
Parameters6 — Takes a snapshot and stores only the largest specified number of objects matching the specified
vision::signatureobject.int32_t takeSnapshot( signature &sig, uint32_t count );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Ο αριθμός αναγνώρισης υπογραφής που θα αναζητηθεί. |
|
|
A reference to a |
|
|
A reference to a |
|
|
Ο μέγιστος αριθμός αντικειμένων προς ανίχνευση. |
Returns an int32_t representing the number of matching objects detected.
Ο αισθητήρας όρασης πρέπει να τραβήξει ένα στιγμιότυπο πριν είναι δυνατή η πρόσβαση στα δεδομένα του αντικειμένου.
When
countis specified, only the largest detected objects (up to the specified amount) are stored.
Παραδείγματα
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 (1ης γενιάς).
Available Functionsbool installed();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΕπιστρέφει μια λογική τιμή που υποδεικνύει εάν ο αισθητήρας όρασης είναι συνδεδεμένος:
-
true— The Vision Sensor is connected. -
false— The Vision Sensor is not connected.
Σκηνικά θέατρου#
Calling takeSnapshot updates the Vision Sensor’s detection results. Each snapshot refreshes the objects array, which contains the detected objects for the requested Color Signature or Color Code.
Vision Sensor data is accessed through properties of objects stored in Vision1.objects[index], where index begins at 0.
Η ανάλυση εικόνας του αισθητήρα όρασης είναι 316 επί 212 pixel. Οι ιδιότητες θέσης και μεγέθους αντικειμένου αναφέρονται σε μονάδες pixel σε σχέση με την τρέχουσα προβολή του αισθητήρα.
Διατίθενται τα ακόλουθα ακίνητα:
largestObject— Selects the largest detected object from the most recent snapshot.objectCount— Returns the number of valid objects stored inobjects.objects— Array of detected objects updated bytakeSnapshot..exists— Whether the object entry contains valid data..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..originX— X position of the object’s top-left corner in pixels..originY— Y position of the object’s top-left corner in pixels..angle— Orientation of the detected Color Code in degrees (when applicable).
largestObject#
Retrieves the largest detected object from the objects array.
This method can be used to always get the largest object from objects without specifying an index.
VisionSensor.largestObject
Συστατικό |
Περιγραφή |
|---|---|
|
Το όνομα της παρουσίας του 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#
Returns the number of items inside the objects array as an integer.
VisionSensor.objectCount
Συστατικό |
Περιγραφή |
|---|---|
|
Το όνομα της παρουσίας του 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 is an array containing the detected objects from the most recent call to takeSnapshot.
Each element in the array represents a detected object and provides access to its properties, such as width, height, centerX, and more.
Objects are ordered from largest to smallest (by width), beginning at index 0.
VisionSensor.objects[index].property
Συστατικό |
Περιγραφή |
|---|---|
|
Το όνομα της παρουσίας του Vision Sensor. |
|
The object index in the array. Index begins at |
|
Μία από τις διαθέσιμες ιδιότητες αντικειμένου. |
.υπάρχει#
.exists returns whether the specified object index contains a valid detected object.
Πρόσβαση
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 returns the width of the detected object.
Πρόσβαση
SensorName.objects[index].width
Επιστρεφόμενες τιμές
Returns an int representing the width of the detected object in pixels. The value ranges from 1 to 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 returns the height of the detected object.
Πρόσβαση
SensorName.objects[index].height
Επιστρεφόμενες τιμές
Returns an int representing the height of the detected object in pixels. The value ranges from 1 to 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 returns the x-coordinate of the center of the detected object.
Πρόσβαση
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);
}
.centerY#
.centerY returns the y-coordinate of the center of the detected object.
Πρόσβαση
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 returns the x-coordinate of the top-left corner of the detected object’s bounding box.
Πρόσβαση
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);
}
.originY#
.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box.
Πρόσβαση
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 returns the orientation of the detected Color Code.
Πρόσβαση
SensorName.objects[index].angle
Επιστρεφόμενες τιμές
Returns a double representing the orientation of the detected Color Code in degrees. The value ranges from 0 to 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);
}