机器人专用 Python#
介绍#
英雄机器人 Flop 包含一个进气电机、一个机械臂电机和一个 IQ AI 视觉传感器。
所有标准的 VEXcode VR Python 命令都可以在 IQ 26-27 Level Up Playground 中使用。
以下是所有可用的机器人专用 Python 命令列表:
运动控制——移动并跟踪机器人的电机。
操作
spin – 使电机永远朝一个方向旋转。
spin_for – 使电机旋转特定角度或圈数。
spin_to_position – 将电机旋转到指定位置。
stop – 停止电机旋转。
设置
set_velocity – 设置电机的旋转速度。
set_timeout – 设置电机尝试完成运动的时间。
set_position – 将电机的当前位置更改为新值。
价值观
is_done – 返回电机是否停止运转。
is_spinning – 返回电机是否正在旋转。
position – 返回电机的当前位置。
velocity – 返回电机的旋转速度。
AI视觉——使用IQ AI视觉传感器捕捉和分析物体。
Getters
take_snapshot – 将传感器的当前帧过滤为特定签名,并返回检测到的对象元组。
特性
本页示例使用 Playground 的默认起始位置。
运动#
Flop 有两个机器人专用电机: intake_motor 和 arm_motor两者都使用标准电机指令。
方向常量:
intake_motor:FORWARD= 进气(收集);REVERSE= 排气(排出)arm_motor:FORWARD= 向上(抬起);REVERSE= 向下(放下)
旋转#
spin 表示电机将一直朝指定方向旋转。电机将持续旋转,直到收到下一个指令。
Usage:
motor.spin(direction)
参数 |
描述 |
|---|---|
|
旋转方向: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
spin_for#
spin_for 使电机旋转指定距离。除非传递了 wait=False ,否则项目会等待电机完成旋转后再运行下一个命令。
Usage:
motor.spin_for(direction, amount, unit)
参数 |
描述 |
|---|---|
|
旋转方向: |
|
旋转距离。可以是整数或小数。 |
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
自旋至位置#
spin_to_position 将电机旋转到指定位置。除非传递了 wait=False ,否则项目会等待电机完成旋转后再运行下一个命令。
Usage:
motor.spin_to_position(position, unit)
参数 |
描述 |
|---|---|
|
要旋转到的位置。可以是整数或小数。 |
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_to_position(1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
停止#
stop 停止电机旋转。
Usage:
motor.stop()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
wait(1, SECONDS)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
设置速度#
set_velocity 设置电机旋转速度,以 0 到 100 之间的百分比表示。
Usage:
motor.set_velocity(velocity, unit)
参数 |
描述 |
|---|---|
|
电机转速,以百分比表示,范围从 0 到 100。 |
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.set_velocity(100, PERCENT)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
设置超时#
set_timeout 设置电机在停止前尝试完成一次运动的秒数。
Usage:
motor.set_timeout(timeout, unit)
参数 |
描述 |
|---|---|
|
电机尝试完成一次动作的秒数。 |
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.set_timeout(2, SECONDS)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
设置位置#
set_position 将电机的当前位置更改为新值,重置其编码器。
Usage:
motor.set_position(position, unit)
参数 |
描述 |
|---|---|
|
新的位置值。 |
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.set_position(0, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
已完成#
is_done 返回一个布尔值,指示电机是否已完成其运动。
True– 电机已停止运转。False– 电机仍在运转。
Usage:
motor.is_done()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
while not arm_motor.is_done():
wait(5, MSEC)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
正在旋转#
is_spinning 返回一个布尔值,指示电机当前是否正在旋转。
True– 电机正在旋转。False– 电机不转动。
Usage:
motor.is_spinning()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
while intake_motor.is_spinning():
wait(5, MSEC)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
位置#
position 返回电机的当前位置。
Usage:
motor.position(unit)
参数 |
描述 |
|---|---|
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
while arm_motor.position(DEGREES) < 360:
arm_motor.spin(FORWARD)
wait(2, MSEC)
arm_motor.stop()
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
速度#
velocity 返回电机当前旋转速度的百分比,范围从 -100 到 100。
Usage:
motor.velocity(unit)
参数 |
描述 |
|---|---|
|
计量单位: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
wait(0.2, SECONDS)
brain.screen.print(intake_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
人工智能视觉#
拍摄快照#
take_snapshot 将 IQ AI Vision Sensor 帧中的数据过滤为单个签名(传感器可以识别的事物的保存描述,例如场上的游戏元素),并返回一个元组。
The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.
Usage:
ai_vision.take_snapshot(signature)
参数 |
描述 |
|---|---|
|
Filters the dataset to only include data of the given signature. Available signatures are:
|
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Turn to face a red Bean Bag
drivetrain.set_turn_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.centerX < 140:
drivetrain.turn(LEFT)
elif ai_object.centerX > 180:
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
return
break
# VR threads — Do not delete
vr_thread(main)
特性#
使用 take_snapshot 后,存储在元组中的每个对象都包含七个属性。
除 .id 外,所有属性值均描述了在执行 take_snapshot 操作时,IQ AI 视觉传感器视野中检测到的物体的位置和大小。这些值以像素为单位进行测量,基于传感器的 320 x 240 像素分辨率。
。宽度#
.width 返回检测到的对象在像素中的宽度,该值是 1 到 320 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Approach the Bean Bag, then pick it up
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.width > 200:
drivetrain.stop()
intake_motor.spin(FORWARD)
return
else:
drivetrain.drive(FORWARD)
break
# VR threads — Do not delete
vr_thread(main)
。高度#
.height 返回检测到的对象的高度(以像素为单位),该值是 1 到 240 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Approach the Bean Bag, then pick it up
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.height > 110:
drivetrain.stop()
intake_motor.spin(FORWARD)
return
else:
drivetrain.drive(FORWARD)
break
# VR threads — Do not delete
vr_thread(main)
.centerX#
.centerX 返回检测到的对象中心的 x 坐标(以像素为单位),该值是 0 到 320 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Steer to keep the Bean Bag centered
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.centerX < 140:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 150, MM)
intake_motor.spin(FORWARD)
return
break
# VR threads — Do not delete
vr_thread(main)
.centerY#
.centerY 返回检测到的对象中心的 y 坐标(以像素为单位),该值是 0 到 240 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Print the y-position of the Bean Bag
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
brain.screen.print(ai_object.centerY)
break
# VR threads — Do not delete
vr_thread(main)
.originX#
.originX 返回检测到的对象边界框左上角的 x 坐标(以像素为单位),该值是 0 到 320 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Steer to keep the left edge centered
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.originX < 100:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 150, MM)
intake_motor.spin(FORWARD)
return
break
# VR threads — Do not delete
vr_thread(main)
.originY#
.originY 返回检测到的对象边界框左上角的 y 坐标(以像素为单位),该值是 0 到 240 之间的整数。
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Print the y-position of the Bean Bag's top edge
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
brain.screen.print(ai_object.originY)
break
# VR threads — Do not delete
vr_thread(main)
。ID#
.id 返回检测到的 AI 分类的 ID,以整数形式表示。
人工智能分类 |
ID |
|---|---|
蓝豆袋 |
0 |
红豆袋 |
1 |
黄豆袋 |
2 |
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Pick up the Bean Bag if it's red
drivetrain.set_drive_velocity(30, PERCENT)
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 150, MM)
break
# VR threads — Do not delete
vr_thread(main)
def main():
# Stop when you see the Red Goal
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.turn(RIGHT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
for ai_object in ai_objects:
if ai_object.id == 1:
drivetrain.stop()
brain.screen.print("Found the Red Goal!")
return
# VR threads — Do not delete
vr_thread(main)