Αισθητήρας όρασης#
Εισαγωγή#
Ο αισθητήρας όρασης είναι ένας αισθητήρας που βασίζεται σε κάμερα και μπορεί να ανιχνεύει και να παρακολουθεί χρωματικές υπογραφές και χρωματικούς κωδικούς. Επιτρέπει σε ένα ρομπότ να αναλύει το περιβάλλον του και να ανταποκρίνεται σε οπτικά ερεθίσματα.
For the examples below, the configured Vision Sensor will be named vision_1, and the configured Color Signature objects, such as RED_BOX, will be used in all subsequent examples throughout this API documentation when referring to Vision class methods.
Παρακάτω είναι μια λίστα με όλες τις μεθόδους:
Λήψη δεδομένων από τον αισθητήρα όρασης.
take_snapshot— Captures data for a specific signature.installed— Whether the Vision Sensor is connected to the IQ (2nd gen) Brain.
Properties — Object data returned from take_snapshot.
largest_object— Immediately select the largest object from the Vision Sensor snapshot..exists— Whether the object exists in the current detection..width— Width of the detected object..height— Height of the detected object..centerX— X position of the object’s center..centerY— Y position of the object’s center..angle— Orientation of the Color Code..originX— X position of the object’s top-left corner..originY— Y position of the object’s top-left corner.
Κατασκευαστές — Μη αυτόματη αρχικοποίηση και ρύθμιση παραμέτρων των αισθητήρων.
Αποκτητές#
take_snapshot#
take_snapshot filters data from the Vision Sensor frame to a single signature — a saved description of something the sensor can recognize, such as a configured Color Signature or Color Code — and returns a tuple.
Οι Υπογραφές Χρώματος και Κωδικοί Χρώματος πρέπει πρώτα να ρυθμιστούν στο Vision Utility πριν μπορέσουν να χρησιμοποιηθούν με αυτήν τη μέθοδο.
Η πλειάδα αποθηκεύει αντικείμενα ταξινομημένα από το μεγαλύτερο στο μικρότερο κατά πλάτος, ξεκινώντας από τον δείκτη 0. Η πρόσβαση στις ιδιότητες κάθε αντικειμένου είναι δυνατή χρησιμοποιώντας τον δείκτη του. Επιστρέφεται μια κενή πλειάδα εάν δεν εντοπιστούν αντικείμενα που να ταιριάζουν.
Usage:
vision_1.take_snapshot(SIGNATURE)
Παράμετροι |
Περιγραφή |
|---|---|
|
Filters the dataset to only include data of the given signature. Available signatures are:
|
# Move forward if a red object is detected
while True:
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
Χρωματικές υπογραφές#
A Color Signature is a unique color that the Vision Sensor can recognize. These signatures allow the Vision Sensor to detect and track objects based on their color. Once a Color Signature is configured, the sensor can identify objects with that specific color in its field of view. Color signatures are used with take_snapshot to process and detect colored objects in real-time.
In order to use a configured Color Signature in a project, its name must be the name of the Vision Sensor, two underscores, and then the Color Signature’s name. For example: vision_1__RED_BOX.
# Display if any objects match the RED_BOX signature
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Signature
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
brain.screen.print("Color signature detected!")
wait(100, MSEC)
Κωδικοί Χρώματος#
Ένας Χρωματικός Κώδικας είναι ένα δομημένο μοτίβο που αποτελείται από χρωματικές υπογραφές που είναι διατεταγμένες σε μια συγκεκριμένη σειρά. Αυτοί οι κωδικοί επιτρέπουν στον Αισθητήρα Όρασης να αναγνωρίζει προκαθορισμένα μοτίβα χρωμάτων. Οι Χρωματικοί Κώδικες είναι χρήσιμοι για την αναγνώριση σύνθετων αντικειμένων ή τη δημιουργία μοναδικών δεικτών για αυτόνομη πλοήγηση.
In order to use a configured Color Code in a project, its name must be the name of the sensor, two underscores, and then the Color Code’s name. For example: vision_1__BOX_CODE.
# Display if any objects match the BOX_CODE code
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Code
box_code = vision_1.take_snapshot(vision_1__BOX_CODE)
if box_code[0].exists:
brain.screen.print("Color code detected!")
wait(100, MSEC)
installed#
installed returns a Boolean indicating whether the Vision Sensor is currently connected to the IQ (2nd gen) Brain.
True— The Vision Sensor is connected to the IQ (2nd gen) Brain.False— The Vision Sensor is not connected to the IQ (2nd gen) Brain.
Usage:
vision_1.installed()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Display a message if the Vision Sensor is connected
if vision_1.installed():
brain.screen.print("Installed!")
Σκηνικά θέατρου#
largest_object#
largest_object retrieves the largest detected object to get data from in the tuple returned from the latest use of take_snapshot.
Αυτή η μέθοδος μπορεί να χρησιμοποιηθεί για να λαμβάνεται πάντα το μεγαλύτερο αντικείμενο από μια πλειάδα χωρίς να καθορίζεται δείκτης.
Usage:
vision_1.largest_object()
# Turn slowly until the largest object is centered in
# front of the Vision Sensor
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)
while True:
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if 140 < vision_1.largest_object().centerX < 180:
drivetrain.stop()
wait(10,MSEC)
Tuple Properties#
There are eight properties that are included with each object stored in a tuple after take_snapshot is used.
All property values describe the detected object’s position and size in the Vision Sensor’s view at the moment take_snapshot was used. These values are measured in pixels, based on the sensor’s 316 by 212 pixel resolution.
.υπάρχει#
.exists returns a Boolean indicating if the index exists in the tuple or not.
True— The index exists.False— The index does not exist.
# Check if at least one red objects is detected
# You will receive an error if no objects are detected
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
red_objects = vision_1.take_snapshot(vision_1__RED_BOX)
if red_objects[0].exists:
brain.screen.print("At least 1")
else:
brain.screen.print("No red objects")
wait(0.5, SECONDS)
.πλάτος#
.width returns the width of the detected object in pixels, which is an integer between 1 and 316.
# Move towards a blue object until its width is
# larger than 100 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].width < 100:
drivetrain.drive_for(FORWARD, 10, MM)
else:
drivetrain.stop()
wait(50, MSEC)
.ύψος#
.height returns the height of the detected object in pixels, which is an integer between 1 and 212.
# Move towards a blue object until its height is
# larger than 100 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].height < 100:
drivetrain.drive_for(FORWARD, 10, MM)
else:
drivetrain.stop()
wait(50, MSEC)
.centerX#
.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 316.
# Turn slowly until the largest blue object is centered
# in front of the Vision Sensor.
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if 140 < vision_1.largest_object().centerX < 180:
drivetrain.stop()
wait(10,MSEC)
.centerY#
.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 212.
# Move towards a blue object until its
# center y-coordinate is more than 140 pixels
while True:
blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)
if blue_box[0].exists:
if blue_box[0].centerY < 140:
drivetrain.drive(FORWARD)
else:
drivetrain.stop()
wait(50, MSEC)
.γωνία#
.angle returns the orientation of the detected object in degrees, which is an integer between 0 and 359.
# Turn left or right depending on how a
# configured box code is rotated
while True:
box_code = vision_1.take_snapshot(vision_1__BOX_CODE)
if box_code[0].exists:
if 50 < box_code[0].angle < 100:
drivetrain.turn(RIGHT)
elif 270 < box_code[0].angle < 330:
drivetrain.turn(LEFT)
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 in pixels, which is an integer between 0 and 316.
# Display if a red object is to the
# left or the right
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1,1)
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if red_box[0].originX < 160:
brain.screen.print("To the left!")
else:
brain.screen.print("To the right!")
wait(50, MSEC)
.originY#
.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 212.
# Display if a red object is close or far
# from the robot
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1,1)
red_box = vision_1.take_snapshot(vision_1__RED_BOX)
if red_box[0].exists:
if red_box[0].originY < 80:
brain.screen.print("Far")
else:
brain.screen.print("Close")
wait(50, MSEC)
Κατασκευαστές#
Constructors are used to manually create Vision, Signature, and Code objects, which are necessary for configuring the sensors outside of VEXcode.
Vision Sensor#
Vision creates a Vision Sensor.
Χρήση
Vision(port, brightness, sigs)
Παράμετροι |
Περιγραφή |
|---|---|
|
Σε ποια έξυπνη θύρα είναι συνδεδεμένος ο αισθητήρας όρασης, από το 1 έως το 12. |
|
Προαιρετικά. Η τιμή φωτεινότητας για τον αισθητήρα όρασης, από 1 έως 100. |
|
Προαιρετικό. Το όνομα ενός ή περισσότερων αντικειμένων Υπογραφή χρώματος ή Κωδικός χρώματος. |
Παράδειγμα
# Create a new Signature "RED_BOX" with the Signature class
RED_BOX = Signature(1, -3911, -3435, -3673,10879, 11421, 11150,2.5, 0)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the "RED_BOX" Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)
# Move forward if a red object is detected
while True:
red_object = vision_1.take_snapshot(RED_BOX)
if red_object[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
Color Signature#
Signature creates a Color Signature. Up to seven different Color Signatures can be stored on a Vision Sensor at once.
Χρήση:
Signature(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)
Παράμετρος |
Περιγραφή |
|---|---|
|
The |
|
The value from |
|
The value from |
|
The value from |
|
The value from |
|
The value from |
|
The value from |
|
The value from |
|
The value from |
Για να λάβετε τις τιμές για να δημιουργήσετε μια Υπογραφή Χρώματος, μεταβείτε στο Vision Utility. Μόλις ρυθμιστεί μια Υπογραφή Χρώματος, αντιγράψτε τις τιμές των παραμέτρων από το παράθυρο Διαμόρφωσης.
Παράδειγμα
# Create a new Signature RED_BOX with the Signature class
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the RED_BOX Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)
# Move forward if a red object is detected
while True:
red_object = vision_1.take_snapshot(RED_BOX)
if red_object[0].exists:
drivetrain.drive_for(FORWARD, 10, MM)
wait(5, MSEC)
Color Code#
Code creates a Color Code. It requires at least two already defined Color Signatures in order to be used. Up to eight different Color Codes can be stored on a Vision Sensor at once.
Χρήση:
Code(sig1, sig2, sig3, sig4, sig5)
Παράμετρος |
Περιγραφή |
|---|---|
|
Μια [Υπογραφή Χρώματος] που δημιουργήθηκε προηγουμένως (#color-signature). |
|
Μια [Υπογραφή Χρώματος] που δημιουργήθηκε προηγουμένως (#color-signature). |
|
Προαιρετικό. Μια [Υπογραφή Χρώματος] που δημιουργήθηκε προηγουμένως (#color-signature). |
|
Προαιρετικό. Μια [Υπογραφή Χρώματος] που δημιουργήθηκε προηγουμένως (#color-signature). |
|
Προαιρετικό. Μια [Υπογραφή Χρώματος] που δημιουργήθηκε προηγουμένως (#color-signature). |
Παράδειγμα
# Create two new Signatures for a red and blue box
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440, 2.5, 1)
BLUE_BOX = Signature(2, -4443, -3373, -3908,6253, 7741, 6997, 2.5, 1)
# Create a Color Code for a red box to the left of a blue box
RED_BLUE = Code(RED_BOX, BLUE_BOX)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the red_box and blue_box Signatures.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX, BLUE_BOX)
# Display a message if Color Code is detected
while True:
brain.screen.set_cursor(1, 1)
brain.screen.clear_row(1)
# Change to any configured Color Code
box_code = vision_1.take_snapshot(RED_BLUE)
if box_code[0].exists:
brain.screen.print("Color code detected!")
wait(100, MSEC)