想象#

介绍#

VEX AIR 无人机的视觉传感器可检测并追踪 AprilTag ID。这使得无人机能够分析周围环境、跟踪物体并根据检测到的视觉数据做出反应。以下是所有可用方法和属性的列表:

Getters——检测无人机是否持有物体。

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

吸气剂#

get_data#

get_data filters the data from the Vision Sensor frame to return a tuple. The Vision Sensor can detect AprilTag IDs.

该元组存储的对象按宽度从大到小排序,从索引 0 开始。每个对象的 属性 可以通过其索引访问。如果未检测到匹配的对象,则返回一个空元组。

Usage:
drone.vision.get_data(camera, signature, count)

参数

描述

camera

The camera to retrieve data from:

  • DOWNWARD_CAMERA
  • FORWARD_CAMERA

signature

What signature to get data of. Available signatures are:

  • ALL_TAGS - All AprilTags
  • TAG0 through TAG37 - A single AprilTag with an ID from 0 to 37

count

可选。设置可返回的最大对象数,范围为 1 至 24(默认值: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)

特性#

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:

  • 前向:640 x 480像素

  • 向下:640 x 400像素

.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.

0° 表示物体中心与传感器中心对齐。正值表示物体位于右侧,负值表示物体位于左侧。

传感器

返回

向下

无人机前部必须旋转一定角度才能与物体的中心对齐,角度范围从 -180º 到 180º。

向前

物体向视觉传感器中心左侧或右侧偏移的度数,范围为 -180º 至 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:

对象类型

数值

包含的对象

drone.vision.TAG_OBJECT

8

四月标签 ID

drone.vision.COLOR_OBJECT

1

彩色签名 - 即将推出!

drone.vision.CODE_OBJECT

2

颜色代码 - 即将推出!

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