机器人专用 Python#
介绍#
Hero Bot Flex 包含爪电机、手臂电机、AI 视觉传感器、距离传感器和游戏定位系统 (GPS) 传感器。
所有标准的 VEXcode VR 方法都可以在 V5 26-27 覆盖 Playground 中使用。
以下是所有可用的机器人专用方法列表:
驱动系统——控制机器人的移动和转向。
操作
drive— 使机器人无限向前或向后移动。drive_for— 使机器人向前或向后移动指定的距离。turn— 使机器人永远向左或向右转弯。turn_for— 使机器人向左或向右旋转指定的度数。turn_to_heading— 将机器人转向 -359 到 359 度之间的特定航向。turn_to_rotation— 将机器人旋转到指定的方向。stop— 停止机器人的运动。
变异因子
set_heading— 将机器人的当前航向更改为新的航向。set_rotation— 将机器人的当前旋转更改为新的旋转。set_timeout— 设置机器人尝试完成一次动作的时间。set_drive_velocity— 告诉机器人以多快的速度行驶。set_turn_velocity— 告诉机器人转弯的速度。
Getters
运动控制——移动并跟踪机器人的电机。
操作
spin— 使电机永远正转或反转。spin_for— 使电机旋转指定的距离。spin_to_position— 将电机旋转到特定位置。stop— 停止电机旋转。
变异因子
set_position— 将电机的当前位置更改为新值。set_velocity— 告诉电机旋转的速度。set_timeout— 设置电机尝试完成一次运动的时间。
Getters
is_done— 返回电机是否停止移动,以布尔值表示。is_spinning— 返回电机是否旋转,以布尔值表示。position— 返回电机的当前位置。velocity— 返回电机的旋转速度。
人工智能视觉——使用人工智能视觉传感器捕捉和分析物体。
Getters
take_snapshot– 返回特定签名检测到的对象元组。
特性
控制台 – 将信息打印到控制台并监控数值。
打印
brain.screen.print— 在控制台中显示文本、数字或变量值。brain.screen.next_row— 将控制台光标移动到下一行的开头。brain.screen.clear_screen— 清除控制台中的所有行。
监视器
monitor_variable— 向“监视器”选项卡添加一个或多个预定义变量。monitor_sensor— 向监视器选项卡添加一个或多个传感器值。
感知——利用机器人的各种传感器。
距离
found_object– 返回距离传感器是否检测到范围内的对象。object_distance– 返回距离传感器与最近检测到的对象之间的距离。
全球定位系统
x_position– 返回场地中 GPS 传感器的当前 x 坐标。y_position– 返回场地中 GPS 传感器的当前 y 坐标。heading– 根据 GPS 传感器返回机器人当前所面向的航向。
本页示例使用游乐场赛前检查清单中的起始位置 C。
传动系统#
动力系统控制着VR机器人的行驶和转向。动力系统可以前进或后退、左转或右转、转向特定航向并跟踪其旋转。
行动#
驾驶#
drive 使机器人无限前进或后退。机器人将持续移动,直到收到其他指令,例如转向或停止。
Usage:
drivetrain.drive(direction)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive(FORWARD)
wait(0.5, SECONDS)
drivetrain.stop()
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
驱动#
drive_for 使机器人前进或后退指定距离。程序会等待机器人完成移动后才执行下一行代码。
Usage:
drivetrain.drive_for(direction, distance, units, wait)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
|
机器人行驶的距离。这可以是整数 |
|
可选。距离单位: |
|
可选 |
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转动#
turn 表示机器人无限向左或向右转向。机器人将持续转向,直到收到其他指令,例如行驶或停止。
Usage:
drivetrain.turn(direction)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn(LEFT)
wait(0.78, SECONDS)
drivetrain.stop()
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转弯#
turn_for 参数使机器人向左或向右旋转指定角度。旋转角度是相对于机器人当前位置的。程序会等待机器人完成旋转后再执行下一行代码。
Usage:
drivetrain.turn_for(direction, angle, units, wait)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
|
The number of degrees the robot turns. This can be an |
|
可选。角度单位: |
|
可选 |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_for(LEFT, 110, DEGREES)
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转向航向#
航向角是指机器人朝向的方向,以度为单位。`turn_to_heading 将机器人转向 -359 度到 359 度之间的特定航向角。机器人将沿最短路径转向以达到目标航向角。
起始航向为0度。
Usage:
drivetrain.turn_to_heading(heading, units, wait)
参数 |
描述 |
|---|---|
|
机器人应面向的航向,范围从 -359 度到 359 度。 |
|
可选。角度单位: |
|
可选 |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_to_heading(-110, DEGREES)
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转向旋转#
turn_to_rotation 将机器人旋转到指定角度。
旋转角度是指机器人转动的幅度,以度为单位。项目开始时,旋转角度设置为 0 度。向右转动会增加旋转角度,向左转动会减少旋转角度。
例如,如果机器人从0度开始旋转720度,它将向右转两次。如果它接着旋转360度,它将向左转一次,因为360小于720。
Usage:
drivetrain.turn_to_rotation(rotation, units, wait)
参数 |
描述 |
|---|---|
|
机器人将旋转到的旋转值,以度为单位。这可以是整数 |
|
可选。角度单位: |
|
可选 |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_to_rotation(-110, DEGREES)
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
停止#
stop 停止机器人的移动。
Usage:
drivetrain.stop()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn(LEFT)
wait(0.78, SECONDS)
drivetrain.stop()
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
变异体#
设置标题#
航向角是指机器人面向的方向,以度为单位。0 set_heading 将机器人的当前航向角更改为新的航向角值。
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。
Usage:
drivetrain.set_heading(heading, units)
参数 |
描述 |
|---|---|
|
要设置的机器人航向值(以度为单位)。 |
|
可选。角度单位: |
def main():
# Make facing the Neutral Goal the new heading
drivetrain.set_heading(110, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置旋转#
旋转角度是指机器人转动的幅度,以度为单位。0 set_rotation 将机器人的当前旋转角度更改为新值。
Usage:
drivetrain.set_rotation(rotation, units)
参数 |
描述 |
|---|---|
|
要设置的机器人旋转值,以度为单位。这可以是整数 |
|
可选。角度单位: |
def main():
# Make facing the Neutral Goal the new rotation
drivetrain.set_rotation(110, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置超时#
set_timeout 设置机器人尝试完成一个动作的超时时间(以秒为单位)。如果机器人无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止机器人卡在某个动作中。
Usage:
drivetrain.set_timeout(time, units)
参数 |
描述 |
|---|---|
|
The number of seconds the robot can try to finish a movement. This can be a positive |
|
可选。时间单位: |
def main():
# Turn to face the Neutral Goal
drivetrain.set_timeout(0.8, SECONDS)
drivetrain.turn_for(LEFT, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置驱动速度#
set_drive_velocity 告诉机器人行驶速度。百分比越高,机器人行驶速度越快;百分比越低,机器人行驶速度越慢。
每个项目开始时,机器人默认以 50% 的速度行驶。
注意:速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。
Usage:
drivetrain.set_drive_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
行驶速度,范围从 0% 到 100%。可以是整数 |
|
可选。速度单位: |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_for(LEFT, 110, DEGREES)
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置转弯速度#
set_turn_velocity 告诉机器人转弯的速度。百分比越高,机器人转弯越快;百分比越低,机器人转弯越慢。
每个项目开始时,机器人默认以 50% 的速度旋转。
注意:速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。
Usage:
drivetrain.set_turn_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
转弯速度,范围从 0% 到 100%。可以是整数 |
|
可选。速度单位: |
def main():
# Turn to face the Neutral Goal faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(LEFT, 110, DEGREES)
# VR threads — Do not delete
vr_thread(main)
获取器#
标题#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359.99 degrees.
机器人的初始航向为0度。
Usage:
drivetrain.heading(units)
参数 |
描述 |
|---|---|
|
可选。航向单位: |
def main():
# Turn to the Goal, then show heading
drivetrain.turn_for(LEFT, 110, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading(DEGREES))
# VR threads — Do not delete
vr_thread(main)
旋转#
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.
向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。
Usage:
drivetrain.rotation(units)
参数 |
描述 |
|---|---|
|
可选。旋转单位: |
def main():
# Spin to find a Pin, then show rotation
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation(DEGREES))
# VR threads — Do not delete
vr_thread(main)
已完成#
is_done 返回机器人是否已完成移动,返回值为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
True— 机器人已停止移动。False— 机器人仍在移动。
此方法与以下具有 wait 参数的传动系统方法配合使用:drive_for, turn_for, turn_to_heading, 和 turn_to_rotation。
Usage:
drivetrain.is_done()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_for(LEFT, 110, DEGREES)
arm_motor.spin_for(FORWARD, 800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES, wait=False)
wait(0.5, SECONDS)
while not drivetrain.is_done():
wait(5, MSEC)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
正在移动#
is_moving 返回机器人是否正在移动,返回值为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
True— 机器人正在移动。False— 机器人没有移动。
Usage:
drivetrain.is_moving()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Raise the Arm once stopped near the Goal
drivetrain.turn_for(LEFT, 110, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES, wait=False)
wait (0.5, SEC)
while drivetrain.is_moving():
wait(5, MSEC)
arm_motor.spin_for(FORWARD, 800, DEGREES)
# VR threads — Do not delete
vr_thread(main)
速度#
velocity 返回机器人行驶的速度,以 -100% 到 100% 的百分比表示。
Usage:
drivetrain.velocity(units)
参数 |
描述 |
|---|---|
|
可选。速度单位: |
def main():
# Show speed while driving to the Goal
drivetrain.drive_for(FORWARD, 150, MM)
brain.screen.print("Velocity: ")
brain.screen.print(drivetrain.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
运动#
Flex 使用机械臂电机来升降机械臂,使用机械爪电机来打开和关闭机械爪。
每个电机都有其自身的运动方向。方向描述解释了每个方向如何驱动 Flex 上的电机运动。
行动#
旋转#
spin 会使电机沿两个方向之一持续旋转。电机将持续旋转,直到收到其他指令,例如改变旋转方向或停止。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The direction the motor spins:
|
def main():
# Raise the Arm before driving toward a Pin
arm_motor.spin(FORWARD)
wait(1, SECONDS)
arm_motor.stop()
# VR threads — Do not delete
vr_thread(main)
spin_for#
spin_for 会使电机旋转指定距离。旋转距离是相对于电机当前位置的。程序会等待电机旋转完毕后再执行下一行代码。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The direction the motor spins:
|
|
电机旋转的距离。0 度 |
|
可选。距离单位: |
|
Optional.
|
def main():
# Raise the Arm to carry the Pin to the Goal
arm_motor.spin_for(FORWARD, 800, DEGREES)
# VR threads — Do not delete
vr_thread(main)
自旋至位置#
spin_to_position 将电机旋转到特定位置。
电机的位置是指它旋转的距离,以 DEGREES 或 TURNS进行测量。一圈等于 360 度。在项目开始时,机械臂电机的位置设置为 0 度。爪形电机是个例外——它的初始位置为 467 度(完全闭合)。也可以使用 set_position 方法设置电机位置。
位置值是绝对值。这意味着旋转方向取决于电机的当前位置。
例如,如果电机从0度开始旋转到720度,它将正转两圈。如果它接着旋转到360度,它将反转一圈,因为360小于720。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机旋转到的位置值。0 度 |
|
可选。位置单位: |
|
Optional.
|
def main():
# Raise the Arm, then release the Pin
arm_motor.spin_to_position(800, DEGREES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
停止#
stop 停止电机旋转。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Raise the Arm partway to hold the Pin
arm_motor.spin(FORWARD)
wait(1, SECONDS)
arm_motor.stop()
# VR threads — Do not delete
vr_thread(main)
变异体#
设置位置#
电机的位置是指它旋转的距离,以 DEGREES 或 TURNS测量。 set_position 用于将电机的当前位置更改为新值。
例如,如果电机旋转了 180 度,将位置设置为 0 度会将该位置从 180 度重置为 0 度。然后,电机可以根据这个新值旋转到其他位置。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机位置值设置。0 度 |
|
可选。位置单位: |
def main():
# Reset encoder, raise Arm, place Pin
arm_motor.set_position(0, DEGREES)
arm_motor.spin_to_position(800, DEGREES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置速度#
set_velocity 告诉电机旋转的速度。百分比越高,电机旋转速度越快;百分比越低,电机旋转速度越慢。
每个项目开始时,每个电机默认以 50% 的速度旋转。
注意:更高的速度会使电机转速更快,但精度可能会降低。更低的速度会使电机转速更慢,但精度可能会更高。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
旋转速度,范围从 0% 到 100%。可以是整数 |
|
速度单位: |
def main():
# Raise the Arm fast to release the Pin
arm_motor.set_velocity(100, PERCENT)
arm_motor.spin_for(FORWARD, 800, DEGREES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置超时#
set_timeout 设置电机尝试完成一次运动的超时时间。如果电机无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止电机卡在某个运动中。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The amount of time the motor can try to finish a movement. This can be a positive |
|
时间单位: |
def main():
# Score a Pin in a Neutral Goal
drivetrain.turn_for(LEFT, 110, DEGREES)
arm_motor.set_timeout(1, SECONDS)
arm_motor.spin_for(FORWARD, 1800, DEGREES)
drivetrain.drive_for(FORWARD, 25, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
获取器#
已完成#
is_done 返回电机是否停止运动,返回值为布尔值。这可用于根据电机的运动状态控制其他行为的执行时间。
True— 电机停止运转。False— 电机仍在运转。
此方法与以下具有 wait 参数的 Motion 方法配合使用:spin_for 和 spin_to_position。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Raise the Arm while driving to the Goal
arm_motor.spin_to_position(800, DEGREES, wait=False)
wait(0.1, SECONDS)
while not arm_motor.is_done():
drivetrain.drive(FORWARD)
wait(5, MSEC)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
正在旋转#
is_spinning 返回电机是否正在旋转的布尔值。这可用于根据电机的运动状态控制其他行为的时序。
True— 电机正在旋转。False— 电机不转动。
此方法与以下具有 wait 参数的 Motion 方法配合使用:spin_for 和 spin_to_position。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Raise the Arm while driving to the Goal, then release the Pin
arm_motor.spin_to_position(800, DEGREES, wait=False)
wait(0.1, SECONDS)
while arm_motor.is_spinning():
drivetrain.drive_for(FORWARD, 25, MM)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
位置#
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. position returns the motor’s current position.
项目开始时,机械臂电机的初始位置设置为 0 度。爪形电机是个例外——它的初始位置为 467 度(完全闭合)。如果电机向前旋转一整圈,位置就会增加;如果电机向反方向旋转,位置就会减少。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
将电机位置恢复到: |
def main():
# Raise the Arm, then release the Pin
while arm_motor.position(DEGREES) < 800:
arm_motor.spin(FORWARD)
wait(2, MSEC)
arm_motor.stop()
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
速度#
velocity 返回电机旋转的速度。
正值表示电机正转,负值表示电机反转。
用法:
此方法可以使用两种可用的电机对象之一,如下所示:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
要返回的速度单位: |
def main():
# Show Arm Motor velocity while raising
arm_motor.set_velocity(100, PERCENT)
arm_motor.spin_to_position(800, DEGREES, wait=False)
wait(0.2, SECONDS)
brain.screen.print(arm_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
人工智能视觉#
获取器#
拍摄快照#
take_snapshot 将来自 AI 视觉传感器帧的数据过滤为单个签名——传感器可以识别的事物的保存描述,例如场上的游戏元素——并返回一个元组。
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():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Turn to face the Pin
drivetrain.set_turn_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].centerX < 140:
drivetrain.turn(LEFT)
elif ai_objects[0].centerX > 180:
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
break
# VR threads — Do not delete
vr_thread(main)
特性#
使用 take_snapshot 后,存储在元组中的每个对象都包含七个属性。
除 .id 外,所有属性值均描述了在执行 take_snapshot 操作时,AI 视觉传感器视野中检测到的物体的位置和大小。这些值以像素为单位,基于传感器的 320 x 240 像素分辨率。
。宽度#
.width 返回检测到的对象在像素中的宽度,该值是 1 到 320 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Approach the next Cup and Pin
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].width > 140:
drivetrain.stop()
break
else:
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)
。高度#
.height 返回检测到的对象的高度(以像素为单位),该值是 1 到 240 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Approach the next Cup and Pin
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].height > 200:
drivetrain.stop()
break
else:
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)
.centerX#
.centerX 返回检测到的对象中心的 x 坐标(以像素为单位),该值是 0 到 320 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Turn and approach the next Cup and Pin
drivetrain.set_turn_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].centerX < 140:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 25, INCHES)
break
# VR threads — Do not delete
vr_thread(main)
.centerY#
.centerY 返回检测到的对象中心的 y 坐标(以像素为单位),该值是 0 到 240 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Approach the next Cup and Pin
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].centerY > 230:
drivetrain.stop()
break
else:
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)
.originX#
.originX 返回检测到的对象边界框左上角的 x 坐标(以像素为单位),该值是 0 到 320 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Turn and approach the next Cup and Pin
drivetrain.set_turn_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].originX < 100:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 25, INCHES)
break
# VR threads — Do not delete
vr_thread(main)
.originY#
.originY 返回检测到的对象边界框左上角的 y 坐标(以像素为单位),该值是 0 到 240 之间的整数。
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Approach the next Cup and Pin
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].originY > 140:
drivetrain.stop()
break
else:
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)
。ID#
.id 返回检测到的 AI 分类的 ID,以整数形式表示。
人工智能分类 |
ID |
签名 |
|---|---|---|
红蓝针 |
0 |
|
蓝黄针 |
1 |
|
红黄针 |
2 |
|
黄针 |
3 |
|
杯子 |
4 |
|
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 70, DEGREES)
# Display the ID of the next closest object
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
brain.screen.print(ai_objects[0].id)
break
# VR threads — Do not delete
vr_thread(main)
安慰#
项目运行时,控制台会显示文本、数字和变量值。控制台方法还可以将光标移动到新行、清除控制台以及向“监视器”选项卡添加变量或传感器值。
打印#
脑屏幕打印#
brain.screen.print 使用当前光标位置在控制台中显示文本、数字或变量值。
当您希望下一个打印值从新行开始时,请使用 brain.screen.next_row。
Usage:
brain.screen.print(value)
参数 |
描述 |
|---|---|
|
要在控制台中显示的文本、数字或变量值。 |
def main():
# Display a message in the Console
brain.screen.print("Hello, robot!")
# VR threads — Do not delete
vr_thread(main)
brain.screen.下一行#
brain.screen.next_row 将光标移动到控制台下一行的第 1 列。下一个打印的值将显示在该行。
Usage:
brain.screen.next_row()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print on two rows
brain.screen.print("Row 1")
brain.screen.next_row()
brain.screen.print("Row 2")
# VR threads — Do not delete
vr_thread(main)
brain.screen.clear_screen#
brain.screen.clear_screen 清除控制台中的所有行并将光标移回第 1 行。
Usage:
brain.screen.clear_screen()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Display text, then clear it after two seconds
brain.screen.print("This will disappear...")
wait(2, SECONDS)
brain.screen.clear_screen()
# VR threads — Do not delete
vr_thread(main)
监视器#
监控变量#
monitor_variable 将一个或多个预定义变量添加到 VEXcode 的“监视器”选项卡中。这样,您就可以在项目运行时监视变量值的变化。
要成功监控 monitor_variable 变量必须是全局变量。请以字符串形式提供每个变量名。
Usage:
monitor_variable(“variable”)
monitor_variable(“variable1”, “variable2”)
参数 |
描述 |
|---|---|
|
要监控的预定义全局变量的名称,以字符串形式给出。要监控多个变量,请用逗号分隔每个变量名。 |
def main():
# Monitor the number of loops
global loops
monitor_variable("loops")
# Turn in a full circle 3 times
for loops in range(12):
drivetrain.turn_for(RIGHT, 90, DEGREES)
# VR threads — Do not delete
vr_thread(main)
监控传感器#
monitor_sensor 将一个或多个传感器值添加到 VEXcode 的“监视器”选项卡中。这样,您就可以在项目运行时观察传感器值的变化。
请以字符串形式提供每个传感器的值。
Usage:
monitor_sensor(“sensor”)
monitor_sensor(“sensor1”, “sensor2”)
参数 |
描述 |
|---|---|
|
The sensor value to monitor, given as a string. To monitor more than one sensor value, separate each sensor value with a comma. Supported sensor identifiers include:
|
def main():
# Monitor the robot's heading
monitor_sensor("drivetrain.heading")
drivetrain.turn_for(RIGHT, 450, DEGREES)
# VR threads — Do not delete
vr_thread(main)
传感#
距离#
找到的对象#
found_object 返回距离传感器当前是否检测到 2000 毫米内的物体。
True— 距离传感器检测到物体。False— 距离传感器未检测到物体。
Usage:
distance.found_object()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Turn until something is detected, then release the Pin
drivetrain.turn(RIGHT)
while True:
wait(5, MSEC)
if distance.found_object():
drivetrain.stop()
break
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
物体距离#
object_distance 返回距离传感器与最近检测到的物体之间的距离。
Usage:
distance.object_distance(units)
参数 |
描述 |
|---|---|
|
距离单位: |
def main():
# Drive until 200 mm from the Goal, then release the Pin
while distance.object_distance(MM) > 200:
wait(5, MSEC)
drivetrain.drive(FORWARD)
drivetrain.stop()
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
全球定位系统#
x_position#
x_position 返回场上 GPS(游戏定位系统™)传感器的当前 x 坐标。
Usage:
gps.x_position(units)
参数 |
描述 |
|---|---|
单位 |
偏移值的单位: |
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive(FORWARD)
while not gps.x_position(MM) > -1370:
wait(2, MSEC)
drivetrain.stop()
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
y 位置#
y_position 返回场上 GPS(游戏定位系统™)传感器的当前 y 坐标。
Usage:
gps.y_position(units)
参数 |
描述 |
|---|---|
单位 |
偏移值的单位: |
def main():
# Raise the Arm, drive to the Goal, release the Pin
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive(FORWARD)
while not gps.y_position(MM) < -450:
wait(2, MSEC)
drivetrain.stop()
claw_motor.spin_for(REVERSE, 180, DEGREES)
# VR threads — Do not delete
vr_thread(main)
标题#
heading 返回机器人当前面向的航向,该航向基于 GPS(游戏定位系统™)传感器的读数,范围为 0 到 359 度。
Usage:
gps.heading()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Raise the Arm, drive to the Goal, release the Pin
monitor_sensor("gps.heading")
arm_motor.spin_for(FORWARD, 300, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)
claw_motor.spin_for(REVERSE, 180, DEGREES)
# Turn left to face the Neutral Goal
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.turn(LEFT)
while not gps.heading() < 18:
wait(2, MSEC)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)