机器人专用 Python#

介绍#

英雄机器人 Flop 包含一个进气电机、一个机械臂电机和一个 IQ AI 视觉传感器。

所有标准的 VEXcode VR Python 命令都可以在 IQ 26-27 Level Up Playground 中使用。

以下是所有可用的机器人专用 Python 命令列表:

运动控制——移动并跟踪机器人的电机。

  • 操作

    • spin – 使电机永远朝一个方向旋转。

    • spin_for – 使电机旋转特定角度或圈数。

    • spin_to_position – 将电机旋转到指定位置。

    • stop – 停止电机旋转。

  • 设置

  • 价值观

    • is_done – 返回电机是否停止运转。

    • is_spinning – 返回电机是否正在旋转。

    • position – 返回电机的当前位置。

    • velocity – 返回电机的旋转速度。

AI视觉——使用IQ AI视觉传感器捕捉和分析物体。

  • Getters

    • take_snapshot – 将传感器的当前帧过滤为特定签名,并返回检测到的对象元组。

  • 特性

    • .width – 返回检测到的对象以像素为单位的宽度。

    • .height – 返回检测到的对象的高度(以像素为单位)。

    • .centerX – 返回检测到的对象的中心的 x 坐标。

    • .centerY – 返回检测到的对象中心的 y 坐标。

    • .originX – 返回检测到的对象边界框左上角的 x 坐标。

    • .originY – 返回检测到的对象边界框左上角的 y 坐标。

    • .id – 返回检测到的 AI 分类的 ID。

本页示例使用 Playground 的默认起始位置。

运动#

Flop has two robot-specific motors: intake_motor and arm_motor. Both use standard motor commands.

方向常量:

  • intake_motor: FORWARD = intake (collects); REVERSE = outtake (ejects)

  • arm_motor: FORWARD = up (raises); REVERSE = down (lowers)

旋转#

spin spins a motor in the given direction forever. The motor will continue to spin until it is given another command.

Usage:
motor.spin(direction)

参数

描述

direction

The direction to spin: FORWARD or REVERSE.

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 spins a motor for a specific distance. The project waits until the motor is done before the next command runs unless wait=False is passed.

Usage:
motor.spin_for(direction, amount, unit)

参数

描述

direction

The direction to spin: FORWARD or REVERSE.

amount

旋转距离。可以是整数或小数。

unit

The unit of measurement: DEGREES or TURNS.

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 spins a motor to a specific position. The project waits until the motor is done before the next command runs unless wait=False is passed.

Usage:
motor.spin_to_position(position, unit)

参数

描述

position

要旋转到的位置。可以是整数或小数。

unit

The unit of measurement: DEGREES or TURNS.

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 stops a motor from spinning.

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 sets how fast a motor will spin as a percentage from 0 to 100.

Usage:
motor.set_velocity(velocity, unit)

参数

描述

velocity

电机转速,以百分比表示,范围从 0 到 100。

unit

The unit of measurement: PERCENT.

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 sets how many seconds a motor will try to finish a movement before stopping.

Usage:
motor.set_timeout(timeout, unit)

参数

描述

timeout

电机尝试完成一次动作的秒数。

unit

The unit of measurement: SECONDS.

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 changes the motor’s current position to a new value, resetting its encoder.

Usage:
motor.set_position(position, unit)

参数

描述

position

新的位置值。

unit

The unit of measurement: DEGREES or TURNS.

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 returns a Boolean indicating whether the motor has finished its movement.

  • True – The motor has finished moving.

  • False – The motor is still moving.

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 returns a Boolean indicating whether the motor is currently spinning.

  • True – The motor is spinning.

  • False – The motor is not spinning.

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 returns the motor’s current position.

Usage:
motor.position(unit)

参数

描述

unit

The unit of measurement: DEGREES or TURNS.

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 returns how fast the motor is currently spinning as a percentage from -100 to 100.

Usage:
motor.velocity(unit)

参数

描述

unit

The unit of measurement: PERCENT.

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 filters data from the IQ AI Vision Sensor frame to a single signature — a saved description of something the sensor can recognize, such as a game element on the field — and returns a tuple.

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

Usage:
ai_vision.take_snapshot(signature)

参数

描述

signature

Filters the dataset to only include data of the given signature. Available signatures are:

  • AiVision.ALL_AIOBJS - Detects all Bean Bags.
  • AiVision.ALL_TAGS - Detects AprilTags, found on the sides of the Goals.

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)

特性#

There are seven properties that are included with each object stored in a tuple after take_snapshot is used.

All property values except .id describe the detected object’s position and size in the IQ AI Vision Sensor’s view at the moment take_snapshot was used. These values are measured in pixels, based on the sensor’s 320 by 240 pixel resolution.

。宽度#

.width returns the width of the detected object in pixels, which is an integer between 1 and 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 returns the height of the detected object in pixels, which is an integer between 1 and 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 returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 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 returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 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 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 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 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 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 returns the ID of the detected AI Classification as an integer.

人工智能分类

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)