想象#
介绍#
VEX V5 的视觉传感器能够检测和跟踪颜色特征和颜色代码。这使得视觉传感器能够分析周围环境,并根据检测到的视觉数据做出反应。
This page uses vision_1, RED_BOX, and BOX_CODE as the example Vision Sensor, Color Signature, and Color Code names respectively. Replace them with your own configured names as needed.
以下是所有方法的列表:
数据获取器 – 从视觉传感器获取数据。
take_snapshot– Captures data for a specific Color Signature or Color Code.largest_object– Select the largest object from the snapshot.installed– Whether the Vision Sensor is connected to the V5 Brain.
Properties – Object data returned from take_snapshot.
.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..angle– Orientation of the Color Code 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.
构造函数 – 手动初始化和配置视觉传感器及其特征。
获取器#
take_snapshot#
take_snapshot filters the data from the Vision Sensor frame to return a tuple. The Vision Sensor can detect configured Color Signatures and Color Codes.
要使用此方法,必须先在视觉实用程序中配置颜色签名和颜色代码。
该元组按宽度从大到小排序存储对象,索引从 0 开始。每个对象的属性 (#properties) 可以通过其索引访问。如果没有检测到匹配的对象,则返回空元组。
Usage:
vision_1.take_snapshot(SIGNATURE)
参数 |
描述 |
|---|---|
|
What signature to get data of. This is the name of the Vision Sensor, two underscores, and then the Color Signature’s or Color Code’s name. For example: |
# 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 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 Vision 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)
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)
installed#
installed returns a Boolean indicating whether the Vision Sensor is currently connected to the V5 Brain.
True– The Vision Sensor is connected to the V5 Brain.False– The Vision Sensor is not connected to the V5 Brain.
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Display a message if the Vision Sensor is connected
if vision_1.installed():
brain.screen.print("Vision Sensor Installed!")
特性#
There are eight properties that are included with each object stored in a tuple after take_snapshot is used.
Some property values are based off of the detected object’s position in the Vision Sensor’s view at the time that take_snapshot was used. The Vision Sensor has a resolution of 316 by 212 pixels.
.exists#
.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 object 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#
.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#
.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#
.angle returns the orientation of the detected object in degrees, which is an integer between 0 and 360.
# 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 70 < box_code[0].angle < 110:
drivetrain.turn_for(RIGHT, 45, DEGREES)
elif 250 < box_code[0].angle < 290:
drivetrain.turn_for(LEFT, 45, DEGREES)
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 Vision Sensor outside of VEXcode.
Vision#
Vision creates a Vision Sensor.
用法
Vision(smartport, brightness, sigs)
参数 |
描述 |
|---|---|
|
The Smart Port that the Vision Sensor is connected to, written as |
|
可选。视觉传感器的亮度值,范围从 1 到 100。 |
|
Optional. The name of one or more Color Signature or Color Code objects separated by commas ( |
# Create the Color Signatures
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 the Color Code
BOX_CODE = Code(RED_BOX, BLUE_BOX)
"""
Create a Vision Sensor with the following values:
- Port 1
- brightness: 80
- RED_BOX and BOX_CODE signatures
"""
vision_1 = Vision(Ports.PORT1, 80, RED_BOX, BOX_CODE)
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 |
要获取创建颜色签名所需的值,请转到视觉实用程序。配置颜色签名后,从配置窗口复制参数值。
"""
Create a Color Signature with the following values:
- index: 1
- uMin: 10121
- uMax: 10757
- uMean: 10439
- vMin: -1657
- vMax: -1223
- vMean: -1440
- rgb: 2.5
- type: 1
"""
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1)
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)
范围 |
描述 |
|---|---|
|
先前创建的颜色签名。 |
|
先前创建的颜色签名。 |
|
可选。先前创建的颜色签名。 |
|
可选。先前创建的颜色签名。 |
|
可选。先前创建的颜色签名。 |
# Create the Color Signatures
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 with the Color Signatures
BOX_CODE = Code(RED_BOX, BLUE_BOX)