Visión#
Introducción#
El sensor de visión del dron VEX AIR detecta y rastrea los identificadores de AprilTag. Esto permite al dron analizar su entorno, seguir objetos y reaccionar según los datos visuales detectados. A continuación, se muestra una lista de todos los métodos y propiedades disponibles:
Getters – Detecta si el dron sostiene un objeto.
get_data– Returns a tuple of detected objects based on a given signature.
Properties – Object data returned from get_data.
exists– Whether the object exists in the current detection as a Boolean.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.bearing– Angle of the object relative to the front Vision Sensor or the degrees needed for the drone to face the object.rotation– Orientation of the object in degrees.originX– X position of the object’s top-left corner in pixels.originY– Y position of the object’s top-left corner in pixels.id– Tag ID of the object.type– Returns the object’s type.
Captadores#
get_data#
get_data filters the data from the Vision Sensor frame to return a tuple. The Vision Sensor can detect AprilTag IDs.
La tupla almacena objetos ordenados de mayor a menor por ancho, comenzando en el índice 0. Se puede acceder a las propiedades de cada objeto mediante su índice. Si no se detectan objetos coincidentes, se devuelve una tupla vacía.
Usage:
drone.vision.get_data(camera, signature, count)
Parámetros |
Descripción |
|---|---|
|
The camera to retrieve data from:
|
|
What signature to get data of. Available signatures are:
|
|
Opcional. Establece el número máximo de objetos que se pueden devolver, de 1 a 24 (valor predeterminado: 8). |
# Climb upward when an AprilTag is detected
drone.take_off(climb_to=500)
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
drone.climb_for(direction=UP, distance=200)
Propiedades#
There are multiple properties that are included with each object stored in a tuple after get_data is used.
Some property values are based off of the detected object’s position in the Vision Sensor’s view at the time that get_data was used. Each Vision Sensor has a different resolution:
Orientado hacia adelante: 640 x 480 píxeles
Mirando hacia abajo: 640 x 400 píxeles
.exists#
.exists returns if the index exists in the tuple or not. This property returns a Boolean value. Returns an error if there are no detected objects.
True– The index exists.False– The index does not exist.
# Climb upward when an AprilTag is detected
drone.take_off(climb_to=500)
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
drone.climb_for(direction=UP, distance=200)
.width#
.width returns the width of the detected object in pixels as an integer from 1 to 640.
# Show if the AprilTag ID appears large or small
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
clear_console()
if vision_data[0].exists:
if vision_data[0].width > 100:
print("Large")
else:
print("Small")
wait(0.2, SECONDS)
.height#
.height returns the height of the detected object in pixels as an integer from 1 to 480.
# Show if the AprilTag ID appears large or small
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
clear_console()
if vision_data[0].exists:
if vision_data[0].height > 100:
print("Large")
else:
print("Small")
wait(0.2, SECONDS)
.centerX#
.centerX returns the x-coordinate of the center of the detected object in pixels as an integer from 1 to 640.
# Show if an AprilTag is to the left or the right of the camera
while True:
clear_console()
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
if vision_data[0].centerX < 320:
print("To the left!")
else:
print("To the right!")
wait(0.2, SECONDS)
.centerY#
.centerY returns the y-coordinate of the center of the detected object in pixels as an integer from 1 to 480.
# Take off and align drone with AprilTag ID on wall
# Change drone.take_off parameter to see different results
drone.take_off(climb_to=800)
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
if vision_data[0].centerY < 240:
print("Below the target!")
else:
print("Above the target!")
else:
print("No target found!")
.bearing#
.bearing returns an angle indicating an object’s position relative to the drone. The behavior depends on which Vision Sensor is used.
Un valor de 0° indica que el centro del objeto y el sensor están alineados. Los valores positivos indican que el objeto está a la derecha, mientras que los negativos indican que está a la izquierda.
Sensor |
Devoluciones |
|---|---|
Hacia abajo |
Grados que debe girar la parte frontal del dron para alinearse con el centro del objeto, de –180º a 180º. |
Adelante |
Grados en los que el objeto se desplaza hacia la izquierda o la derecha del centro del sensor de visión, de –180º a 180º. |
# Align the front of the drone to an AprilTag ID
drone.set_turn_velocity(15)
drone.take_off(climb_to=500)
while True:
vision_data = drone.vision.get_data(DOWNWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
if -20 < vision_data[0].bearing < 20:
# Hover once sensor is centered on AprilTag ID
drone.hover()
controller.sound.play(SUCCESS)
while controller.sound.is_active():
wait(50, MSEC)
elif vision_data[0].bearing < -21:
drone.turn(LEFT)
else:
drone.turn(RIGHT)
wait(5, MSEC)
.rotation#
.rotation returns the orientation of the detected AprilTag as an integer in degrees from 1 to 359.
# Turn left or right based on the rotation of an AprilTag ID
drone.take_off(climb_to=1000)
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
if 240 < vision_data[0].rotation < 300:
drone.turn_for(LEFT, 90)
elif 60 < vision_data[0].rotation < 120:
drone.turn_for(RIGHT, 90)
else:
drone.hover()
wait(0.2, SECONDS)
.originX#
.originX returns the x-coordinate of the top-left corner of the detected object’s bounding box in pixels as an integer from 1 to 640.
# Draw lines to the AprilTag origin from all corners
while True:
controller.screen.clear_screen()
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
controller.screen.draw_line(0, 0, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(0, 480, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(640, 0, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(640, 480, vision_data[0].originX, vision_data[0].originY)
wait(0.2, SECONDS)
.originY#
.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels as an integer from 1 to 480.
# Draw lines to the AprilTag origin from all corners
while True:
controller.screen.clear_screen()
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
controller.screen.draw_line(0, 0, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(0, 480, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(640, 0, vision_data[0].originX, vision_data[0].originY)
controller.screen.draw_line(640, 480, vision_data[0].originX, vision_data[0].originY)
wait(0.2, SECONDS)
.id#
.id returns the identification number of the detected AprilTag ID as an integer from 0 to 37.
# Display the detected AprilTag ID
while True:
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
clear_console()
if vision_data[0].exists:
print(vision_data[0].id)
wait(0.2, SECONDS)
.type#
.type returns what type of object is detected. It will return the following:
Tipo de objeto |
Valor numérico |
Objetos incluidos |
|---|---|---|
|
8 |
Identificadores de AprilTag |
|
1 |
Firmas de color - ¡Próximamente! |
|
2 |
Códigos de colores - ¡Próximamente! |
# Display what AprilTag ID is detected
while True:
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
vision_data = drone.vision.get_data(FORWARD_CAMERA, ALL_TAGS)
if vision_data[0].exists:
if vision_data[0].type == drone.vision.TAG_OBJECT:
controller.screen.print("AprilTag ID: ")
controller.screen.print(vision_data[0].id)
wait(0.1, SECONDS)